从用户那里收到一条消息并显示它,但是当我点击该按钮时,该应用就会停止

时间:2017-04-19 14:49:17

标签: android

我正在尝试开发一个应用程序,该应用程序从用户接收消息并在按下按钮(发送)时在新选项卡中显示消息,但是当我单击按钮时应用程序停止而不显示消息

主要活动

 package com.example.prakash.myfirst;
 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

public static final String EXTRA_MESSAGE = "com.example.Myfirst.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = "hi";
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}}

显示消息活动

   package com.example.prakash.myfirst;

     import android.content.Intent;
     import android.support.v7.app.AppCompatActivity;
     import android.os.Bundle;
     import android.widget.TextView;

    public class DisplayMessageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        TextView text = (TextView) findViewById(R.id.textView);
        text.setText(message);
    }
}

activity_main.xml中

  <Button
        android:text="@string/send_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:elevation="0dp"
        tools:text="@string/send_button"
        tools:hint="@string/send_button"
        android:onClick="sendMessage (MainActivity)"
        app:layout_constraintTop_toTopOf="@+id/editText"
        app:layout_constraintBottom_toBottomOf="@+id/editText"
        app:layout_constraintVertical_bias="0.6"
        android:layout_marginEnd="20dp"
        app:layout_constraintRight_toRightOf="parent" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editText"
        android:layout_marginStart="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        app:layout_constraintRight_toLeftOf="@+id/button"
        android:layout_marginEnd="20dp"
        android:hint="@string/edit_msg" />

    <TextView
        android:text="TextView"
        android:layout_width="196dp"
        android:layout_height="36dp"
        android:id="@+id/textView2"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp" />
</android.support.constraint.ConstraintLayout>

android manifest

  <?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.prakash.myfirst">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DisplayMessageActivity"
        android:parentActivityName=".MainActivity" >
        <!-- The meta-data tag is required if you support API level 15 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>
</application>

logcat的

 04-21 06:01:54.454 6266-6266/com.example.prakash.myfirst 
 E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
 length
 >04-21 06:01:54.454 6266-6266/com.example.prakash.myfirst 
 E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
 length
 04-21 06:01:55.652 6266-6266/com.example.prakash.myfirst 
 E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
 length
 04-21 06:01:55.652 6266-6266/com.example.prakash.myfirst 
 E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
 length
 04-21 06:01:59.147 6266-6266/com.example.prakash.myfirst E/AndroidRuntime: 
 FATAL EXCEPTION: main

  Process: com.example.prakash.myfirst, PID: 6266                                                                     
  java.lang.IllegalStateException: Could not find method sendMessage 
  (MainActivity)(View) in a parent or ancestor Context for android:onClick 
  attribute defined on view class android.support.v7.widget.AppCompatButton 
  with id 'button'

1 个答案:

答案 0 :(得分:1)

此行设置onClick侦听器是错误的。 android:onClick =&#34; sendMessage(MainActivity)&#34;

我甚至不打算像那样声明onClick。只需在活动中的代码中设置它即可。

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               sendMessage(v)
            }
          });

您可能也非常喜欢Butterknife的绑定,而不是总是必须使用findById并且能够注释监听器函数