-3
我正在嘗試從我的reg.java
類調用函數registerGCM();
到類MainActivity.java
。嘗試調用另一個類的函數時發生NullPoint錯誤
當我使用功能從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;
}
任何想法爲什麼我無法調用此函數?
NullPointerException:http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – dotvav
對象GCM_REG沒有初始化我猜。請檢查你的代碼。 –