我正在關注Google的android dev教程。我剛剛開始,但我遇到了一個問題,似乎無法解決它。 http://developer.android.com/training/basics/firstapp/starting-activity.html#ReceiveIntent應用程序崩潰按鈕點擊
每當我點擊「發送」按鈕,我的應用程序崩潰,而不是打開新的活動。
的XML我最初的家庭活動:
</LinearLayout>
<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:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</LinearLayout>
該方法的sendMessage在Home.Java文件
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);
}
和Java的新活動
//Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(Home.EXTRA_MESSAGE);
//Creating the textview
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
//Set text view as activity layout
setContentView(textView);
的主要錯誤:
java.lang.RuntimeException: Unable to start activity ComponentInfo{appfactree.me.dotnotes/appfactree.me.dotnotes.DisplayMessageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
請問你是否需要別的東西。 非常感謝您的幫助。
編輯:按照要求完整DisplayMessageActivity.java代碼:
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(Home.EXTRA_MESSAGE);
//Creating the textview
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
//Set text view as activity layout
setContentView(textView);
}
}
發佈您的DisplayMessageActivity.java代碼 –
這個問題最有可能發生在您的DisplayMessageActivity.class –
再次讀取您的錯誤,您需要解決您的問題的一切都在那裏。 (另外,您的錯誤不在發佈的代碼中) – njzk2