Android 如何使用按钮跨多个页面打开XML页面

Android 如何使用按钮跨多个页面打开XML页面,android,Android,我已经在我的应用程序的主页上有了按钮,但我正在尝试这样做,以便我可以在多个页面上有按钮。我试着创建一个新的.java文件,就像原始文件一样,并对其进行编辑,我还试着将第二页添加到我的原始代码中,但都不起作用。这是我的密码: package com.dogger20011.app; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import androi

我已经在我的应用程序的主页上有了按钮,但我正在尝试这样做,以便我可以在多个页面上有按钮。我试着创建一个新的.java文件,就像原始文件一样,并对其进行编辑,我还试着将第二页添加到我的原始代码中,但都不起作用。这是我的密码:

package com.dogger20011.app;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class beforemain extends MainActivity implements OnClickListener {

private AutoUpdateApk aua;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
aua = new AutoUpdateApk(getApplicationContext());

Button mBtn1 = (Button) findViewById(R.id.button1);
Button mBtn2 = (Button) findViewById(R.id.button2);
Button mBtn3 = (Button) findViewById(R.id.button3);
Button mBtn4 = (Button) findViewById(R.id.button4);
Button mBtn5 = (Button) findViewById(R.id.button5);


mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
mBtn3.setOnClickListener(this);
mBtn4.setOnClickListener(this);
mBtn5.setOnClickListener(this);
}



@Override
public void onClick(View v) {
switch(v.getId()) { //Get the id of the button that was clicked
case R.id.button1:
    Intent i = new Intent(beforemain.this, Activity2.class);
    startActivity(i);
    break;
case R.id.button2:
    Intent i1 = new Intent(beforemain.this, Activity3.class);
    startActivity(i1);
    break;
case R.id.button3:
    Intent i2 = new Intent(beforemain.this, Activity4.class);
    startActivity(i2);
    break;
case R.id.button4: 
    Intent i3 = new Intent(beforemain.this, Activity5.class);
 startActivity(i3);
 break;
case R.id.button5:
    Intent i5 = new Intent(Intent.ACTION_MAIN);
    i5.setComponent(new ComponentName("com.mojang.minecraftpe",  
"com.mojang.minecraftpe.MainActivity"));
    i5.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i5);  

}

}


}
这是我的下载XML第二页由于规则删除的所有链接:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/Downloads"
android:background="@drawable/black_background">



<ScrollView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="101dp"
    android:autoLink="web"
    android:linksClickable="true"
    android:text="MCPE 0.9.0 Build 5:
[Link Removed]"
    android:textSize="22dp"
    android:color="#008000" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="match_parent"
    android:layout_height="101dp"
    android:autoLink="web"
    android:linksClickable="true"
    android:color="#008000"
    android:text="MCPE 0.8.1:
[Link Removed]"
    android:textSize="22dp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="102dp"
    android:autoLink="web"
    android:linksClickable="true"
    android:text="BlockLauncher Pro 1.6.10:
[Link Removed]"
    android:textSize="22dp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:autoLink="web"
    android:linksClickable="true"
    android:text="PocketInvEditor Pro 1.11:
[Link Removed]"
    android:textSize="22dp" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:text="Mods for PE2 (Play Store Link):
[Link Removed]" 
    android:autoLink="web"
    android:linksClickable="true"
    android:textSize="22dp"/>

 <TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:text="Minecraft Skin Studio:
[Link Removed]"
    android:autoLink="web"
    android:linksClickable="true"
    android:textSize="22dp"/>

 <Button
     android:id="@+id/button5"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:text="Open MCPE!"
     android:autoLink="web"
     android:linksClickable="true"/>



</LinearLayout>
</ScrollView>
</LinearLayout>

定义打开xml页面?您的代码仅尝试在单击按钮时打开新活动。我现在的代码正在运行,但我需要做的是使其能够在不同的页面上添加按钮。所以我的主页上有1-4个按钮。但我想让我的下载页面也有按钮。显示下载页面的xml布局。您是否在下载片段/页面的xml布局中添加了按钮?我在原始帖子中添加了我最新的代码和下载页面。我在我的下载页面上设置了按钮,并将其设置为按钮5,因此我将其添加到我的代码中,现在应用程序正在崩溃。发布您的logcat输出。
06-18 14:51:31.200: E/AndroidRuntime(913): FATAL EXCEPTION: main
06-18 14:51:31.200: E/AndroidRuntime(913): Process: com.dogger20011.app, PID: 913
06-18 14:51:31.200: E/AndroidRuntime(913): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dogger20011.app/com.dogger20011.app.beforemain}: java.lang.NullPointerException
06-18 14:51:31.200: E/AndroidRuntime(913):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-18 14:51:31.200: E/AndroidRuntime(913):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-18 14:51:31.200: E/AndroidRuntime(913):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-18 14:51:31.200: E/AndroidRuntime(913):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-18 14:51:31.200: E/AndroidRuntime(913):  at android.os.Handler.dispatchMessage(Handler.java:102)
06-18 14:51:31.200: E/AndroidRuntime(913):  at android.os.Looper.loop(Looper.java:136)
06-18 14:51:31.200: E/AndroidRuntime(913):  at android.app.ActivityThread.main(ActivityThread.java:5017)
06-18 14:51:31.200: E/AndroidRuntime(913):  at java.lang.reflect.Method.invokeNative(Native Method)
06-18 14:51:31.200: E/AndroidRuntime(913):  at java.lang.reflect.Method.invoke(Method.java:515)
06-18 14:51:31.200: E/AndroidRuntime(913):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-18 14:51:31.200: E/AndroidRuntime(913):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-18 14:51:31.200: E/AndroidRuntime(913):  at dalvik.system.NativeStart.main(Native Method)
06-18 14:51:31.200: E/AndroidRuntime(913): Caused by: java.lang.NullPointerException
06-18 14:51:31.200: E/AndroidRuntime(913):  at com.dogger20011.app.beforemain.onCreate(beforemain.java:29)
06-18 14:51:31.200: E/AndroidRuntime(913):  at android.app.Activity.performCreate(Activity.java:5231)
06-18 14:51:31.200: E/AndroidRuntime(913):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-18 14:51:31.200: E/AndroidRuntime(913):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-18 14:51:31.200: E/AndroidRuntime(913):  ... 11 more