我的应用程序的所有页面都有一个“菜单”图像视图,如果点击菜单,所有菜单项如登录,家庭,工作,约会打开,如果我点击“关于”menuitem应用程序获得强制关闭并在“OnClicklistener of menu”上获取空指针异常,但其他Menuitems没有此问题,请有人解决此问题吗?
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.setOnClickListener(new View.OnClickListener() {
ImageView menu = (ImageView)findViewById(R.id.menu);
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 home = (ImageView) findViewById(R.id.home);
home.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, Home.class));
}
});
ImageView jobs = (ImageView) findViewById(R.id.jobs);
jobs.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, Jobs.class));
}
});
ImageView log = (ImageView) findViewById(R.id.log);
log.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(About.this, Login.class));
}
});
ImageView about = (ImageView) findViewById(R.id.about);
about.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
startActivity(new Intent(getApplicationContext(), 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" />
<ImageView
android:id="@+id/jobs"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/jobs" />
<ImageView
android:id="@+id/log"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/log" />
<ImageView
android:id="@+id/home"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/home" />
</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"/>
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="710dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="46dp"
android:text="@string/AboutPage"
android:textColor="@color/white" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ll1"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="@string/WelcometoRebuixcom"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white" />
答案 0 :(得分:1)
你应该移动代码
ImageView menu = (ImageView)findViewById(R.id.menu);
前
menu.setOnClickListener(new View.OnClickListener() {}
答案 1 :(得分:1)
试试这个
ImageView menu = (ImageView)findViewById(R.id.menu);
menu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {............
你应该把
ImageView menu = (ImageView)findViewById(R.id.menu);
befor
menu.setOnClickListener
答案 2 :(得分:1)
ImageView menu;
menu.setOnClickListener(new View.OnClickListener() {
ImageView menu = (ImageView)findViewById(R.id.menu);
查看这些代码行,第二个是强制关闭的原因,因为menu
NULL 。
就像交换它一样,
menu = (ImageView)findViewById(R.id.menu);
menu.setOnClickListener(new View.OnClickListener() {
<强>更新强>
您宣布ImageView menu;
现在第二行menu.setOnClickListener(new View.OnClickListener() {
在没有定义ImageView menu
的情况下,您要为其设置setOnCLickListener()
,这会导致 NullPointerException 。
因此,您必须在声明后定义ImageView menu
,如menu = (ImageView)findViewById(R.id.menu);
。
如何声明和定义对象和成员变量,请参阅基本的Android编程和核心Java教程。我认为你的编程概念很差。
答案 3 :(得分:1)
初始化你的imageview befor onllick listener,如下所示:
ImageView menu = (ImageView)findViewById(R.id.menu); <<---- Here 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); } } });