Я искал мой вопрос здесь и в гугле с небольшим успехом. Я пришел близко, но не совсем нашел мой вопрос. Так что я новичок в разработке Android , и я не могу понять, где я нахожусь здесь не так. Я делаю учебник для Начиная другой Активность на сайте developer.android.com.
Вот мой MainActivity.java
:
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.content.Intent;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = com.example.myfirstapp.MESSAGE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** 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);
}
}
Линия String message = editText.getText().toString();
в IDE говорит , что это может бросить Java.lang.NullPointerException
. Когда я запустить приложение, он загружает, а затем падает.
Вот мой activity_main.xml
:
<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: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=.MainActivity>
<EditText android:id=@+id/edit_message
android:layout_width=wrap_content
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
android:name=com.example.myfirstapp.DisplayMessageActivity
android:label=@string/title_activity_display_message
android:parentActivityName=com.example.myfirstapp.MainActivity >
<meta-data
android:name=android.support.PARENT_ACTIVITY
android:value=com.example.myfirstapp.MainActivity />
</activity>
</LinearLayout>
Вот мой DisplayMessageActivity.java:
package com.example.myfirstapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;
/**
* Created by rvance on 5/16/13.
*/
public class DisplayMessageActivity extends Activity {
@SuppressLint(NewApi)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
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) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Вот мой файл AndroidManifest.xml:
<?xml version=1.0 encoding=utf-8?>
<manifest xmlns:android=http://schemas.android.com/apk/res/android
package=com.example.myfirstapp
android:versionCode=1
android:versionName=1.0 >
<uses-sdk
android:minSdkVersion=8
android:targetSdkVersion=17 />
<application
android:allowBackup=true
android:icon=@drawable/ic_launcher
android:label=Russell's First App
android:theme=@style/AppTheme >
<activity
android:name=com.example.myfirstapp.MainActivity
android:label=@string/app_name >
<intent-filter>
<action android:name=android.intent.action.MAIN />
<category android:name=android.intent.category.LAUNCHER />
</intent-filter>
</activity>
<activity android:name=.DisplayMessageActivity android:label=activity_display_message/>
</application>
</manifest>
Проблема, мне кажется, что в методе SendMessage () в классе MainActivity. В частности, линияString message = editText.getText().toString();
/** 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(); // The line that crashes is here
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
В конце концов, я не сомневаюсь, что облажались где-то, но я не вижу, где. Буду признателен за любую оказанную помощь.