2014-07-16 20 views
1

即使使用其他類如編輯文本,我也有此問題。不幸的是,應用程序已停止按鈕對象

我確定這個ID。

這是代碼:

public class MainActivity extends ActionBarActivity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button bshow = (Button)findViewById(R.id.show_msg); 

     bshow.setText("Showing"); 
    } 

} 

這是fragment_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.exampleknight283.e003.MainActivity$PlaceholderFragment" > 

    <TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

    <Button 
    android:id="@+id/show_msg" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_marginTop="44dp" 
    android:text="@string/showv" /> 

    </RelativeLayout> 

和我得到這個在註銷

07-16 11:27:32.155: E/Trace(1382): error opening trace file: No such file or  directory (2) 
    07-16 11:27:32.625: D/AndroidRuntime(1382): Shutting down VM 
    07-16 11:27:32.625: W/dalvikvm(1382): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
    07-16 11:27:32.645: E/AndroidRuntime(1382): FATAL EXCEPTION: main 
    07-16 11:27:32.645: E/AndroidRuntime(1382): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exampleknight283.e003/com.exampleknight283.e003.MainActivity}: java.lang.NullPointerException 
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.os.Looper.loop(Looper.java:137) 
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.ActivityThread.main(ActivityThread.java:5041) 
07-16 11:27:32.645: E/AndroidRuntime(1382): at java.lang.reflect.Method.invokeNative(Native Method) 
07-16 11:27:32.645: E/AndroidRuntime(1382): at java.lang.reflect.Method.invoke(Method.java:511) 
07-16 11:27:32.645: E/AndroidRuntime(1382): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-16 11:27:32.645: E/AndroidRuntime(1382): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    07-16 11:27:32.645: E/AndroidRuntime(1382): at dalvik.system.NativeStart.main(Native Method) 
    07-16 11:27:32.645: E/AndroidRuntime(1382): Caused by: java.lang.NullPointerException 
07-16 11:27:32.645: E/AndroidRuntime(1382): at com.exampleknight283.e003.MainActivity.onCreate(MainActivity.java:29) 
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.Activity.performCreate(Activity.java:5104) 
07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
    07-16 11:27:32.645: E/AndroidRuntime(1382): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 

我在Android開發新的。但我在java的不錯。現在我按照一些教程,我沒有exastly他做了什麼,但我有錯誤,喜歡什麼,我說我有其他類,如「編輯類」同樣的問題

+0

你有logcat嗎? – Chefes

+0

列出您的logcat和activity_main.xml –

+0

[可惜MyApp已停止。我怎樣才能解決這個問題?](http://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this) – panini

回答

0

嘗試:

public class MainActivity extends ActionBarActivity 
{ 
    Button bshow; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     bshow = (Button) findViewById(R.id.show_msg); 

     bshow.setText("Showing"); 
    } 

} 
+0

爲什麼這個工作,當OP不會? – stealthjong

+0

是的stealthjong你是對的它是關於activity_main我沒有設置,因爲我是新的Android開發。謝謝 –

相關問題