java.lang.ClassCastException:android.app.ContextImpl

发布于 2024-11-09 16:54:41 字数 5403 浏览 0 评论 0原文

我正在尝试将 ScoreNinja 合并到我的小游戏中: http://scoreninja.appspot.com/

但是,每次都会抛出运行时异常:

05-24 23:22:59.888: ERROR/AndroidRuntime(21237): FATAL EXCEPTION: main
05-24 23:22:59.888: ERROR/AndroidRuntime(21237): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shaq.pushcounter/com.shaq.pushcounter.ChickenPushupTimer}: java.lang.ClassCastException: android.app.ContextImpl
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.os.Looper.loop(Looper.java:123)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at java.lang.reflect.Method.invokeNative(Native Method)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at java.lang.reflect.Method.invoke(Method.java:507)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at dalvik.system.NativeStart.main(Native Method)


05-24 23:22:59.888: ERROR/AndroidRuntime(21237): Caused by: java.lang.ClassCastException: android.app.ContextImpl
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at com.scoreninja.adapter.ScoreNinjaAdapter.<init>(ScoreNinjaAdapter.java:85)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at com.shaq.pushcounter.ChickenPushupTimer.onCreate(ChickenPushupTimer.java:31)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     ... 11 more

问题可能是ChickenPushupTimer.java:31。

这是我的整个 Java 文件:

public class ChickenPushupTimer extends Activity
{
    int count = 0;
    TextView timeLeft;
    TextView totalPushups;
    ImageButton button;
    SoundManager mSoundManager;
    Vibrator myVib;
    ScoreNinjaAdapter scoreNinjaAdapter;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chickentimer);
        scoreNinjaAdapter = new ScoreNinjaAdapter(getBaseContext(), "cockpushups", "71A7AD115870BA0E05C13BE2B9D3F984");

        myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(1, R.raw.chicken);

        timeLeft = (TextView) findViewById(R.id.timeLeft);
        totalPushups = (TextView) findViewById(R.id.totalPushups);
        button = (ImageButton) findViewById(R.id.chickenbutton);

        countdownTimer();

        button.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v)
            {
                myVib.vibrate(250);
                count++;
                totalPushups.setText("" + count);
                mSoundManager.playSound(1);
            }
        });
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.popupmenu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.goback:
            /*Intent myIntent = new Intent(getBaseContext(), MainMenu.class);
            startActivity(myIntent);*/
            finish();
            return true;
     //   case R.id.help:
     //       showHelp();
     //       return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public void countdownTimer()
    {
        new CountDownTimer(16000, 1000) {

             public void onTick(long millisUntilFinished) {
                 timeLeft.setText("" + millisUntilFinished / 1000);
             }

             public void onFinish() {
                 scoreNinjaAdapter.show(count);
                 finish();
             }
          }.start();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        scoreNinjaAdapter.onActivityResult(requestCode, resultCode, data);
    }
}

这是第 31 行:

scoreNinjaAdapter = new ScoreNinjaAdapter(getBaseContext(), "cockpushups", "71A7AD115870BA0E05C13BE2B9D3F984");

我使用的上下文可能有问题。任何帮助将不胜感激!

I am trying to incorporate ScoreNinja into my little game: http://scoreninja.appspot.com/

However, a runtime exception is thrown every time:

05-24 23:22:59.888: ERROR/AndroidRuntime(21237): FATAL EXCEPTION: main
05-24 23:22:59.888: ERROR/AndroidRuntime(21237): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shaq.pushcounter/com.shaq.pushcounter.ChickenPushupTimer}: java.lang.ClassCastException: android.app.ContextImpl
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.os.Looper.loop(Looper.java:123)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at java.lang.reflect.Method.invokeNative(Native Method)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at java.lang.reflect.Method.invoke(Method.java:507)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at dalvik.system.NativeStart.main(Native Method)


05-24 23:22:59.888: ERROR/AndroidRuntime(21237): Caused by: java.lang.ClassCastException: android.app.ContextImpl
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at com.scoreninja.adapter.ScoreNinjaAdapter.<init>(ScoreNinjaAdapter.java:85)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at com.shaq.pushcounter.ChickenPushupTimer.onCreate(ChickenPushupTimer.java:31)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-24 23:22:59.888: ERROR/AndroidRuntime(21237):     ... 11 more

The problem is probably ChickenPushupTimer.java:31.

Here is my entire Java file:

public class ChickenPushupTimer extends Activity
{
    int count = 0;
    TextView timeLeft;
    TextView totalPushups;
    ImageButton button;
    SoundManager mSoundManager;
    Vibrator myVib;
    ScoreNinjaAdapter scoreNinjaAdapter;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chickentimer);
        scoreNinjaAdapter = new ScoreNinjaAdapter(getBaseContext(), "cockpushups", "71A7AD115870BA0E05C13BE2B9D3F984");

        myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
        mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(1, R.raw.chicken);

        timeLeft = (TextView) findViewById(R.id.timeLeft);
        totalPushups = (TextView) findViewById(R.id.totalPushups);
        button = (ImageButton) findViewById(R.id.chickenbutton);

        countdownTimer();

        button.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v)
            {
                myVib.vibrate(250);
                count++;
                totalPushups.setText("" + count);
                mSoundManager.playSound(1);
            }
        });
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.popupmenu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.goback:
            /*Intent myIntent = new Intent(getBaseContext(), MainMenu.class);
            startActivity(myIntent);*/
            finish();
            return true;
     //   case R.id.help:
     //       showHelp();
     //       return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public void countdownTimer()
    {
        new CountDownTimer(16000, 1000) {

             public void onTick(long millisUntilFinished) {
                 timeLeft.setText("" + millisUntilFinished / 1000);
             }

             public void onFinish() {
                 scoreNinjaAdapter.show(count);
                 finish();
             }
          }.start();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        scoreNinjaAdapter.onActivityResult(requestCode, resultCode, data);
    }
}

This is line 31:

scoreNinjaAdapter = new ScoreNinjaAdapter(getBaseContext(), "cockpushups", "71A7AD115870BA0E05C13BE2B9D3F984");

There may be something wrong with the context I am using. Any help would be GREATLY appreciated!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

司马昭之心 2024-11-16 16:54:41

如果您查看 ScoreNinjaAdapter 这里,它表明它将上下文转换为Activity。尝试使用 this 而不是 getBaseContext,因为您是从 Activity 进行调用。

If you look at the code for ScoreNinjaAdapter here, it shows that it casts the context to Activity. Try using this instead of getBaseContext, since you're calling from an activity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文