如何在 menu.setOnClickListener(new View.OnClickListener() { 的位置恢复 Null 指针异常的问题,如果我按下另一个活动(关于页面)应该打开,我有一个 imageview(menu),但在这里Imageview的OnClick,我遇到了错误并且应用程序强制关闭。这是代码
public class About extends Activity {
LinearLayout line1, line2;
ImageView menu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
menu = (ImageView)findViewById(R.id.menu);
menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
menu.setVisibility(View.VISIBLE);
// TODO Auto-generated method stub
line1.setVisibility(View.VISIBLE);
if (line2.getVisibility() == View.INVISIBLE || line2.getVisibility() == View.GONE) {
line2.setVisibility(View.VISIBLE); }
else {
line2.setVisibility(View.INVISIBLE);
}
}
});
ImageView about = (ImageView) findViewById(R.id.about);
about.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, About.class));
}
});
xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black" >
<LinearLayout
android:id="@+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/black"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
<ImageView
android:id="@+id/menu"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/menu" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:background="@color/black"
android:layout_toRightOf="@+id/ll1"
android:visibility="gone"
>
<ImageView
android:id="@+id/about"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@+id/textView1"
android:layout_toLeftOf="@+id/jobs"
android:src="@drawable/about" />
</LinearLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@color/white"
android:textColor="@color/white"/>