-1

The UI portion alone worked fine, but later when I wrote the OnClickListener and ran the code I got an error stating "Unfortunately app stopped working".

I re-tried with a new project again I am getting these error. I have attached my XML, MainActivity class and logs as well. Kindly help me out. I have been stuck with this simple program for a whole week. :(

Log:

 05-18 01:54:31.015: D/Android :(1436): The onCreate() event
    05-18 01:54:31.015: D/AndroidRuntime(1436): Shutting down VM
    05-18 01:54:31.015: W/dalvikvm(1436): threadid=1: thread exiting with uncaught  exception (group=0xb2afaba8)
    05-18 01:54:31.055: E/AndroidRuntime(1436): FATAL EXCEPTION: main
    05-18 01:54:31.055: E/AndroidRuntime(1436): Process: com.example.test3, PID: 1436
    05-18 01:54:31.055: E/AndroidRuntime(1436): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test3/com.example.test3.MainActivity}: java.lang.NullPointerException
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.os.Handler.dispatchMessage(Handler.java:102)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.os.Looper.loop(Looper.java:136)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at java.lang.reflect.Method.invoke(Method.java:515)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at dalvik.system.NativeStart.main(Native Method)
    05-18 01:54:31.055: E/AndroidRuntime(1436): Caused by: java.lang.NullPointerException
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at com.example.test3.MainActivity.onCreate(MainActivity.java:25)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.Activity.performCreate(Activity.java:5231)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    05-18 01:54:31.055: E/AndroidRuntime(1436):     ... 11 more




---

I am not sure if anything is wrong with my MainActivity.java.I am attaching my XMl file and MainActivity.java here for getting a clear picture.

MainActivity.java:

package com.example.test3;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
       String msg = "Android : ";
       int counter;
       Button add,sub;
       TextView display;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Log.d(msg, "The onCreate() event");
          counter=0;
          add=(Button) findViewById(R.id.addButton);
          sub=(Button) findViewById(R.id.subButton);
          display=(TextView) findViewById(R.id.disp);
          add.setOnClickListener(new View.OnClickListener() {

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

            }
        });

        sub.setOnClickListener(new View.OnClickListener() {

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

       }


    }

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.test3.MainActivity$PlaceholderFragment" >

    <Button
        android:id="@+id/subButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/disp"
        android:layout_toRightOf="@+id/addButton"
        android:text="Sub 1" />

    <Button
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="58dp"
        android:layout_marginTop="14dp"
        android:text="Add 1" />

    <TextView
        android:id="@+id/disp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/addButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:text="Count is 0"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>
Steve
  • 9,270
  • 5
  • 47
  • 61
Ragesh Kr
  • 1,573
  • 8
  • 29
  • 46

1 Answers1

2

The likely cause (guessing) views belong to Fragment Layout. So should initialize views in fragment onCreateView as below.

The other thing i notice is you do counter++ on both button click. It should probably be count-- on sub button click

@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.Add);
      sub=(Button) rootView.findViewById(R.id.Sub);
      disp=(TextView) rootView.findViewById(R.id.Display);
      add.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        counter++;
        disp.setText("counter is "+counter);

     }
     });

    sub.setOnClickListener(new View.OnClickListener() {

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

        counter++; 
        disp.setText("counter is "+counter);

    }
   });

    return rootView;
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256