我是学习Android开发的新手,我正在关注this tutorial来创建一个小应用程序。我只是改变了一些代码。当我点击发送按钮时,我得到一个提示“不幸的是,我的第一个应用已经停止了。”有人可以告诉我无论如何。
我替换了教程中给出的代码(此处显示为第二类代码中的块注释),并替换为包含 * 的注释的代码。
第一项活动: -
package com.practice.firstapp1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import com.practice.firstapp1.R;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE= "com.practice.fristapp1.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 */
/** 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.id1);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
第一次活动的布局: -
<?xml version="1.0" encoding="utf-8"?>
<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/id1"
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" />
</LinearLayout>
第二次活动的代码: -
package com.practice.firstapp1;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
//Get the intent which invoked this activity and read its extra's data into the string named message.
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = (TextView) findViewById(R.id.text_view_2); //************
textView.setText(message);//***********
setContentView(textView);//***********
//The above three statements along with statements from its layout (which have been given *s in comments have been placed instead of the statements in the following block comment. Because I wanted to create the TextView in layout.xml and handle its work in code.
/*
// 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) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
第二次活动的布局: -
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView <!--//*********************************-->
android:id="@+id/text_view_2" <!--//***********************-->
android:layout_width="wrap_content" <!--//**********************-->
android:layout_height="wrap_content" <!--//**************-->
android:textSize="40sp" /> <!--//**************************-->
</RelativeLayout>
ANDROID MANIFEST FILE: -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.practice.firstapp1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.practice.firstapp1.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="com.practice.firstapp1.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.practice.firstapp1.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.practice.firstapp1.MainActivity" />
</activity>
</application>
</manifest>
答案 0 :(得分:1)
来自您的评论
I havn't done anything in the manifest file. I think it is done automatically when we are using eclipse.
是的,如果您创建新的Android活动,eclipse会将活动添加到清单。
你的清单看起来很好。删除以下行
编辑1:
setContentView(textView);
activity_display_message.xml
已经有了textview,您可以在代码中初始化textView并设置文本。 TextView已经有了父级。
无需setContentView(textView)