1

I have a about screen that has some buttons, but I keep getting NullPointerException in the first setOnClickListener line,what's the problem?

about.java

public class About extends Activity{

    private Button backbutton;
    private Button likebutton;
    private Button faq;
    private Button update;
    private Button feedback;
    private Button rate;
    private Button help;

    private String faqpath ="http://appkon.com/hdtvs/faq.html";
    private String faqname ="常见问题";
    private String feedbackpath="http://appkon.com/hdtvs/feedback.html" ;
    private String feedbackname="反馈问题" ;
    private String ratename="评价APP" ;
    private String ratepath="http://appkon.com/hdtvs/rate.html" ;
    private String updatepath="http://appkon.com/hdtvs/update.html" ;
    private String updatename="检查更新" ;

    private String wentiname ="";
    private String wentipath ="";
    private String gengxinname ="";
    private String gengxinpath ="";
    private String pingjianame ="";
    private String pingjiapath ="";
    private String changjianpath ="";
    private String changjianname ="";



    @Override    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        backbutton=(Button) findViewById(R.id.back);
        likebutton=(Button) findViewById(R.id.share);
        faq =(Button)findViewById(R.id.faq);
        update =(Button)findViewById(R.id.update);
        feedback =(Button)findViewById(R.id.feedback);
        rate =(Button)findViewById(R.id.rate);
        help =(Button)findViewById(R.id.child);

        try{


        backbutton.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, HDtvs.class);
            startActivity(intent);
            About.this.finish();

          }
        });

        likebutton.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, Renrenframe.class);
            startActivity(intent);
            About.this.finish();

          }
        });

        faq.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, Aboutframe.class);
            Bundle bundle = new Bundle();
            bundle.putString("changjianpath",faqpath);
            bundle.putString("changjianname",faqname);
            intent.putExtras(bundle);
            startActivity(intent);
            About.this.finish();

          }
        });

        feedback.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, Aboutframe.class);
            Bundle bundle = new Bundle();
            bundle.putString("wentipath",feedbackpath);
            bundle.putString("wentiname",feedbackname);
            intent.putExtras(bundle);
            startActivity(intent);
            About.this.finish();

          }
        });

        rate.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, Aboutframe.class);
            Bundle bundle = new Bundle();
            bundle.putString("pingjiapath",ratepath);
            bundle.putString("pingjianame",ratename);
            intent.putExtras(bundle);
            startActivity(intent);
            About.this.finish();

          }
        });

        update.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, Aboutframe.class);
            Bundle bundle = new Bundle();
            bundle.putString("gengxinpath",updatepath);
            bundle.putString("gengxinname",updatename);
            intent.putExtras(bundle);
            startActivity(intent);
            About.this.finish();

          }
        });

        help.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v)
          {
            Intent intent = new Intent();
            intent.setClass(About.this, WapsList.class);
            startActivity(intent);
            About.this.finish();

          }
        });
        }catch (Exception e)
        { 
            e.printStackTrace();
        }
}
}

about.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
    android:gravity="center|top"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/main_background_repeat"
>

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="45dip"
    android:id="@+id/header"
    android:background="@drawable/header"
    >

<Button android:id="@+id/back" 
    android:textSize="14.0sp" 
    android:textColor="#ffe1ebef" 
    android:gravity="center"    
    android:background="@drawable/button_back" 
    android:layout_width="54.0dip" 
    android:layout_height="33.0dip" 
    android:layout_marginLeft="10.0dip" 
    android:text="@string/back" 
    android:layout_alignParentLeft="true" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" />

    <TextView 
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:textColor="#FFFFFF"
            android:textSize="18.0sp"
    android:layout_marginTop="8dip"
    android:text="@string/about"/>


<Button android:id="@+id/share" 
    android:textSize="14.0sp" 
    android:textColor="#ffe1ebef" 
    android:gravity="center"    
    android:background="@drawable/share" 
    android:layout_width="54.0dip" 
    android:layout_height="33.0dip" 
    android:layout_marginRight="10.0dip" 
    android:text="@string/share" 
    android:layout_alignParentRight="true"
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:layout_marginTop="10dip"/>

    </RelativeLayout>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/function"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
      android:orientation="vertical"
    android:gravity="center|top"
    android:layout_marginTop="20dip">

   <TextView android:id="@+id/funcdesc"
    android:layout_width="280dip"
    android:layout_height="wrap_content"
    android:text="@string/funcdesc"
    android:gravity="center" />


   <Button android:id="@+id/feedback"
     android:textColor="#6f789b"
     android:textSize="16.0sp"
     android:gravity="center"   
     android:background="@drawable/button" 
     android:layout_width="185.0dip" 
     android:layout_height="44.0dip" 
     android:layout_marginRight="10.0dip" 
     android:text="@string/feedback" 
     android:layout_alignParentRight="true"
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_marginTop="10dip"/>

   <Button android:id="@+id/rate"
     android:textColor="#6f789b"
     android:textSize="16.0sp"
     android:gravity="center"   
     android:background="@drawable/button" 
     android:layout_width="185.0dip" 
     android:layout_height="44.0dip" 
     android:layout_marginRight="10.0dip" 
     android:text="@string/rate" 
     android:layout_alignParentRight="true"
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_marginTop="10dip"/>

   <Button android:id="@+id/faq"
     android:textColor="#6f789b"
     android:textSize="16.0sp"
     android:gravity="center"   
     android:background="@drawable/button" 
     android:layout_width="185.0dip" 
     android:layout_height="44.0dip" 
     android:layout_marginRight="10.0dip" 
     android:text="@string/faq" 
     android:layout_alignParentRight="true"
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_marginTop="10dip"/>

   <Button android:id="@+id/update"
     android:textColor="#6f789b"
     android:textSize="16.0sp"
     android:gravity="center"   
     android:background="@drawable/button" 
     android:layout_width="185.0dip" 
     android:layout_height="44.0dip" 
     android:layout_marginRight="10.0dip" 
     android:text="@string/update" 
     android:layout_alignParentRight="true"
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"
     android:layout_marginTop="10dip" />

     </LinearLayout>


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/donation"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
      android:orientation="vertical"
    android:gravity="center|top"
    android:layout_marginTop="20dip">

    <TextView android:id="@+id/funcdesc"
    android:layout_width="280dip"
    android:layout_height="wrap_content"
    android:text="@string/helpdesc"
    android:gravity="center" />

     <Button android:id="@+id/child"
     android:textColor="#6f789b"
     android:textSize="16.0sp"
     android:gravity="center"   
     android:background="@drawable/button" 
     android:layout_width="185.0dip" 
     android:layout_height="44.0dip" 
     android:layout_marginRight="10.0dip" 
     android:text="@string/child" 
     android:layout_alignParentRight="true"
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"
     android:layout_marginTop="10dip" />

     </LinearLayout>


</LinearLayout>

This is my error track

12-05 13:36:27.466: WARN/System.err(308): java.lang.NullPointerException
12-05 13:36:27.476: WARN/System.err(308):     at com.appkon.hdtvs.About.onCreate(About.java:59)
12-05 13:36:27.486: WARN/System.err(308):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-05 13:36:27.506: WARN/System.err(308):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
12-05 13:36:27.506: WARN/System.err(308):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-05 13:36:27.526: WARN/System.err(308):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-05 13:36:27.537: WARN/System.err(308):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-05 13:36:27.546: WARN/System.err(308):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 13:36:27.556: WARN/System.err(308):     at android.os.Looper.loop(Looper.java:123)
12-05 13:36:27.576: WARN/System.err(308):     at android.app.ActivityThread.main(ActivityThread.java:4363)
12-05 13:36:27.586: WARN/System.err(308):     at java.lang.reflect.Method.invokeNative(Native Method)
12-05 13:36:27.596: WARN/System.err(308):     at java.lang.reflect.Method.invoke(Method.java:521)
12-05 13:36:27.596: WARN/System.err(308):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-05 13:36:27.607: WARN/System.err(308):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-05 13:36:27.616: WARN/System.err(308):     at dalvik.system.NativeStart.main(Native Method)

Your post does not have much context to explain the code sections; please explain your scenario more clearly.

1
  • do you encounter the same behavior using all the buttons or just some particular? Commented Dec 5, 2011 at 15:47

2 Answers 2

1

Looks like you're setting the contentView to R.layout.main, but the file is named about.xml. If main.xml does not contain a widget with the id back then it will return null from findViewById()

Sign up to request clarification or add additional context in comments.

Comments

1

Your button wit Id R.id.back is in about.xml whereas you inflate R.layout.main Try to use setContentView(R.layout.about); instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.