从片段上的按钮打开新活动

时间:2016-03-17 02:44:34

标签: java android android-fragments

我正在制作纸牌游戏,使用片段翻转卡片。单击某个按钮时,我想打开一个新活动。 我已经尝试在意图中使用this运算符但是程序没有在哪里。另外,我尝试过使用getactivity();然而,在意图中,它标有红色并且不会运行。

我正在开展新活动的班级: (注意此活动中还有其他方法。变量donedone在不同方法中设置为1)

public class Card extends FragmentActivity {

InterstitialAd mInterstitialAd;
int n;
int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;
int t,p;
int bb=0,cc=0,dd=0,ee=0,zz=0;
int ww=0;
int donedone=0;





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vv);

    rulepick();

    //Ad Stuff
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    requestNewInterstitial();

    mInterstitialAd.setAdListener(new AdListener() {
        @Override

        public void onAdClosed() {
            Button nextcard = (Button) findViewById(R.id.checkbutton);
            requestNewInterstitial();
            //ShowButton
            nextcard.setVisibility(View.VISIBLE);
        }
    });


    Button mClickButton1 = (Button) findViewById(R.id.checkbutton1);
    Button mClickButton2 = (Button) findViewById(R.id.checkbutton);

    mClickButton1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.checkbutton1: {
                    // do something for button 1 click

                    rulepick();
                    cardflip();
                    //if(donedone==1){
                    //Intent myIntent = new Intent(this,LastActivity.class);
                    //  startActivity(myIntent);
                    //}
                    break;
                }
            }
        }

    });

    mClickButton2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.checkbutton: {
                    // do something for button 1 click

                    rulepick1();
                    cardflip1();
                    //if(donedone==1){
                //  Intent myIntent = new Intent(this, LastActivity.class);
                        //startActivity(myIntent);
                    //}
                    break;
                }
            }
        }

    });


    //Add request stuff
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

}

我要去的活动:     public class LastActivity扩展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lastactivity);
}

}

清单文件:          

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

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".LastActivity"/>
    <activity android:name=".Card" />


</application>

</manifest>

我做错了什么? 谢谢

1 个答案:

答案 0 :(得分:0)

OnClickListener内,您需要限定this,因此它是封闭式活动而不是听众本身:

mClickButton1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.checkbutton1: {
                // do something for button 1 click

                rulepick();
                cardflip();
                if(donedone==1){
                  // Note use of Card.this instead of just this:
                  Intent myIntent = new Intent(Card.this,LastActivity.class);
                  startActivity(myIntent);
                }
                break;
            }
        }
    }

});