在Android中的Onclicklistener上的空指针异常(Null pointer Exception at Onclicklistener in android)

编程入门 行业动态 更新时间:2024-10-28 14:26:49
在Android中的Onclicklistener上的空指针异常(Null pointer Exception at Onclicklistener in android)

如何在menu.setOnClickListener(新的View.OnClickListener(){的位置恢复Null指针异常的问题,如果我按下另一个活动(关于页面)应该打开,我有一个imageview(菜单),但在这里点击图片查看,我得到上面的错误,应用程序强行关闭。这是代码

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"/>

How to recover the problem of Null pointer exception in the place of menu.setOnClickListener(new View.OnClickListener() { , i have an imageview(menu) if i press on that another activity(about page) should get opened, but here on OnClick of imageview, i'm getting above error and app gets force close. Here's the code

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 file

<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"/>

最满意答案

我怀疑,您已将菜单声明为实例变量(位于类的顶部),但您从未将其实例化为局部变量。 添加行

menu = (ImageView)findViewById(R.id.menu)

在onCreate()方法的顶部,在设置OnClickListener之前,它将起作用。

声明实例变量(您已经完成)和实际初始化它之间存在差异。 从这个意义上讲,您可以像常规java类中的构造函数一样查看onCreate()方法。 将变量声明在类的顶部是不够的,您必须将其初始化为实际对象的变量。

这可能听起来微不足道,但相信我并非如此。 当我第一次学习java时,在围绕这个概念开始之前,我花了很多时间在屏幕上尖叫着对你现在遇到的同样的错误。

As I suspected, you have declared menu as an instance variable (at the top of your class), but you never instantiate it as a local variable. Add the line

menu = (ImageView)findViewById(R.id.menu)

at the top of your onCreate() method, BEFORE you set the OnClickListener, and it will work.

There is a difference between declaring the instance variable (which you have done) and actually initializing it. In this sense, you can look at the onCreate() method like a constructor in a regular java class. It's not enough to declare the variable at the top of the class, you have to initialize it as a variable of the actual object.

This might sound trivial, but believe me it's not. When I was first learning java, before I wrapped my head around this concept, I spent many hours screaming at the screen over the same sorts of errors you're having now.

更多推荐

本文发布于:2023-07-04 10:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023485.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   异常   Onclicklistener   Android   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!