繁体   English   中英

在android中按下按钮后显示文本

[英]show text after pressing button in 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

活动布局中是否存在button和TextView? in onCreate? 您是否在onCreate中调用 您正在使用ADT吗?

如果您正在使用ADT,则可以创建一个新的活动和一个新的xml布局,因此在onCreate中调用setContentView(R.layout.YourLayout)

YourLayout.xml必须包含按钮和TextView

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM