[英]Application crash starting another activity with error ActivityNotFoundException
[英]Starting another activity - application crash
一開始,我注意到我是Android開發的新手。
我從http://developer.android.com/training/basics/firstapp/starting-activity.html制作了應用程序,並且運行良好(也是第二項活動)。 我想做些小改動:在第二個活動中,我想顯示“您的消息:”文本,以及在第一個活動中輸入的消息下方。 我以自己的方式做到了,現在當我點擊“發送”按鈕時,應用程序崩潰了。
我的第二個活動xml代碼:
<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="com.example.lislav.firstandroidapp.DisplayMessageActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/header_my_message"
android:textSize="40px"/>
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70px"/>
</RelativeLayout>
和第二個活動的Java代碼:
package com.example.lislav.firstandroidapp;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textViewMessage = (TextView) findViewById(R.id.message);
textViewMessage.setText(message);
setContentView(R.layout.activity_display_message);
}
@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);
}
}
我認為onCreate函數有問題。但是我不知道是什么。 對不起,如果有任何明顯的錯誤。
僅在調用setContentView
之后,才必須調用findViewById
方法。 只需在調用super.onCreate
之后立即放置setContentView
super.onCreate
。
順便閱讀一下logcat
工具。 這是記錄器,您應該始終使用它來面對任何問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.