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

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

构造函数 Intent(MenuView, Class) 未定义
1 个快速修复可用:
删除参数以匹配 Intent()

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

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

提前致谢

【问题讨论】:

    标签: java android eclipse android-intent


    【解决方案1】:

    改变

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

    到

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

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

    【讨论】:

    • 完美运行!感谢您的快速重播!还是要等三分钟才能按接受!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 2015-05-11
    • 2018-01-01
    相关资源
    最近更新 更多