删除参数以匹配 Intent()

Posted

技术标签:

【中文标题】删除参数以匹配 Intent()【英文标题】:Remove arguments to match Intent() 【发布时间】:2016-02-23 13:26:45 【问题描述】:

我正在尝试制作一个简单的安卓游戏,但我无法切换活动。我的 Intent() 代码出现错误:

构造函数 Intent(MenuView, Class) 未定义1 个快速修复可用:

我尝试了多种解决方案,也花了很多时间,但到目前为止还没有解决。尝试过的解决方案:

-android Remove arguments to match "intent()" -Android: Intent asks remove argument & getBaseContext() ask to create its method -StartActivity() red highlighted -error to startActivity(intent), whats going wrong?

package com.gregsapps.fallingbird;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.view.View;

public class MenuView extends View
private Context context;

        Paint text;
        Paint red;
        Paint pink;
        Rect playRect;
        boolean usePink = false;
        Rect tempPlayRect;
        //String playText = "PLAY";
        char[] playTextArray;
        int runs = 10;

        public MenuView(Context context) 
            super(context);
            this.context = context;
            this.setDrawingCacheEnabled(true);
            tempPlayRect = new Rect();
            playRect = new Rect();
            playTextArray = new char[4];
            playTextArray[0] = 'P';
            playTextArray[1] = 'L';
            playTextArray[2] = 'A';
            playTextArray[3] = 'Y';
            text = new Paint();
            text.setColor(Color.BLACK);
            text.setTextSize(100f);
            red = new Paint();
            red.setColor(Color.rgb(255, 0, 0));
            red.setTextSize(100f);
            pink = new Paint();
            pink.setColor(Color.rgb(255, 69, 0));
            System.out.println("setup");
        

        protected void onDraw(Canvas canvas)
            if(runs == 10) runOnce(canvas);
            System.out.println("Looping");
            playRect.offsetTo(canvas.getWidth()/2 - playRect.width()/2, canvas.getHeight()/3);
            //System.out.println(playRect.centerX());
            canvas.drawColor(Color.rgb(10, 255, 255));
            canvas.drawText("FallingBird", canvas.getWidth()/2, canvas.getHeight()/10, text);
            if(usePink) canvas.drawRect(playRect, pink);
            else canvas.drawRect(playRect, red);
            //canvas.drawText("PLAY", canvas.getWidth()/2-(playRect.width()-20)/2, canvas.getHeight()/5, text);
            canvas.drawText("PLAY", canvas.getWidth()/2, (int) (canvas.getHeight()/3 + (tempPlayRect.height()+canvas.getHeight()/20)), text);
            System.out.println(text.measureText("PLAY"));
            runs++;
            usePink = false;
            //if(StaticVarHandler.touch == false)
            //  StaticVarHandler.touchX = -1;
            //  StaticVarHandler.touchY = -1;
            //  System.out.println("not touched!");
            //
            System.out.println(StaticVarHandler.touch);
            if(playRect.contains(StaticVarHandler.touchX, StaticVarHandler.touchY))
                //usePink = true;
                context.startActivity(new Intent(this, Game.class));
                //startActivity(i);
            
            invalidate();
        

        private void runOnce(Canvas canvas) 
            text.setTextAlign(Align.CENTER);
            text.setTextSize(canvas.getWidth()/15);
            System.out.println("run once" + text.measureText("PLAY"));
            text.getTextBounds(playTextArray, 0, playTextArray.length, tempPlayRect);
            playRect.set(0, 0, (int) (text.measureText("PLAY")+canvas.getWidth()/20), tempPlayRect.height()+canvas.getWidth()/20);
            //playRect.top += 10;
            //playRect.bottom += 10;
            //playRect.left += 10;
            //playRect.right += 10;
            runs = 0;
        

错误位置:

            System.out.println(StaticVarHandler.touch);
            if(playRect.contains(StaticVarHandler.touchX, StaticVarHandler.touchY))
                //usePink = true;
                context.startActivity(new Intent(this, Game.class));
                //startActivity(i);
            

清单:

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="22" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Menu"
            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
            android:name=".Game"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

提前致谢

【问题讨论】:

【参考方案1】:

改变

context.startActivity(new Intent(this, Game.class));

context.startActivity(new Intent(context, Game.class));

this 在第一种情况下是指View 类,但Intent 构造函数需要Activity 上下文。

【讨论】:

完美运行!感谢您的快速重播!还是要等三分钟才能按接受!

以上是关于删除参数以匹配 Intent()的主要内容,如果未能解决你的问题,请参考以下文章

无法让 RKPathMatcher pathMatcher 匹配以删除孤儿对象

scala 模式匹配以删除某些情况

sed的基本用法

意图:删除始终/仅一次按钮

在 Android 中以编程方式从启动器中删除应用程序

[ jquery 方法 removeClass([class|fn]) ] 此方法为每个匹配的元素删除指定的类名