简体   繁体   中英

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

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

you are getting NullPointerException because you didn't have initialize imageview.please do it first.

 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.

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. 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.

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

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

you have not Instantiate menu 1st instantiate.

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.

No warnings. Just need to make sure common buttons like Save have a good naming convention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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