我是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
答案 0 :(得分:0)
活动布局中是否存在按钮和TextView? 你在onCreate中调用 setContentView(布局)吗? 你在使用ADT吗?
如果您正在使用ADT,您可以创建一个新活动 一个新的xml布局,所以调用setContentView(R.layout.YourLayout) 在onCreate
YourLayout.xml必须包含按钮和TextView