简体   繁体   中英

Android null pointer error with button

I am having trouble trying to make a simple app that is a portfolio. Depending on what button you click I want to load the next activity with the content filled in and I want the color of this little subheader bar to change to the color of the button on the main page. I tried to achieve this with a variable called webColor, to see if I can get it to work with 1 button first before I make it a variable for all of them. The web link is a color code that the variable should change to, it looks like it should work but then when I run it I get the following.

   05-18 23:07:08.573    1554-1554/com.dredaydesigns.dredaycreativeportfolio     D/dalvikvm﹕ Late-enabling CheckJNI
    05-18 23:07:08.917    1554-1554/com.dredaydesigns.dredaycreativeportfolio     I/dalvikvm﹕ Could not find method     android.content.res.TypedArray.getChangingConfigurations, referenced from method         android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
    05-18 23:07:08.917    1554-1554/com.dredaydesigns.dredaycreativeportfolio     W/dalvikvm﹕ VFY: unable to resolve virtual method 406:     Landroid/content/res/TypedArray;.getChangingConfigurations ()I
    05-18 23:07:08.953    1554-1554/com.dredaydesigns.dredaycreativeportfolio     D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002    
    05-18 23:07:08.953    1554-1554/com.dredaydesigns.dredaycreativeportfolio     I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType,     referenced from method android.support.v7.internal.widget.TintTypedArray.getType
    05-18 23:07:08.953    1554-1554/com.dredaydesigns.dredaycreativeportfolio      W/dalvikvm﹕ VFY: unable to resolve virtual method 428: Landroid/content/res/TypedArray;.getType (I)I
05-18 23:07:08.953    1554-1554/com.dredaydesigns.dredaycreativeportfolio   D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
     05-18 23:07:08.969    1554-1554/com.dredaydesigns.dredaycreativeportfolio D/AndroidRuntime﹕ Shutting down VM
     05-18 23:07:08.969    1554-1554/com.dredaydesigns.dredaycreativeportfolio W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4ce1b20)
05-18 23:07:08.969    1554-1554/com.dredaydesigns.dredaycreativeportfolio E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.dredaydesigns.dredaycreativeportfolio, PID: 1554
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dredaydesigns.dredaycreativeportfolio/com.dredaydesigns.dredaycreativeportfolio.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.dredaydesigns.dredaycreativeportfolio.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)








package com.dredaydesigns.dredaycreativeportfolio;
import android.R;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
String webColor;
Button webButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_item);

    webButton = (Button) findViewById(R.id.button1);

    webButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String webColor = webButton.getText().toString();
            Intent goToNext = new     Intent(MainActivity.this,content_activity.class);
            goToNext.putExtra(webColor,"#C41E72");
            startActivity(goToNext);

        }
    });


}

Take a look at your codes specifically as explained by the error codes:

 Caused by: java.lang.NullPointerException
        at com.dredaydesigns.dredaycreativeportfolio.MainActivity.onCreate(MainActivity.java:22)

If you examine it closely, you will see the error is on MainActivity.java line 22 . What is on that line? Check if it is already initialised.

public class MainActivity extends ActionBarActivity {
String webColor;
Button webButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_item);

    webButton = (Button) findViewById(R.id.button1);

    webButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String webColor = webButton.getText().toString();
            Intent goToNext = new     Intent(MainActivity.this,content_activity.class);
            goToNext.putExtra(webColor,"#C41E72");
            startActivity(goToNext);

        }
    });


}

to replace this.

public class MainActivity extends **Activity** {
String webColor;
Button webButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_item);

    webButton = (Button) findViewById(R.id.button1);
    webButton.setOnClickListener(new OnClickListener() {
        @Override
        **public void onClick(View v) {
            webColor= webButton.getText().toString();
            Intent goToNext = new         Intent(MainActivity.this,SecondClassName.class);
            goToNext.putExtra(webColor,"#C41E72");
            startActivity(goToNext);
        }
    });


}

and Sure about you enter activity details in manifest ?

setContentView(R.layout.activity_list_item);

webButton = (Button) findViewById(R.id.button1);

webButton.setOnClickListener

I think, there is no button which its id = button1 in layout file "activity_list_item", please make sure you are using correct layout file. Line 22 cause NullPointerException may be "webButton.setOnClickListener", because activity can not find webButton here.

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