我按照android官网的教程做了第一个android app,但是在我的nexus4手机上做测试时发现程序在完成第一个活动后无法进入第二个活动。想让大神们帮忙看一下,谢谢啦!package com.example.myfirstapp;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
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;
    }
    
    public void sendMessge(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);
    }
}
package com.example.myfirstapp;import android.annotation.SuppressLint;
import android.annotation.TargetApi;
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);
// Show the Up button in the action bar.
setupActionBar();
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB){
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Intent intent=getIntent();
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

TextView textView=new TextView(this);
textView.setTextSize(40);
textView.setText(message);

setContentView(textView);
} /**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        /></RelativeLayout><?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/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"/>
    
    
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<resources>    <string name="app_name">My First App</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="title_activity_display_message">My Message</string>
    </resources>
android测试

解决方案 »

  1.   

    DisplayMessageActivity这个你在AndroidManaifeset.xml里面注册了吗
      

  2.   

    这个已经注册了,这是eclipse自动加上去的。无法运行和android的版本有关系么,教程上说他们的代码都是在android4.0上运行的,我这个是4.3<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>
      

  3.   

    这个已经注册了,这是eclipse自动加上去的。无法运行和android的版本有关系么,教程上说他们的代码都是在android4.0上运行的,我这个是4.3<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>
    没关系,报的什么错
      

  4.   

    这个已经注册了,这是eclipse自动加上去的。无法运行和android的版本有关系么,教程上说他们的代码都是在android4.0上运行的,我这个是4.3<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>
    没关系,报的什么错
    控制面板显示的是这些,没有报错
    [2013-10-11 01:48:45 - MyFirstApp] ------------------------------
    [2013-10-11 01:48:45 - MyFirstApp] Android Launch!
    [2013-10-11 01:48:45 - MyFirstApp] adb is running normally.
    [2013-10-11 01:48:45 - MyFirstApp] Performing com.example.myfirstapp.MainActivity activity launch
    [2013-10-11 01:48:45 - MyFirstApp] Automatic Target Mode: using device '047faa901cd02ed8'
    [2013-10-11 01:48:45 - MyFirstApp] Uploading MyFirstApp.apk onto device '047faa901cd02ed8'
    [2013-10-11 01:48:45 - MyFirstApp] Installing MyFirstApp.apk...
    [2013-10-11 01:48:48 - MyFirstApp] Success!
    [2013-10-11 01:48:48 - MyFirstApp] Starting activity com.example.myfirstapp.MainActivity on device 047faa901cd02ed8
    [2013-10-11 01:48:48 - MyFirstApp] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.myfirstapp/.MainActivity }
    [2013-10-11 02:59:25 - MyFirstApp] ------------------------------
    [2013-10-11 02:59:25 - MyFirstApp] Android Launch!
    [2013-10-11 02:59:25 - MyFirstApp] adb is running normally.
    [2013-10-11 02:59:25 - MyFirstApp] Performing com.example.myfirstapp.MainActivity activity launch
    [2013-10-11 02:59:25 - MyFirstApp] Automatic Target Mode: using device '047faa901cd02ed8'
    [2013-10-11 02:59:25 - MyFirstApp] Uploading MyFirstApp.apk onto device '047faa901cd02ed8'
    [2013-10-11 02:59:25 - MyFirstApp] Installing MyFirstApp.apk...
    [2013-10-11 02:59:27 - MyFirstApp] Success!
    [2013-10-11 02:59:27 - MyFirstApp] Starting activity com.example.myfirstapp.MainActivity on device 047faa901cd02ed8
    [2013-10-11 02:59:28 - MyFirstApp] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.myfirstapp/.MainActivity }
      

  5.   

    这是Consle输出的,没有错误。
    发LogCat上来看看。
      

  6.   

    setContentView(textView);是死在这里么。
      

  7.   

    把setupActionBar();注释掉再试一下。
      

  8.   

    sendMessge(View v)在onCreate里面没调用
      

  9.   

    是的,因为打错了,应该是sendMessage(View view),现在改过来了,没有问题了。谢谢了