不幸的是应用程序停止了android模拟器genymotion

时间:2014-03-29 21:47:02

标签: java android

我是Android应用程序开发的新手(从昨天起我才开始学习android)。

我在AVD中收到消息 - 不幸的是已经停止了,请帮我解决这个错误,我希望在android中学到更多但是被这个错误所吸引.. plz..plz ..帮助我来制作这个。

我的main.java是

package com.example.first;

public class MainActivity extends Activity {


int counter = 0 ;
Button add,sub;
TextView total;
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    add = (Button) findViewById(R.id.bt_add);
    sub = (Button) findViewById(R.id.bt_sub);
    total = (TextView) findViewById(R.id.textView1);


    add.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            counter++;
            total.setText("ur total is "+ counter);

        }
    });



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * 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);
        return rootView;
    }
}

}

我的main.xml是

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="ur total is 0"
    android:textSize="40sp" />

    <Button
        android:id="@+id/bt_add"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="add one" />

    <Button
        android:id="@+id/bt_sub"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/bt_add"
        android:layout_below="@+id/bt_add"
        android:layout_marginTop="19dp"
        android:text="sub one" />

,日志是

        03-29 21:33:13.494: D/AndroidRuntime(1256): Shutting down VM
03-29 21:33:13.494: W/dalvikvm(1256): threadid=1: thread exiting with uncaught exception (group=0xa4d6cb20)
03-29 21:33:13.494: E/AndroidRuntime(1256): FATAL EXCEPTION: main
03-29 21:33:13.494: E/AndroidRuntime(1256): Process: com.example.first, PID: 1256
03-29 21:33:13.494: E/AndroidRuntime(1256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first/com.example.first.MainActivity}: java.lang.NullPointerException
03-29 21:33:13.494: E/AndroidRuntime(1256):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at android.os.Looper.loop(Looper.java:136)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at java.lang.reflect.Method.invoke(Method.java:515)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at dalvik.system.NativeStart.main(Native Method)
03-29 21:33:13.494: E/AndroidRuntime(1256): Caused by: java.lang.NullPointerException
03-29 21:33:13.494: E/AndroidRuntime(1256):     at com.example.first.MainActivity.onCreate(MainActivity.java:39)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at android.app.Activity.performCreate(Activity.java:5231)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-29 21:33:13.494: E/AndroidRuntime(1256):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-29 21:33:13.494: E/AndroidRuntime(1256):     ... 11 more
03-29 21:34:01.629: D/dalvikvm(1310): Late-enabling CheckJNI
03-29 21:34:01.673: D/AndroidRuntime(1310): Shutting down VM
03-29 21:34:01.673: W/dalvikvm(1310): threadid=1: thread exiting with uncaught exception (group=0xa4d6cb20)
03-29 21:34:01.673: E/AndroidRuntime(1310): FATAL EXCEPTION: main
03-29 21:34:01.673: E/AndroidRuntime(1310): Process: com.example.first, PID: 1310
03-29 21:34:01.673: E/AndroidRuntime(1310): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first/com.example.first.MainActivity}: java.lang.NullPointerException
03-29 21:34:01.673: E/AndroidRuntime(1310):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at android.os.Looper.loop(Looper.java:136)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at java.lang.reflect.Method.invoke(Method.java:515)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at dalvik.system.NativeStart.main(Native Method)
03-29 21:34:01.673: E/AndroidRuntime(1310): Caused by: java.lang.NullPointerException
03-29 21:34:01.673: E/AndroidRuntime(1310):     at com.example.first.MainActivity.onCreate(MainActivity.java:38)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at android.app.Activity.performCreate(Activity.java:5231)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-29 21:34:01.673: E/AndroidRuntime(1310):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-29 21:34:01.673: E/AndroidRuntime(1310):     ... 11 more
03-29 21:36:56.615: D/AndroidRuntime(1358): Shutting down VM
03-29 21:36:56.615: W/dalvikvm(1358): threadid=1: thread exiting with uncaught exception (group=0xa4d6cb20)
03-29 21:36:56.615: E/AndroidRuntime(1358): FATAL EXCEPTION: main
03-29 21:36:56.615: E/AndroidRuntime(1358): Process: com.example.first, PID: 1358
03-29 21:36:56.615: E/AndroidRuntime(1358): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first/com.example.first.MainActivity}: java.lang.NullPointerException
03-29 21:36:56.615: E/AndroidRuntime(1358):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at android.os.Looper.loop(Looper.java:136)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at java.lang.reflect.Method.invoke(Method.java:515)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at dalvik.system.NativeStart.main(Native Method)
03-29 21:36:56.615: E/AndroidRuntime(1358): Caused by: java.lang.NullPointerException
03-29 21:36:56.615: E/AndroidRuntime(1358):     at com.example.first.MainActivity.onCreate(MainActivity.java:38)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at android.app.Activity.performCreate(Activity.java:5231)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-29 21:36:56.615: E/AndroidRuntime(1358):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-29 21:36:56.615: E/AndroidRuntime(1358):     ... 11 more
03-29 21:48:16.663: D/AndroidRuntime(1395): Shutting down VM
03-29 21:48:16.663: W/dalvikvm(1395): threadid=1: thread exiting with uncaught exception (group=0xa4d6cb20)
03-29 21:48:16.663: E/AndroidRuntime(1395): FATAL EXCEPTION: main
03-29 21:48:16.663: E/AndroidRuntime(1395): Process: com.example.first, PID: 1395
03-29 21:48:16.663: E/AndroidRuntime(1395): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first/com.example.first.MainActivity}: java.lang.NullPointerException
03-29 21:48:16.663: E/AndroidRuntime(1395):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at android.os.Looper.loop(Looper.java:136)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at java.lang.reflect.Method.invoke(Method.java:515)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at dalvik.system.NativeStart.main(Native Method)
03-29 21:48:16.663: E/AndroidRuntime(1395): Caused by: java.lang.NullPointerException
03-29 21:48:16.663: E/AndroidRuntime(1395):     at com.example.first.MainActivity.onCreate(MainActivity.java:38)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at android.app.Activity.performCreate(Activity.java:5231)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-29 21:48:16.663: E/AndroidRuntime(1395):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-29 21:48:16.663: E/AndroidRuntime(1395):     ... 11 more
03-29 22:09:36.395: D/AndroidRuntime(1448): Shutting down VM
03-29 22:09:36.395: W/dalvikvm(1448): threadid=1: thread exiting with uncaught exception (group=0xa4d6cb20)
03-29 22:09:36.395: E/AndroidRuntime(1448): FATAL EXCEPTION: main
03-29 22:09:36.395: E/AndroidRuntime(1448): Process: com.example.first, PID: 1448
03-29 22:09:36.395: E/AndroidRuntime(1448): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.first/com.example.first.MainActivity}: java.lang.NullPointerException
03-29 22:09:36.395: E/AndroidRuntime(1448):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at android.os.Looper.loop(Looper.java:136)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at java.lang.reflect.Method.invoke(Method.java:515)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at dalvik.system.NativeStart.main(Native Method)
03-29 22:09:36.395: E/AndroidRuntime(1448): Caused by: java.lang.NullPointerException
03-29 22:09:36.395: E/AndroidRuntime(1448):     at com.example.first.MainActivity.onCreate(MainActivity.java:38)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at android.app.Activity.performCreate(Activity.java:5231)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-29 22:09:36.395: E/AndroidRuntime(1448):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-29 22:09:36.395: E/AndroidRuntime(1448):     ... 11 more

plz ..解释为什么会这样..告诉我解决方案

4 个答案:

答案 0 :(得分:0)

您似乎忘记在清单中声明活动。将其添加到AndroidManifest.xml:

<activity android:label="AnotherActivity" android:name="com.somayaji.abhi.Second" />

答案 1 :(得分:0)

该日志显示您尚未在android manefist.xml文件中注册您的活动。每个活动必须在android清单文件中注册才能工作。 (引自developers.android.com)

<activity
android:name="com.example.facebook.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

答案 2 :(得分:0)

你在第38行获得了NPE:

add.setOnClickListener(new View.OnClickListener()

所以add为空,因为您将按钮声明为:

add = (Button) findViewById(R.id.bt_add);

因此bt_add上的activity_main ID不存在,我认为您发布的这个xml是fragment_main.xml或其他任何内容。

中将主要布局定义为activity_main
setContentView(R.layout.activity_main);

因此您必须在activity_main

中拥有以下ID

1- bt_add

2- bt_sub

3- textView1

因此您必须拥有以下代码activity_main

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="ur total is 0"
    android:textSize="40sp" />

    <Button
        android:id="@+id/bt_add"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="add one" />

    <Button
        android:id="@+id/bt_sub"
        android:layout_width="250dp"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/bt_add"
        android:layout_below="@+id/bt_add"
        android:layout_marginTop="19dp"
        android:text="sub one" />

答案 3 :(得分:0)

从8/26/14开始,似乎有更简单的方法来解决这个问题(如果我错了,请纠正我)但你要做的就是在main.java中更改 的setContentView(R.layout.activity_main);至 的setContentView(R.layout.fragment_main); 只有更新的原因我有同样的问题。