NullPointerException In Ecclipse Android Sdk Unfor

2019-07-21 05:44发布

问题:

I am a beginner. Stuck on this NullPointerException. This is a simple program of buttons. As soon as i run as android application, the project opens and immediately gets closed by displaying unfortunately my app has stopped.

04-15 17:40:05.656: D/AndroidRuntime(599): Shutting down VM
04-15 17:40:05.656: W/dalvikvm(599): threadid=1: thread exiting with uncaught exception (group=0xb2adcd58)
04-15 17:40:05.666: E/AndroidRuntime(599): FATAL EXCEPTION: main
04-15 17:40:05.666: E/AndroidRuntime(599): Process: com.example.buttona, PID: 599
04-15 17:40:05.666: E/AndroidRuntime(599): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttona/com.example.buttona.Buttonwa}: java.lang.NullPointerException
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.ActivityThread.access$800(ActivityThread.java:138)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.os.Looper.loop(Looper.java:136)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.ActivityThread.main(ActivityThread.java:5042)
04-15 17:40:05.666: E/AndroidRuntime(599):  at java.lang.reflect.Method.invokeNative(Native Method)
04-15 17:40:05.666: E/AndroidRuntime(599):  at java.lang.reflect.Method.invoke(Method.java:515)
04-15 17:40:05.666: E/AndroidRuntime(599):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
04-15 17:40:05.666: E/AndroidRuntime(599):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-15 17:40:05.666: E/AndroidRuntime(599):  at dalvik.system.NativeStart.main(Native Method)
04-15 17:40:05.666: E/AndroidRuntime(599): Caused by: java.lang.NullPointerException
04-15 17:40:05.666: E/AndroidRuntime(599):  at com.example.buttona.Buttonwa.onCreate(Buttonwa.java:29)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.Activity.performCreate(Activity.java:5231)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 17:40:05.666: E/AndroidRuntime(599):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
04-15 17:40:05.666: E/AndroidRuntime(599):  ... 11 more
04-15 17:40:11.466: I/Process(599): Sending signal. PID: 599 SIG: 9
04-15 17:57:32.017: D/AndroidRuntime(662): Shutting down VM
04-15 17:57:32.017: W/dalvikvm(662): threadid=1: thread exiting with uncaught exception (group=0xb2adcd58)
04-15 17:57:32.027: E/AndroidRuntime(662): FATAL EXCEPTION: main
04-15 17:57:32.027: E/AndroidRuntime(662): Process: com.example.buttona, PID: 662
04-15 17:57:32.027: E/AndroidRuntime(662): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttona/com.example.buttona.Buttonwa}: java.lang.NullPointerException
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.ActivityThread.access$800(ActivityThread.java:138)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.os.Handler.dispatchMessage(Handler.java:102)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.os.Looper.loop(Looper.java:136)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.ActivityThread.main(ActivityThread.java:5042)
04-15 17:57:32.027: E/AndroidRuntime(662):  at java.lang.reflect.Method.invokeNative(Native Method)
04-15 17:57:32.027: E/AndroidRuntime(662):  at java.lang.reflect.Method.invoke(Method.java:515)
04-15 17:57:32.027: E/AndroidRuntime(662):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
04-15 17:57:32.027: E/AndroidRuntime(662):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-15 17:57:32.027: E/AndroidRuntime(662):  at dalvik.system.NativeStart.main(Native Method)
04-15 17:57:32.027: E/AndroidRuntime(662): Caused by: java.lang.NullPointerException
04-15 17:57:32.027: E/AndroidRuntime(662):  at com.example.buttona.Buttonwa.onCreate(Buttonwa.java:29)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.Activity.performCreate(Activity.java:5231)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-15 17:57:32.027: E/AndroidRuntime(662):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
04-15 17:57:32.027: E/AndroidRuntime(662):  ... 11 more
04-15 17:57:34.957: I/Process(662): Sending signal. PID: 662 SIG: 9

This Is The Java File.

public class Buttonwa extends Activity {

Button name, surname;
TextView display;

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

    name = (Button) findViewById(R.id.name_button);
    surname = (Button) findViewById(R.id.surname_button);
    display = (TextView) findViewById(R.id.display);
    name.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            display.setText("Your Name Is Shashank Yadav");

        }
    });

    surname.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            display.setText("Your Name Is Yadav Shashank");

        }
    });

this is the 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.buttona.Buttonwa$PlaceholderFragment" >

<TextView
    android:id="@+id/display"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
     />
<Button 
android:id="@+id/name_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/Name"
/>

<Button 
android:id="@+id/surname_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/name_button"
android:text="@string/Surname"
/>


</RelativeLayout>

回答1:

The problem is you have created View elements in your fragment fragment_main.xml (I assume that's what it's called, you haven't posted the name) but your code expects them in your activity.

When you do this:

setContentView(R.layout.activity_buttonwa);

name = (Button) findViewById(R.id.name_button);
surname = (Button) findViewById(R.id.surname_button);
display = (TextView) findViewById(R.id.display);

You are saying, "Hey activity, check activity_buttonwa.xml for all these view elements, you'll find them there.

Judging from your error message, name is null and therefore you are getting a NullPointerException when you try and set an OnClickListener on it. It's null because it's not in your activity_buttonwa.xml file.

To fix, you can either move all that xml into activity_buttonwa.xml or you can move the inflation code into your fragment:

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        name = (Button) rootView.findViewById(R.id.name_button);
        surname = (Button) rootView.findViewById(R.id.surname_button);
        display = (TextView) rootView.findViewById(R.id.display);

        // Add OnClickListeners, etc.

        return rootView;
    }
}

I would say it's better to move the inflation code to your fragment, as above.