onClick method issue

前端 未结 3 803
终归单人心
终归单人心 2021-01-14 20:22

ok, so I am going through the tutorial of the \"MyFirstApp\" on the devoloper.android.com site. I am on the last tutorial and I have did everything for the first chapter you

相关标签:
3条回答
  • 2021-01-14 21:06

    I think you're getting the message from the intent the wrong way. EXTRA_MESSAGE is the content and message is the identifier.

    So your code on DisplayMessageActivity.java

    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    

    should actually be:

    String message = intent.getStringExtra(message);
    
    0 讨论(0)
  • 2021-01-14 21:15

    You can see in your activity_main.xml you have to declare button with onClick() this way then and then it will be work.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click" 
            android:onClick="sendMessage()"/>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2021-01-14 21:20

    It looks like you misspelled the function name in your xml for the Button onClick(). You have an uppercase "m" in your java code

     public void sendMessage(View view) {
    

    and probably a lowercase "m" in your xml

    <Button
    ...
    android:onClick="sendmessage"/>
    

    To comply with java standards, change it in your xml to

     android:onClick="sendMessage"/>
    
    0 讨论(0)
提交回复
热议问题