К сожалению остановке приложения для Android эмулятор genymotion

? Omar Hassan @ | Original: StackOverFlow
---

Я новичок в андроид разработки приложений ( со вчерашнего дня только я начал изучать Android).

Я получаю сообщение AVD нравится - к сожалению, остановилась, пожалуйста, помогите мне решить эту ошибку, я хочу узнать больше в Android, но всасывается при этой ошибке .. plz..plz .. помогите мне sove это .

мой 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;
    }
}

}

my main.xml is

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

and the log is

        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

плз .. объяснить, почему я получаю так .. скажите мне решение

---

Top 5 ответ

1Noob @

Похоже, вы забыли объявить активность в манифесте. Добавьте к этому ваши AndroidManifest.xml :

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

Это бревно показывает, что вы еще не зарегистрированы вашу деятельность в андроид файла manefist.xml . Каждый вид деятельности должен быть зарегистрирован в андроид файле манифеста для того, чтобы работать. ( Цитата из 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>
3shayan pourvatan @

Вы получили NPE в строке 38, которая :

add.setOnClickListener(new View.OnClickListener()

так add является недействительным, и потому, что вы заявляете свою кнопку, как:

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

так bt_add ID не существует на activity_main, и я думаю, что это XML, что вы пост fragment_main.xml или что-нибудь еще.

как вы определяете свой ​​основной макет activity_main в

setContentView(R.layout.activity_main);

так что вы должны быть следующие записи в activity_main

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" />
4Bluel3ull @

Там, кажется,намного проще способ решить эту проблему по состоянию на 8/26/14 ( поправьте меня, если им неправильно ), но все, что вам нужно сделать, это в изменении main.java setContentView ( R.layout.activity_main ) ; для setContentView ( R.layout.fragment_main ) ; обновляется только потому что я была такая же проблема Тай.