0

I am having the next problem:

  • i have an activity with its own layout called camera_layout, when i had all my layout in that file all went ok,
  • now to display some of the views over my extended surfaceview i have added another layout with the code for those views
  • in order to add this linearlayout to the frame layout that contains my extended surfaceview after i add the the surfaceview, on my oncreate method i set some views using findviewbyid,
  • at this moment all is ok
  • after this on my onpreview method i get a nullpointer exception at that views i set.

Here is my code:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Log.d("Function", "onCreate iniciado");
    setContentView(R.layout.camera_layout);

    //tFrame=new Target_Frame(this);
    cViewActual=(CView)findViewById(R.id.cViewActual);
    bestMatchCView=(CView)findViewById(R.id.cViewBestMatch);

    actualCLabelTextView=(TextView)findViewById(R.id.textViewActualC);
    bestMatchLabelTextView=(TextView)findViewById(R.id.BestMatchtextViewLabel);
    coNametextView=(TextView)findViewById(R.id.textViewCName);

    mCamera=getCameraInstance();
    mPreview=new CameraPreview(this,mCamera,cViewActual,bestMatchCView,colorNametextView);          
    FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview);

    hasFlash=checkFlash(this);
    flashActivated=false;
    flashButton=new ImageButton(this);

    flashButton.setImageResource(R.drawable.flashofficon);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
    params.gravity=Gravity.CENTER;

    flashButton.setLayoutParams(params);
    addListenerToFlashButton();

    preview.addView(mPreview);

    preview.addView(flashButton);
    View  itemLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_bar_layout,null,false);
    preview.addView(itemLayout);
}

My camera layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="5dp"
            android:layout_height="5dp"
            android:layout_gravity="top|left"           
            android:text="Button" />

    </FrameLayout>

</LinearLayout>

And my layout for the views I want to put over the surfaceview:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="113dp"
    android:layout_height="fill_parent"
    android:gravity="right"
    android:id="@+id/itemToolLayout" 
    android:orientation="vertical">

    <TextView
        android:id="@+id/textViewActualC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Actual Color"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <com.example.prueba.CView
        android:id="@+id/cViewActual"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/BestMatchtextViewLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Best Match"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <com.example.prueba.CView
        android:id="@+id/cViewBestMatch"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/textViewCName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Color Name"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button_capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Capture" />

</LinearLayout>
gotwo
  • 663
  • 8
  • 16
user1805792
  • 319
  • 1
  • 4
  • 17

2 Answers2

2

use

actualCLabelTextView=(TextView)findViewById(R.id.textViewActualC);

instead of

actualCLabelTextView=(TextView)findViewById(R.id.textViewActual);

because TextView with textViewActual id not exist in current Activity layout it is textViewActualC

and always attach relevant logcat results with question if application is crashing

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 1
    reverse your instructions, he should use `R.id.textViewActualC` insteaf of `R.id.textViewActual` – Houcine Apr 02 '13 at 17:01
  • The error of the textviewid was an error pasting my code here, so mi nullpointer exception is not because of this – user1805792 Apr 02 '13 at 18:29
  • It seems that all the views that i load from the secondary layout file are null so i think that the probles is that code like this: cViewActual=(ColorView)findViewById(R.id.colorViewActual); doesn´t load the view into that variable with that code i only achieve to load views from the principal layout file – user1805792 Apr 03 '13 at 13:53
0

I have changed some things that were wrong and i post here the new code, the problem was that my cViewActual and all the views after this are null, because they aren´t on my principal layout file, i thought that after inflate a view->itemlayout with the layout of my secondary layout file i could access to the views of that layout using findViewById, but it seems that this doesn´t find the views, now i change some thing to use findViewById on itemlayout view and now i can find the views of the secondary layout

Here is my modified code:

    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Log.d("Function", "onCreate iniciado");
    setContentView(R.layout.camera_layout);


    FrameLayout preview=(FrameLayout)findViewById(R.id.camera_preview);


    hasFlash=checkFlash(this);
    flashActivated=false;
    flashButton=new ImageButton(this);

    flashButton.setImageResource(R.drawable.flashofficon);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
    params.gravity=Gravity.CENTER;

    flashButton.setLayoutParams(params);
    addListenerToFlashButton();


    View  itemLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_bar_layout,null,false);

    tFrame=new Target_Frame(this);
    cViewActual=(ColorView)itemLayout.findViewById(R.id.colorViewActual);   
    bestMatchCView=(ColorView)itemLayout.findViewById(R.id.colorViewBestMatch);
    actualColorLabelTextView=(TextView)itemLayout.findViewById(R.id.textViewActualColor);
    bestMatchLabelTextView=(TextView)itemLayout.findViewById(R.id.BestMatchtextViewLabel);
    colorNametextView=(TextView)itemLayout.findViewById(R.id.textViewColorName);
    mCamera=getCameraInstance();
    mPreview=new CameraPreview(this, mCamera,cViewActual,bestMatchCView,colorNametextView);

    preview.addView(mPreview);
    preview.addView(flashButton);
    preview.addView(itemLayout);
}
user1805792
  • 319
  • 1
  • 4
  • 17