0

I read this tutorial and using code from it to implement ScoreLoop service in my Game. And submiting score working fine, but show other android activity or simple Toast message are crashes with NullPointer error.

Crashes in line:

Intent intent = new Intent(mContext, EntryScreenActivity.class);

and in:

Toast.makeText(mContext, "Refreshing scores", Toast.LENGTH_SHORT).show(); 

In debug mode i saw that mContext is not null

Where is error and how fix it?

LogCat:

08-22 17:54:27.329: E/AndroidRuntime(15468): java.lang.NullPointerException 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.content.ContextWrapper.getResources(ContextWrapper.java:81) 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.widget.Toast.<init>(Toast.java:92) 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.widget.Toast.makeText(Toast.java:233) 08-22 17:54:27.329: E/AndroidRuntime(15468): at com.masterofcode.android.slingshot.ActionResolverAndroid$3.run(ActionResolverAndroid.java:46) 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.os.Handler.handleCallback(Handler.java:605) 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.os.Handler.dispatchMessage(Handler.java:92) 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.os.Looper.loop(Looper.java:137) 08-22 17:54:27.329: E/AndroidRuntime(15468): at android.app.ActivityThread.main(ActivityThread.java:4424) 08-22 17:54:27.329: E/AndroidRuntime(15468): at java.lang.reflect.Method.invokeNative(Native Method) 08-22 17:54:27.329: E/AndroidRuntime(15468): at java.lang.reflect.Method.invoke(Method.java:511) 08-22 17:54:27.329: E/AndroidRuntime(15468): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 08-22 17:54:27.329: E/AndroidRuntime(15468): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 08-22 17:54:27.329: E/AndroidRuntime(15468): at dalvik.system.NativeStart.main(Native Method)

========================================================================

public class ActionResolverAndroid implements ActionResolver {
  Handler uiThread;
  BumbleAndroid mContext; //Your class here which extends AndroidApplication
  ScoreloopHandler handler;

  public ActionResolverAndroid(BumbleAndroid mContext) {
      uiThread = new Handler(); //This binds the handler to the "main" thread, see documentation of handler
      this.mContext = mContext;
      handler = new ScoreloopHandler(mContext);
  }
}

In this way I getting mContext

Igor Kostenko
  • 2,754
  • 7
  • 30
  • 57

2 Answers2

1

This question: Null at android.content.ContextWrapper.getResources(ContextWrapper.java:80) implies that getResources will return null when invoked on the wrong context (it seems you need the context of the current Activity to make Toast).

Here's another one with similar implications: Difference between Activity Context and Application Context

Would be nice to find some more concrete documentation about this, but I haven't found it ...

Community
  • 1
  • 1
P.T.
  • 24,557
  • 7
  • 64
  • 95
0

The problem was in wrong mContext.

correct code is:

public class MyGameAndroidActivity extends AndroidApplication implements
        ActionResolver {
public void showScoreloop() {
        Intent intent = new Intent(this, EntryScreenActivity.class);
        this.startActivity(intent);
    }
}
Igor Kostenko
  • 2,754
  • 7
  • 30
  • 57