user1805792
user1805792

Reputation: 329

nullpointer exception with a new layout

I am having the next problem:

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>

Upvotes: 0

Views: 208

Answers (2)

user1805792
user1805792

Reputation: 329

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);
}

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

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

Upvotes: 2

Related Questions