从第二个活动到android中的第三个活动的简单导航,不传递任何值

时间:2014-11-05 16:45:46

标签: android

从第一个活动到第二个活动导航正在工作但是第三个活动导航的第二个活动在android中无法工作任何1请帮助我。

这是我的MainActivity.java


package com.exampl.test;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.app.Activity;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void activity2(View view){
        Intent intent = new Intent(this,com.exampl.test.MainActivity2.class);
        startActivity(intent);
     }

    @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;
    }


}

这是我的MainActivity2.java

package com.exampl.test;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;


public class MainActivity2 extends Activity {

   @SuppressLint("NewApi")
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_activity2);
      getActionBar().setDisplayHomeAsUpEnabled(true);
   }
   public void activity3(View view){
       Intent intent = new Intent(this,com.exampl.test.MainActivity3.class);
       startActivity(intent);
   }
       
   @Override
   public void onBackPressed() {
	   
	        super.onBackPressed();
   }
	   
}

这是我的MainActivity3.java

package com.exampl.test;

import android.annotation.SuppressLint;
import android.app.Activity;

import android.os.Bundle;


public class MainActivity3 extends Activity {

   @SuppressLint("NewApi")
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_activity3);
      getActionBar().setDisplayHomeAsUpEnabled(true);
   }
   @Override
   public void onBackPressed() {
	   
	        super.onBackPressed();
   }
	   
}

这是我的appmanifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exampl.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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.exampl.test.MainActivity2"
         android:label="@string/title_activity_main_activity2"
         android:parentActivityName="com.exampl.test.MainActivity" >
      </activity>
      <activity
         android:name="com.exampl.test.MainActivity3"
         android:label="@string/title_activity_main_activity3"
         android:parentActivityName="com.exampl.test.MainActivity2" >
      </activity>
    </application>

</manifest>

请帮我修改以上编码 提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个!

MainActivity

package com.example.acct;

import com.example.acct.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button =(Button) findViewById(R.id.button1);
        Toast.makeText(this, "Activity1", Toast.LENGTH_SHORT).show();
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                activity2();
            }
        });
    }

    public void activity2() {
        Intent intent = new Intent(this, com.example.acct.MainActivity2.class);
        startActivity(intent);
    }

    @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;
    }

}

MainActivity2

 package com.example.acct;

import com.example.acct.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity2 extends Activity {
    Button button;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        button = (Button) findViewById(R.id.button1);
        Toast.makeText(this, "Activity2", Toast.LENGTH_SHORT).show();
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                activity3();
            }
        });
    }

    public void activity3() {
        Intent intent = new Intent(this, com.example.acct.MainActivity3.class);
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
    }

}

MainActivity3

  package com.example.acct;

import com.example.acct.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity3 extends Activity {
    Button button;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        Toast.makeText(this, "Activity3", Toast.LENGTH_SHORT).show();
        getActionBar().setDisplayHomeAsUpEnabled(true);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                activity3();
            }
        });
    }

    public void activity3() {
        Intent intent = new Intent(this, com.example.acct.MainActivity3.class);
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
    }

}

的AndroidManifest.xml

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.acct"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.acct.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=".MainActivity2"
         android:parentActivityName=".MainActivity" >
      </activity>
      <activity
         android:name=".MainActivity3"
         android:parentActivityName=".MainActivity2" >
      </activity>
    </application>

</manifest>