2013-12-18 76 views
0

我認爲應用程序停止了setContentView(R.layout.main);TextState = (EditText) findViewById(R.id.TextState);以及更多,但我不知道需要做什麼。抱歉不正確的帖子,但請幫助我。Android」不幸的是,<app>已停止。「

\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 
+3

'TextState'是一個'TextView',而不是'EditText' –

+0

作爲zouzou說textstate是一個textview不是edittext替換TextState =(EditText)findViewById(R.id.TextState);與TextState =(TextView)findViewById(R.id.TextState); – saiful103a

+0

我這樣做,但沒有幫助 – user2800980

回答

0

我發現這裏你的錯誤


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

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

或者在XML文件

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

更改TextState =(EditText)findViewById(R.id.TextState)TextState =(TextView)findViewById(R.id.TextState)並沒有幫助(( – user2800980

+0

Chang in xml as i said^^請嘗試 – user1747373

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

只要閱讀你的Logcat,你會得到一個NPE在32行。可能findViewById沒有找到你的視圖,該字段爲空。

0

你應該找到符合32

com.example.app.MainActivity.onCreate問題(MainActivity.java: