English 中文(简体)
java.lang.ClassCastException:android.app.ContextImpl
原标题:java.lang.ClassCastException: android.app.ContextImpl

我正试图将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");

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

最佳回答

如果您查看ScoreInjaAdapter的代码此处,它显示它将上下文强制转换为Activity。请尝试使用this而不是getBaseContext,因为您是从活动调用的。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签