简体   繁体   English

使用Android按钮打开新的课程/活动

[英]Using Android button to open new class/activity

I have created a text button on my activity_main that when clicked I want to open a new xml called gallery. 我在activity_main上创建了一个文本按钮,单击该按钮时,我想打开一个新的XML,即Gallery。 I have created an xml and class for this. 我为此创建了一个xml和类。 When I click the button it doesn't take me anywhere. 当我单击按钮时,它不会带我到任何地方。

activity_main.xml activity_main.xml

<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>

android manifest Android清单

  <activity android:name=".Gallery"
        android:label="@string/app_name" />

two.java 二.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.widget.Button;

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.gallery);

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

    });
}
}

gallery.java 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,};


}

In the setContentView method of your Two Activity replace R.layout.gallery with R.layout.activity_main and replace view.getCotext() with two.this 在“两个活动”的setContentView方法中,将R.layout.gallery替换为R.layout.activity_main ,并将view.getCotext()替换为two.this
Intent myIntent = new Intent( Two.this , Gallery.class); Intent myIntent = new Intent( Two.this ,Gallery.class);

The button you used is declared in R.layout.activity_main not R.layout.gallery . 您所使用的按钮被宣布R.layout.activity_mainR.layout.gallery So when you use in Activity Tow , it is not invoked rightly... 因此,当您在Activity Tow使用时,它不会被正确调用...

Try to replace this code : 尝试替换此代码:

Your given wrong xml reference to yor Two Activity is activity_main instead of gallery. 您给您的两个活动指定的错误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);
        }

    });
}
}

add this to the android manifest under the first activity 将此添加到第一个活动下的android清单中

<activity
        android:name=".gallery" >
</activity>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM