在Android中切换三个活动?

时间:2014-12-08 07:19:53

标签: android android-activity

我有三个活动的场景:

  1. MainActivity
  2. DerivedActivity
  3. ThirdPage
  4. 我想在第一到第二和第三之间导航。

    这是我的 Manifest (AndroidManifest.xml)

       <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="org.example.implicitexplicit"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="14"
            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=".DerivedActivity"></activity>
            <activity android:name=".ThirdPage"></activity>
        </application>
    
    </manifest>
    

    现在是主要活动(MainActivity.java)

    package org.example.implicitexplicit;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    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 impliex(View v)
    {
        Intent intent=new Intent(this, DerivedActivity.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;
    }
    
    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    }
    

    第二个(DerivedActivity.java)

    package org.example.implicitexplicit;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.MenuItem;
    import android.view.View;
    public class DerivedActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.derived_activity);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    
    private void explicit(View v) {
            Intent intent = new Intent(this, ThirdPage.class);
            startActivity(intent);
        }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        if (item.getItemId()==android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
    }
    

    和上一个活动(ThirdPage.java)

    package org.example.implicitexplicit;
    import android.app.Activity;
    import android.os.Bundle;
    public class ThirdPage extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.thirdpage_activity);
        }
    }
    

    在DerivedActivity.java中,我收到警告

    The method explicit(View) from the type DerivedActivity is never used locally
    

    xml文件如下

    activity_main.xml中

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/eia"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" 
        android:contentDescription="@string/imgalt"/>
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/desc"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/clickme" 
        android:onClick="impliex"/>
    
    </LinearLayout>
    

    derived_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/derivedText"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/clickme"
        android:onClick="explicit"
         />
    
    </LinearLayout>
    

    thirdpage_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/thirdpageText"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
    </LinearLayout>
    

    错误:

    java.lang.IllegalStateException: Could not find a method explicit(View) in the activity class org.example.implicitexplicit.DerivedActivity for onClick handler on view class android.widget.Button with id 'button1'
    

4 个答案:

答案 0 :(得分:4)

private void explicit更改为

public void explicit(View v) {
        Intent intent = new Intent(this, ThirdPage.class);
        startActivity(intent);
    }

答案 1 :(得分:3)

您的explicit方法private您必须将该方法public用作clickListener

答案 2 :(得分:2)

将该方法private的访问修饰符更改为public

public void explicit(View v) {
    Intent intent = new Intent(this, ThirdPage.class);
    startActivity(intent);
}

答案 3 :(得分:1)

您将方法声明为私有,但应首先公开。 第二个例外显示方法的名称是explicitURl,但实际上它是显式的(View)