2015-08-29 98 views
2

我已經運行該應用程序並向其中寫入了一些文本並按下發送。這應該輸出「Hello World!」但相反,應用程序只是每次崩潰。如果還有什麼我可以提供的,我可以這樣做。這是我第一次學習編碼,所以我不知道我可能會出錯。謝謝!創建第二個活動的問題

這是我MyActivity.java:

public class MyActivity extends AppCompatActivity { 
public final static String EXTRA_MESSAGE = "anatol.healthygaming.MESSAGE"; 
/** Called when the user clicks the Send button */ 
public void sendMessage(View view) { 
    Intent intent = new Intent(this, DisplayMessageActivity.class); 
    EditText editText = (EditText) findViewById(R.id.edit_message); 
    String message = editText.getText().toString(); 
    intent.putExtra(EXTRA_MESSAGE, message); 
    startActivity(intent); 

} 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_my, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

這是我DisplayMessageActivity.java:

public class DisplayMessageActivity extends AppCompatActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Get the message from the intent 
    Intent intent = getIntent(); 
    String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE); 

    // Create the text view 
    TextView textView = new TextView(this); 
    textView.setTextSize(40); 
    textView.setText(message); 

    // Set the text view as the activity layout 
    setContentView(textView); 
} 



@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

它說,它是由引起:

Caused by: java.lang.reflect.InvocationTargetException 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference 

Activity_my:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal"> 
<EditText android.id="@+id/edit_message" 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:hint="@string/edit_message" /> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_send" 
    android:onClick="sendMessage"/> 

Activity_display_message:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="anatol.healthygaming.DisplayMessageActivity"> 

<TextView android:text="@string/hello_world" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

+0

請顯示活動請求的android清單文件和xml文件文件。 –

+0

顯示您的佈局文件'activity_my' –

+0

在activity_my.xml中添加了activity_my –

回答

1

EditTextactivity_my.xml文件有一個android.id。將其更改爲android:id,它應該可以解決您的問題。

原因:
由於android.id在android-xml中無效,因此EditText沒有ID。當您在MyActivity.java
EditText editText = (EditText) findViewById(R.id.edit_message);
,編譯器無法找到R.id.edit_message,因爲它不存在,那麼editText被賦予了null值。

0

是的除了Kritixi Lithos告訴你應該在Android Manifest文件中包含你的活動,如果你還沒有。

相關問題