具有新布局的nullpointer异常

时间:2013-04-02 16:34:46

标签: android layout layout-inflater

我遇到了下一个问题:

  • 我的活动有自己的布局,名为camera_layout,当我 让我在该文件中的所有布局都没问题,
  • 现在显示一些 我的扩展表面视图的视图我添加了另一个布局 使用这些观点的代码
  • 以便将此linearlayout添加到 添加后包含扩展表面视图的框架布局 在我的oncreate方法上,我使用设置了一些视图 findviewbyid
  • 此时一切正常
  • 在此onpreview之后
  • 方法我在我设置的视图中得到一个nullpointer异常。

这是我的代码

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



}

我的相机布局:

 <?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>

我的视图布局我希望将其放在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>

2 个答案:

答案 0 :(得分:2)

使用

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

而不是

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

因为当前活动布局中不存在textViewActual id的TextView textViewActualC

如果应用程序崩溃,

并始终附加相关的logcat结果和问题

答案 1 :(得分:0)

我已经改变了一些错误的东西,我在这里发布新代码,问题是我的cViewActual和之后的所有视图都是null,因为它们不在我的主要布局文件中,我认为在膨胀之后一个view-&gt; itemlayout,我的二级布局文件的布局我可以使用findViewById访问该布局的视图,但似乎这找不到视图,现在我改变了一些东西,在itemlayout视图上使用findViewById和现在我可以找到二级布局的视图

这是我修改后的代码:

    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);
}
相关问题