我遇到下一个问题:
camera_layout
我在该文件中的所有布局都正常,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>
使用
actualCLabelTextView=(TextView)findViewById(R.id.textViewActualC);
代替
actualCLabelTextView=(TextView)findViewById(R.id.textViewActual);
因为当前活动布局中不存在具有textViewActual
id的TextView,所以它是textViewActualC
并且如果应用程序崩溃,请始终附上相关的logcat结果,并提出问题。
我已经更改了一些错误的地方,我在这里发布了新代码,问题是我的cViewActual和此后的所有视图都是空的,因为它们不在我的主要布局文件中,所以我认为在view-> itemlayout与我的辅助布局文件的布局,我可以使用findViewById访问该布局的视图,但是似乎这找不到视图,现在我更改了一些东西以在itemlayout视图上使用findViewById,现在我可以找到辅助布局的视图