2017-04-19 27 views
0

我試圖開發一個應用程序,它從用戶處接收消息並在按鈕(發送)時在新選項卡中顯示消息被按下,但是當我點擊該按鈕的應用程序停止,不顯示消息從用戶接收消息並顯示它,但是當我單擊按鈕時,應用程序停止

主要活動

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清單

<?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' 
+0

後logcat的錯誤 –

+1

把你的堆棧跟蹤這裏 –

+1

確保在AndroidMainfest –

回答

1

這條線是錯誤的用於設置onCl聽衆。 android:onClick =「sendMessage(MainActivity)」

我甚至不打擾宣告onClick這樣。只需從您的活動中的代碼中進行設置。

​​

您也可以真正享受Butterknife的綁定,而不是總用findById並可以註解監聽功能

+0

我越來越setOnClickListene()無法解析。 –

+0

@prakash你拼寫監聽錯誤...使用IDE的自動完成 –

+0

我試過了這個建議,但它沒有奏效。 –

相關問題