-1

This is the first time I am creating a new android project. I didn't make any changes. I received this error in the emulator when trying to run the "HelloWorld" project.

I've pinpointed the error to be while setting the onclicklisteners.

public class MainActivity extends ActionBarActivity {

    int counter;
    Button add,sub;
    TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        counter = 0;
        add = (Button)this.findViewById(R.id.bAdd);
        sub = (Button)this.findViewById(R.id.bSub);
        display = (TextView)this.findViewById(R.id.display);

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter = counter+1;
                display.setText("Your number is "+counter);
            }
        });

        if (savedInstanceState == null) {
                    getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                        .commit();
        }
    }
}

Here is the log.

05-23 02:50:51.350: D/AndroidRuntime(2443): Shutting down VM
05-23 02:50:51.350: W/dalvikvm(2443): threadid=1: thread exiting with uncaught exception (group=0xb2aafba8)
05-23 02:50:51.380: E/AndroidRuntime(2443): FATAL EXCEPTION: main
05-23 02:50:51.380: E/AndroidRuntime(2443): Process: com.example.helloworld, PID: 2443
05-23 02:50:51.380: E/AndroidRuntime(2443): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloworld/com.example.helloworld.MainActivity}: java.lang.NullPointerException
05-23 02:50:51.380: E/AndroidRuntime(2443):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at android.os.Looper.loop(Looper.java:136)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at java.lang.reflect.Method.invoke(Method.java:515)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at dalvik.system.NativeStart.main(Native Method)
05-23 02:50:51.380: E/AndroidRuntime(2443): Caused by: java.lang.NullPointerException
05-23 02:50:51.380: E/AndroidRuntime(2443):     at com.example.helloworld.MainActivity.onCreate(MainActivity.java:31)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at android.app.Activity.performCreate(Activity.java:5231)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-23 02:50:51.380: E/AndroidRuntime(2443):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-23 02:50:51.380: E/AndroidRuntime(2443):     ... 11 more
05-23 02:50:55.820: I/Process(2443): Sending signal. PID: 2443 SIG: 9

Where is the problem? What should I do now?

Pararth
  • 8,114
  • 4
  • 34
  • 51
Aiyoyo
  • 318
  • 4
  • 18

3 Answers3

0

You've got a reference to something in the line 31 of the MainActivity.java class.

Check the log you posted:

05-23 02:50:51.380: E/AndroidRuntime(2443): Caused by: java.lang.NullPointerException
05-23 02:50:51.380: E/AndroidRuntime(2443):     at com.example.helloworld.MainActivity.onCreate(MainActivity.java:31)

Seems to be a UI element that you don't have on your xml file. Check it out.

reixa
  • 6,903
  • 6
  • 49
  • 68
0

You have to write the code in OnCreateView, like following one

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        counter = 0;
        add = (Button)rootView.findViewById(R.id.bAdd);
        sub = (Button)rootView.findViewById(R.id.bSub);
        display = (TextView)rootView.findViewById(R.id.display);

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter = counter+1;
                display.setText("Your number is "+counter);
            }
        });
        return rootView;
    }
Ramki Anba
  • 754
  • 1
  • 10
  • 21
0

What is on the line 31 ?

Also try extending Activity instead of ActionBarActivity

hfatih
  • 104
  • 4
  • Super class of ActionBarActivity is Activity then it doesn't have any connection with null pointer exception. One reason could be there controls are not present in the xml from where he is getting. – Hulk May 23 '14 at 07:02