Android“不幸的是,<app>已停止。”</app>

时间:2013-12-18 19:43:38

标签: java android eclipse

我认为App停止了setContentView(R.layout.main);TextState = (EditText) findViewById(R.id.TextState);以及更多,但我不知道需要做什么。对不起,但请帮助我。

\res\layout-land\main.xml: - 需要一个水平界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:orientation="vertical" >

<Button
android:id="@+id/Down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="22dp"
android:layout_marginLeft="22dp"
android:text="Button" />

<Button
android:id="@+id/Up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Down"
android:layout_alignParentLeft="true"
android:layout_marginLeft="22dp"
android:text="Button" />

<Button
android:id="@+id/Left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/Down"
android:layout_alignParentRight="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="111dp"
android:text="Button" />

<Button
android:id="@+id/Right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Left"
android:layout_alignBottom="@+id/Left"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:text="Button" />

<TextView
    android:id="@+id/TextState"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/Up"
    android:layout_centerHorizontal="true"
    android:text="Состояние"
    android:textSize="32sp" />

</RelativeLayout>

MainActivity:

public class MainActivity extends Activity implements OnClickListener {
EditText TextState;
Button Up;
Button Down;
Button Left;
Button Right;
@Override
    public void onCreate(Bundle savedInstanceState) {

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

    TextState = (EditText) findViewById(R.id.TextState);

    Up = (Button) findViewById(R.id.Up);
    Down = (Button) findViewById(R.id.Down);
    Left = (Button) findViewById(R.id.Left);
    Right = (Button) findViewById(R.id.Right);

    Up.setOnClickListener(this);
    Down.setOnClickListener(this);
    Right.setOnClickListener(this);
    Left.setOnClickListener(this);
}
    @Override
    public void onClick(View v) {
         switch (v.getId()) {
            case R.id.Up:

              // TODO:
                TextState.setText("Едем вперед");

              break;
            case R.id.Down:

                  // TODO:
                TextState.setText("Едем назад");

              break;
            case R.id.Right:

                  // TODO:
                TextState.setText("Едем направо");

              break;
            case R.id.Left:

                  // TODO:
                TextState.setText("Едем налево");

              break;
            default:
              break;
    }

    }}

目录下载:

12-18 20:25:44.299: D/AndroidRuntime(647): Shutting down VM
12-18 20:25:44.299: W/dalvikvm(647): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-18 20:25:44.319: E/AndroidRuntime(647): FATAL EXCEPTION: main
12-18 20:25:44.319: E/AndroidRuntime(647): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.NullPointerException
12-18 20:25:44.319: E/AndroidRuntime(647):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-18 20:25:44.319: E/AndroidRuntime(647):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-18 20:25:44.319: E/AndroidRuntime(647):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-18 20:25:44.319: E/AndroidRuntime(647):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-18 20:25:44.319: E/AndroidRuntime(647):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 20:25:44.319: E/AndroidRuntime(647):  at android.os.Looper.loop(Looper.java:137)
12-18 20:25:44.319: E/AndroidRuntime(647):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-18 20:25:44.319: E/AndroidRuntime(647):  at java.lang.reflect.Method.invokeNative(Native Method)
12-18 20:25:44.319: E/AndroidRuntime(647):  at java.lang.reflect.Method.invoke(Method.java:511)
12-18 20:25:44.319: E/AndroidRuntime(647):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-18 20:25:44.319: E/AndroidRuntime(647):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 20:25:44.319: E/AndroidRuntime(647):  at dalvik.system.NativeStart.main(Native Method)
12-18 20:25:44.319: E/AndroidRuntime(647): Caused by: java.lang.NullPointerException
12-18 20:25:44.319: E/AndroidRuntime(647):  at com.example.app.MainActivity.onCreate(MainActivity.java:32)
12-18 20:25:44.319: E/AndroidRuntime(647):  at android.app.Activity.performCreate(Activity.java:5008)
12-18 20:25:44.319: E/AndroidRuntime(647):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-18 20:25:44.319: E/AndroidRuntime(647):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-18 20:25:44.319: E/AndroidRuntime(647):  ... 11 more
12-18 20:26:10.839: I/Process(647): Sending signal. PID: 647 SIG: 9
12-18 20:26:16.799: E/Trace(662): error opening trace file: No such file or directory (2)
12-18 20:26:17.249: D/AndroidRuntime(662): Shutting down VM
12-18 20:26:17.249: W/dalvikvm(662): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-18 20:26:17.260: E/AndroidRuntime(662): FATAL EXCEPTION: main
12-18 20:26:17.260: E/AndroidRuntime(662): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.NullPointerException
12-18 20:26:17.260: E/AndroidRuntime(662):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-18 20:26:17.260: E/AndroidRuntime(662):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-18 20:26:17.260: E/AndroidRuntime(662):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-18 20:26:17.260: E/AndroidRuntime(662):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-18 20:26:17.260: E/AndroidRuntime(662):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 20:26:17.260: E/AndroidRuntime(662):  at android.os.Looper.loop(Looper.java:137)
12-18 20:26:17.260: E/AndroidRuntime(662):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-18 20:26:17.260: E/AndroidRuntime(662):  at java.lang.reflect.Method.invokeNative(Native Method)
12-18 20:26:17.260: E/AndroidRuntime(662):  at java.lang.reflect.Method.invoke(Method.java:511)
12-18 20:26:17.260: E/AndroidRuntime(662):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-18 20:26:17.260: E/AndroidRuntime(662):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 20:26:17.260: E/AndroidRuntime(662):  at dalvik.system.NativeStart.main(Native Method)
12-18 20:26:17.260: E/AndroidRuntime(662): Caused by: java.lang.NullPointerException
12-18 20:26:17.260: E/AndroidRuntime(662):  at com.example.app.MainActivity.onCreate(MainActivity.java:32)
12-18 20:26:17.260: E/AndroidRuntime(662):  at android.app.Activity.performCreate(Activity.java:5008)
12-18 20:26:17.260: E/AndroidRuntime(662):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-18 20:26:17.260: E/AndroidRuntime(662):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-18 20:26:17.260: E/AndroidRuntime(662):  ... 11 more
12-18 20:26:49.719: I/Process(662): Sending signal. PID: 662 SIG: 9

4 个答案:

答案 0 :(得分:0)

我在这里发现了你的错误


EditText TextState;
 TextState = (EditText)findViewById(R.id.TextState)
改成 TextView TextState;

TextState = (TextView)findViewById(R.id.TextState)

或在XML文件中

<EditText
android:id="@+id/TextState"/>

答案 1 :(得分:0)

Caused by: java.lang.NullPointerException
at com.example.app.MainActivity.onCreate(MainActivity.java:32)

刚刚阅读了你的Logcat,你在第32行得到了一个N​​PE。可能findViewById找不到你的View并且该字段为空。

答案 2 :(得分:0)

你应该在第32行找到问题

com.example.app.MainActivity.onCreate(MainActivity.java:<强> 32

答案 3 :(得分:0)

更改EditText的TextView。问题解决了