2014-04-18 111 views
0

我是新來的android,現在我想按下一個按鈕,然後在屏幕上顯示文本。這裏是我的代碼:按下按鈕後顯示文本android

XML

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="sendMessages" 
    android:text="Button" /> 

<TextView 
    android:id="@+id/buttonText" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" 
    android:ems="10" 
    android:inputType="textMultiLine" 
    android:text="hello" /> 

主要活動

public class MainActivity extends ActionBarActivity { 

    protected void onCreate(Bundle savedInstanceState){ 

     ....... 
    } 

    public void sendMessages(View view) { 
     TextView welcome = (TextView) findViewById(R.id.buttonText); 
      welcome.setText("button clicked"); 

    } 
} 

,但是當我在一個模擬器上運行它,按下按鈕後,該應用程序將報告

fortunately, First Android App has stopped 

沒有人知道我的程序有什麼問題嗎?

04-18 00:56:20.147: D/AndroidRuntime(1280): Shutting down VM 
04-18 00:56:20.147: W/dalvikvm(1280): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
04-18 00:56:20.177: E/AndroidRuntime(1280): FATAL EXCEPTION: main 
04-18 00:56:20.177: E/AndroidRuntime(1280): java.lang.IllegalStateException: Could not execute method of the activity 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at  android.view.View$1.onClick(View.java:3591) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at  android.view.View.performClick(View.java:4084) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at android.view.View$PerformClick.run(View.java:16966) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at android.os.Handler.handleCallback(Handler.java:615) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at android.os.Looper.loop(Looper.java:137) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at dalvik.system.NativeStart.main(Native Method) 
04-18 00:56:20.177: E/AndroidRuntime(1280): Caused by: java.lang.reflect.InvocationTargetException 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at android.view.View$1.onClick(View.java:3586) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  ... 11 more 
04-18 00:56:20.177: E/AndroidRuntime(1280): Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.TextView 
04-18 00:56:20.177: E/AndroidRuntime(1280):  at com.example.firstandroidapp.MainActivity.sendMessages(MainActivity.java:110) 
04-18 00:56:20.177: E/AndroidRuntime(1280):  ... 14 more 
+1

發佈完整的錯誤'LogCat'。這個'Button'是否存在於活動佈局中? –

+0

@Hamid Shatu我已經添加了logcat輸出,按鈕確實顯示在佈局中,但一旦按下,應用就會崩潰:( – user2810081

+1

@ user2810081乾淨並構建您的項目 – Raghunandan

回答

0

Do button和TextView是否存在於活動佈局中? 您是否在onCreate中調用 setContentView(Layout)? 你在使用ADT嗎?

如果您正在使用ADT工作,就可以在的onCreate創建一個新的活動, 一個新的XML佈局,所以調用的setContentView(R.layout.YourLayout)

YourLayout.xml必須包含按鈕和TextView的

+0

我做了所有這些,但是當檢查xml時,我發現還有一個id名buttonText,我改成另一個id,問題解決了!謝謝! – user2810081

相關問題