FragmentActivity按钮在片段之间切换

时间:2014-02-06 11:48:29

标签: android android-intent android-fragments android-button

我想要做的就是在MainActivity和ArticleGroups页面之间进行简单的Button页面切换。之后我将实现一个分页类,但我想让按钮先​​工作。

一旦我运行应用程序,应用程序将崩溃并强制关闭,所以我肯定做错了。

MainActivity.java

 public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b = (Button)findViewById(R.id.articlebtn);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick (View v) {
                Intent i = new Intent(MainActivity.this, ArticleGroup.class);
                startActivity(i);
            }
        });
        }

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

    }

activity_main.xml中

    <?xml version="1.0" encoding="UTF-8"?>
<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" tools:context=".MainActivity">

    <fragment
        android:id="@+id/activitymain_fragment"
        android:name="com.dharris.mindfulemployer_v4.MainActivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/articlebtn"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@color/aqua"
        android:onClick="onClick" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/articlebtn"
        android:layout_alignBottom="@+id/articlebtn"
        android:layout_toRightOf="@+id/articlebtn"
        android:text="@string/hello_world" />

</RelativeLayout>

ArticleGroup.java

public class ArticleGroup extends FragmentActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.articlegroup);

    Button b = (Button) findViewById(R.id.articlebtn);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(ArticleGroup.this, MainActivity.class);
            startActivity(i);
        }

    });

}

}

articlegroup.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/articlegroupfragment"
        android:name="com.dharris.mindfulemployer_v4.ArticleGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/articlebtn"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@color/aqua"
        android:onClick="onClick" />

</RelativeLayout>

清单文件

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

   <!--  Activity Default  -->     
        <activity
            android:name="com.dharris.mindfulemployer_v4.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 ArticleGroup -->
        <activity 
            android:name=".ArticleGroup"
            android:label="@string/app_name"
            ></activity>

    </application>

</manifest>

如果有人能够理解为什么这不起作用,那么我将非常感激。

2 个答案:

答案 0 :(得分:0)

你有

 android:onClick="onClick" />

你需要在Activity中按名称onClick,将视图作为参数

您可以删除此

   Button b = (Button)findViewById(R.id.articlebtn);

    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick (View v) {
            Intent i = new Intent(MainActivity.this, ArticleGroup.class);
            startActivity(i);
        }
    });

并且

 public void onClick(View V)
 {

     // do something on button click
 }

答案 1 :(得分:0)

从activity_main.xml和articlegroup.xml

中删除此行
android:onClick="onClick"