2014-04-18 163 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

相关问题