我的应用程序在模拟器中不断崩溃

时间:2014-02-09 15:07:05

标签: android eclipse

我正在开发一个简单的应用程序,每次我在模拟器中运行它时都会说“不幸的是,计数器已停止”。该应用程序打开一瞬间,然后屏幕变黑,表示已停止。它并没有说日食中有任何错误。这是我的代码:(主要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;
}

}

我的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>

我的清单:

<?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>

这是我的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

我的logcat遵循kelmer的建议:

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 个答案:

答案 0 :(得分:0)

您已在清单文件中定义了基础包:

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

在宣布您的活动时,请尝试使用:

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

而不是:

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

答案 1 :(得分:0)

在将onClickListener添加到重置按钮之前,您似乎会关闭onCreate()方法,并且已将reset.addOnClickListener代码括在一些单独的括号中。

当你在顶部声明重置为全局变量时,你得到一个Null指针,但是你也在全局分配onClickListener。由于在加载类时(即在调用onCreate之前)将调用此代码,因此尚未分配重置,因此为空。

编辑:简单地说,你需要从第44和45行删除括号。

答案 2 :(得分:0)

转到File -> Settings -> Appearance and Behavior -> System Settings -> Android SDK -> SDK Tools

禁用或卸载插件:

用于AMD处理器的Android Emulator Hypervisor驱动程序(安装程序)

您的问题应该得到解决。