简体   繁体   English

Android中的Onclicklistener上的空指针异常

[英]Null pointer Exception at Onclicklistener in android

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 如何恢复出现空指针异常的问题,而不是在menu.setOnClickListener(new View.OnClickListener(){)处,如果我按另一个应打开的活动(关于页面),则会显示一个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 file 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"/>

you are getting NullPointerException because you didn't have initialize imageview.please do it first. 您会收到NullPointerException,因为您没有初始化imageview。请先执行。

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

write this line before you set onClick Listener to your image view and you are done.hope it will help. 在将onClick Listener设置为图像视图之前完成此行,然后完成。希望它会有所帮助。

You've not properly initialized the menu variable before setting listener you should 在设置监听器之前,您尚未正确初始化菜单变量

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

then write your listen line.... 然后写你的听线...

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. 正如我所怀疑的那样,您已经将menu声明为实例变量(在类的顶部),但是您从未将其实例化为局部变量。 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. 在onCreate()方法的顶部,在设置OnClickListener之前,它将起作用。

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. 从这个意义上讲,您可以像常规java类中的构造函数一样查看onCreate()方法。 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. 当我第一次学习Java时,在我深入研究这个概念之前,我花了很多时间在屏幕上大喊关于您现在遇到的相同错误。

您应该传递正在运行的活动的上下文,而不是您想要运行的活动,问题是About.this,将其替换为当前Activity的上下文

startActivity(new Intent(About.this, About.class));

you have not Instantiate menu 1st instantiate. 您还没有实例化菜单1st实例化。

You are calling the same class with in the same activity change your following code as following 您在相同的活动中调用相同的类,如下所示更改您的以下代码

  startActivity(new Intent(About.this, About.class));

to

 startActivity(new Intent(getApplicationContext(), NewClass.class));

or 要么

 startActivity(new Intent(CurrentClass.this, NewClass.class));

I had a similar issue. 我有一个类似的问题。 I found the xml id I'd used in findViewById() came from a different page layout than the one active at the time. 我发现在findViewById()中使用的xml ID来自与当时处于活动状态的页面不同的页面布局。

No warnings. 没有警告。 Just need to make sure common buttons like Save have a good naming convention. 只需确保诸如“保存”之类的常用按钮具有良好的命名约定即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM