我正在尝试将registerGCM();
类中的函数reg.java
调用到MainActivity.java
类中。
当我在reg.java
活动中使用该功能时,该应用程序工作正常,但每当我从MainActivity
内部调用它时,我都会收到此错误
07-28 13:38:38.540 24434-24434/com.example.kmi_dev.fbloginsample E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.kmi_dev.fbloginsample, PID: 24434
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kmi_dev.fbloginsample/com.example.kmi_dev.fbloginsample.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.kmi_dev.fbloginsample.reg.registerGCM()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.kmi_dev.fbloginsample.reg.registerGCM()' on a null object reference
at com.example.kmi_dev.fbloginsample.MainActivity.onCreate(MainActivity.java:158)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
正在进行通话的MainActivity的内容
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private static final String MYPREFERENCES = "myPref";
private CallbackManager callbackManager;
private LoginButton fbLoginButton;
private Button login;
.
.
.
.
GPSTracker gps;
reg GCM_REG;
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
SessionManager session;
.
.
String gcmId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get global variable class
final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
gcmId = GCM_REG.registerGCM();
内容registerGCM()
public String registerGCM() {
gcm = GoogleCloudMessaging.getInstance(this);
regId = getRegistrationId(context);
if (TextUtils.isEmpty(regId)) {
registerInBackground();
Log.d("MainActivity",
"registerGCM - successfully registered with GCM server - regId: "
+ regId);
} else {
Toast.makeText(getApplicationContext(),
"RegId already available. RegId: " + regId,
Toast.LENGTH_LONG).show();
}
final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
globalVariable.setGcmRegId(regId);
return regId;
}
public String getRegistrationId(Context context) {
final SharedPreferences prefs = getSharedPreferences(reg.class.getSimpleName(), Context.MODE_PRIVATE);
String registrationId = prefs.getString(REG_ID, "");
if (registrationId.isEmpty()) {
Log.i(TAG, "Registration not found.");
return "";
}
int registeredVersion = prefs.getInt(APP_VERSION, Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
Log.i(TAG, "App version changed.");
return "";
}
return registrationId;
}
任何想法为什么我无法调用此函数???
答案 0 :(得分:6)
您永远不会初始化GCM_REG
。这就是你得到NullPointerException的原因。