0

I am working on a simple app and every time I run it in the emulator it says "Unfortunately, Counter has stopped". The app opens for a split second and then the screen turns black and it says it has stopped. It does not say there are any errors in eclipse. Here is my code: (main java)

package martin.productions.counter;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class Countermain extends Activity {
int counter;
Button add;
Button subtract;
TextView display;
Button reset;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_countermain);
    counter = 0;
    add = (Button) findViewById(R.id.bADD);
    subtract = (Button) findViewById(R.id.bSUBTRACT);
    display = (TextView) findViewById(R.id.textviewdisplay);
    reset = (Button) findViewById(R.id.bRESET);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
            display.setText("Your total is " + counter);
        }
    });
    subtract.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
            display.setText("Your total is " + counter);
        }
    });
}
{
    reset.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter = 0;
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.countermain, menu);
    return true;
}

}

My code for XML:

<LinearLayout 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=".MainActivity"
android:orientation="vertical" 
android:background="@drawable/whitebackground"
android:icon="@drawable/ic_launcher">

<StackView
android:id="@+id/stackView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</StackView>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your total is 0" 
android:textSize="45sp"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/textviewdisplay"
/>

<Button
android:id="@+id/bADD"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Add 1" 
android:textSize="25sp" />


<Button 
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract 1"
android:layout_gravity="center"
android:textSize="25sp"
android:id="@+id/bSUBTRACT"/>

<Button
android:id="@+id/bRESET"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Reset"
android:textSize="25sp"/>

</LinearLayout>

And my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="martin.productions.counter"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="martin.productions.counter.Countermain"
android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Here is my logcat:

02-09 11:15:15.400: D/AndroidRuntime(946): Shutting down VM
02-09 11:15:15.400: W/dalvikvm(946): threadid=1: thread exiting with uncaught exception          (group=0xb1af6ba8)
02-09 11:15:15.460: E/AndroidRuntime(946): FATAL EXCEPTION: main
02-09 11:15:15.460: E/AndroidRuntime(946): Process: martin.productions.counter, PID: 946
02-09 11:15:15.460: E/AndroidRuntime(946): java.lang.RuntimeException: Unable to     instantiate activity      ComponentInfo{martin.productions.counter/martin.productions.counter.Counter}:     java.lang.ClassNotFoundException: Didn't find class "martin.productions.counter.Counter" on   path: DexPathList[[zip file "/data/app/martin.productions.counter-  2.apk"],nativeLibraryDirectories=[/data/app-lib/martin.productions.counter-2, /system/lib]]
02-09 11:15:15.460: E/AndroidRuntime(946):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-09 11:15:15.460: E/AndroidRuntime(946):  at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-09 11:15:15.460: E/AndroidRuntime(946):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-09 11:15:15.460: E/AndroidRuntime(946):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-09 11:15:15.460: E/AndroidRuntime(946):  at android.os.Handler.dispatchMessage(Handler.java:102)
02-09 11:15:15.460: E/AndroidRuntime(946):  at android.os.Looper.loop(Looper.java:136)
02-09 11:15:15.460: E/AndroidRuntime(946):  at android.app.ActivityThread.main(ActivityThread.java:5017)
02-09 11:15:15.460: E/AndroidRuntime(946):  at java.lang.reflect.Method.invokeNative(Native Method)
02-09 11:15:15.460: E/AndroidRuntime(946):  at java.lang.reflect.Method.invoke(Method.java:515)
02-09 11:15:15.460: E/AndroidRuntime(946):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-09 11:15:15.460: E/AndroidRuntime(946):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-09 11:15:15.460: E/AndroidRuntime(946):  at dalvik.system.NativeStart.main(Native Method)
02-09 11:15:15.460: E/AndroidRuntime(946): Caused by: java.lang.ClassNotFoundException: Didn't find class "martin.productions.counter.Counter" on path: DexPathList[[zip file   "/data/app/martin.productions.counter-2.apk"],nativeLibraryDirectories=[/data/app- lib/martin.productions.counter-2, /system/lib]]
02-09 11:15:15.460: E/AndroidRuntime(946):  at   dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
02-09 11:15:15.460: E/AndroidRuntime(946):  at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
02-09 11:15:15.460: E/AndroidRuntime(946):  at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
02-09 11:15:15.460: E/AndroidRuntime(946):  at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-09 11:15:15.460: E/AndroidRuntime(946):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-09 11:15:15.460: E/AndroidRuntime(946):  ... 11 more
02-09 11:15:19.870: I/Process(946): Sending signal. PID: 946 SIG: 9

And my logcat after kelmer's advice:

02-09 11:43:50.500: D/AndroidRuntime(874): Shutting down VM
02-09 11:43:50.500: W/dalvikvm(874): threadid=1: thread exiting with uncaught exception (group=0xb1a6fba8)
02-09 11:43:50.510: E/AndroidRuntime(874): FATAL EXCEPTION: main
02-09 11:43:50.510: E/AndroidRuntime(874): Process: martin.productions.counter, PID: 874
02-09 11:43:50.510: E/AndroidRuntime(874): java.lang.RuntimeException: Unable to instantiate activity    ComponentInfo{martin.productions.counter/martin.productions.counter.Countermain}: java.lang.NullPointerException
02-09 11:43:50.510: E/AndroidRuntime(874):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-09 11:43:50.510: E/AndroidRuntime(874):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-09 11:43:50.510: E/AndroidRuntime(874):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-09 11:43:50.510: E/AndroidRuntime(874):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-09 11:43:50.510: E/AndroidRuntime(874):  at android.os.Handler.dispatchMessage(Handler.java:102)
02-09 11:43:50.510: E/AndroidRuntime(874):  at android.os.Looper.loop(Looper.java:136)
02-09 11:43:50.510: E/AndroidRuntime(874):  at android.app.ActivityThread.main(ActivityThread.java:5017)
02-09 11:43:50.510: E/AndroidRuntime(874):  at java.lang.reflect.Method.invokeNative(Native Method)
02-09 11:43:50.510: E/AndroidRuntime(874):  at java.lang.reflect.Method.invoke(Method.java:515)
02-09 11:43:50.510: E/AndroidRuntime(874):  at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-09 11:43:50.510: E/AndroidRuntime(874):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-09 11:43:50.510: E/AndroidRuntime(874):  at dalvik.system.NativeStart.main(Native Method)
02-09 11:43:50.510: E/AndroidRuntime(874): Caused by: java.lang.NullPointerException
02-09 11:43:50.510: E/AndroidRuntime(874):  at martin.productions.counter.Countermain.<init>(Countermain.java:46)
02-09 11:43:50.510: E/AndroidRuntime(874):  at java.lang.Class.newInstanceImpl(Native Method)
02-09 11:43:50.510: E/AndroidRuntime(874):  at java.lang.Class.newInstance(Class.java:1208)
02-09 11:43:50.510: E/AndroidRuntime(874):  at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-09 11:43:50.510: E/AndroidRuntime(874):  at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-09 11:43:50.510: E/AndroidRuntime(874):  ... 11 more
02-09 11:43:55.130: I/Process(874): Sending signal. PID: 874 SIG: 9

3 Answers3

0

You have defined your base package in the manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="martin.productions.counter"
android:versionCode="1"
android:versionName="1.0" >

When declaring your activity, try using:

<activity
android:name=".Countermain"
android:label="@string/app_name" >

instead of:

<activity
android:name="martin.productions.counter.Countermain"
android:label="@string/app_name" >
M Rajoy
  • 4,028
  • 14
  • 54
  • 111
  • are you getting the same exception after this change? the line `ComponentInfo{martin.productions.counter/martin.productions.counter.Counter` is saying that this is the problem you're facing – M Rajoy Feb 09 '14 at 15:39
  • No, I am getting a different logcat now. I will put up the new one. – user3289794 Feb 09 '14 at 15:46
  • you either posted the wrong code for your class or your brackets don't match near `reset.setOnClickListener(new View.OnClickListener()`. Before this line there's an extra }. Please post the correct code. – M Rajoy Feb 09 '14 at 15:55
0

It would appear you close your onCreate() method just before you add the onClickListener to your reset button, and you have enclosed the reset.addOnClickListener code in some separate brackets.

You are getting a Null Pointer as you declare reset as a global variable at the top, but then you are assigning the onClickListener globally too. As this code will be called as soon as the class is loaded (i.e. before onCreate is called), reset has not yet been assigned, and therefore is null.

EDIT: To put it simply, you need to remove the brackets from line 44 and 45.

TomRichardson
  • 5,933
  • 5
  • 27
  • 30
-1

Go to File -> Settings -> Appearance and Behavior -> System Settings -> Android SDK -> SDK Tools

Disable or Uninstall the Plugin :

Android Emulator Hypervisor Driver for AMD Processors (installer)

Your issue should be fixed.

Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139
Faran
  • 59
  • 1
  • 6