Java 使用Android按钮打开新类/活动
我在我的activity_main上创建了一个文本按钮,单击该按钮时,我希望打开一个名为gallery的新xml。我已经为此创建了一个xml和类。当我点击按钮时,它不会带我去任何地方 activity_main.xmlJava 使用Android按钮打开新类/活动,java,android,class,android-studio,Java,Android,Class,Android Studio,我在我的activity_main上创建了一个文本按钮,单击该按钮时,我希望打开一个名为gallery的新xml。我已经为此创建了一个xml和类。当我点击按钮时,它不会带我去任何地方 activity_main.xml <Button android:text="@string/Gallery" android:id="@+id/Button01" android:layout_width="200dp" android:textSize="28sp" an
<Button android:text="@string/Gallery"
android:id="@+id/Button01"
android:layout_width="200dp"
android:textSize="28sp"
android:background="@color/black"
android:textColor="@color/pink"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
</Button>
gallery.java
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class Gallery extends FragmentActivity {
ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
mPager = (ViewPager) findViewById(R.id.frame);
mPager.setOffscreenPageLimit(1);
mPager.setAdapter(new EndLessAdapter(this, mImageArray));
// Look up the AdView as a resource and load a request.
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
private int[] mImageArray = {R.drawable.y1,};
}
在两个活动的setContentView方法中,将R.layout.gallery替换为R.layout.Activity\u main并将view.getCotext()替换为Two。这
Intent myIntent=新的Intent(Two.this,Gallery.class) 您使用的按钮在
R.layout.activity\u main
中声明,而不是R.layout.gallery
。因此,当您在ActivityTow中使用时,它不会正确调用…尝试替换此代码:
您对两个活动的错误xml引用是Activity_main而不是gallery
public class Two extends Activity {
Button Button01;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Gallery.class);
startActivityForResult(myIntent, 0);
}
});
}
}
将其添加到第一个活动下的android清单中
<activity
android:name=".gallery" >
</activity>
我更改了对R.layout.activity\u main的引用,不幸的是,它仍然只是单击,但没有将我带到其他xml活动。谢谢,伙计,我尝试了这一点,但我仍然可以单击按钮,但什么都没有发生,它没有将我带到其他xml活动。您的代码还可以。我不知道还有什么问题
public class Two extends Activity {
Button Button01;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Gallery.class);
startActivityForResult(myIntent, 0);
}
});
}
}
<activity
android:name=".gallery" >
</activity>