简体   繁体   English

NullPointerException用于Android ContextWrapper

[英]NullPointerException for android ContextWrapper

I have a class (DataSource.java) containing a method, which is calling another method in my MainActivity, which receives a context and a string variable. 我有一个包含方法的类(DataSource.java),该方法正在MainActivity中调用另一个方法,该方法接收上下文和字符串变量。

In the Datasource.java, when calling the method from the MainActivity, I have set the context variable to null, and even if I set it to "context" I receive the same error as I launch the app: EXCEPTION error for this line: context = getApplicationContext(); 在Datasource.java中,当从MainActivity调用方法时,我已将上下文变量设置为null,即使将其设置为“ context”,我也会收到与启动应用程序时相同的错误:此行的EXCEPTION错误: context = getApplicationContext(); .

Could someone suggest me, where am I wrong? 有人可以建议我,我哪里错了?

DataSource.java: DataSource.java:

public class ToursDataSource {

 public boolean addToFollowList(Tour tour) {
    ContentValues values = new ContentValues();
    values.put(ToursDBOpenHelper.COLUMN_IMAGE, tour.getTitle());                
    long result = database.insert(ToursDBOpenHelper.TABLE_FOLLOW, null, values);

    MainActivity orgName = new MainActivity();
    orgName.sendRegId(this, "Test string");

    return null;        
 }
}

MainActivity: 主要活动:

Context context;
    ...

        public void sendRegId(Context context, String organization) {

                 //PROBLEM IS AT THIS LINE
                context = getApplicationContext();

                // Check device for Play Services APK. If check succeeds, proceed with
                //  GCM registration.
                if (checkPlayServices()) {
                    gcm = GoogleCloudMessaging.getInstance(this);
                    regid = getRegistrationId(context);
                    Log.i(TAG, "Registration started!");
                    Log.d("RegisterActivity", "GCM RegId is: " + regid);           

                    if (regid.isEmpty()) {

                      registerInBackground();
                    }
                } else {
                    Log.i(TAG, "No valid Google Play Services APK found.");
                }

                Sender results = new Sender();
                results.shareRegIdWithAppServer(regid, organization);


            }


        ...

Logcat: logcat的:

04-16 15:42:07.765: E/AndroidRuntime(15610): FATAL EXCEPTION: main
04-16 15:42:07.765: E/AndroidRuntime(15610): java.lang.NullPointerException
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.exploreca.tourfinder.MainActivity.sendRegId(MainActivity.java:189)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.exploreca.tourfinder.ToursDataSource.addToFollowList(ToursDataSource.java:205)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.exploreca.tourfinder.TourDetailActivity.onOptionsItemSelected(TourDetailActivity.java:129)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.app.Activity.onMenuItemSelected(Activity.java:2640)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1171)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:630)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:200)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.view.View.performClick(View.java:4475)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.view.View$PerformClick.run(View.java:18786)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.os.Handler.handleCallback(Handler.java:730)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.os.Looper.loop(Looper.java:137)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at android.app.ActivityThread.main(ActivityThread.java:5493)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at java.lang.reflect.Method.invoke(Method.java:525)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
04-16 15:42:07.765: E/AndroidRuntime(15610):    at dalvik.system.NativeStart.main(Native Method)

try to use this: 尝试使用此:

static Context = mConext;

i think this works. 我认为这可行。

Make change like this: 进行如下更改:

context = getApplicationContext();

to

this.context = context;

I guess MainActivity is a Activity class. 我猜MainActivity是一个Activity类。 You cannot instantiate a Activity class. 您不能实例化一个Activity类。 You declare Activities in manifest file. 您在清单文件中声明活动。 Each Activity has a lifecycle. 每个活动都有生命周期。 You cannot treat Activity class a normal java class. 您不能将Activity类视为普通的Java类。

To know the reason Read raghav sood's answer @ 知道原因阅读raghav sood的答案@

Can i Create the object of a activity in other class? 我可以在其他课程中创建活动的对象吗?

Quoting Raghav's Sood 引用拉加夫的肥皂

By treating an Activity as a normal Java class, you end up with a null context. 通过将Activity视为普通的Java类,最终得到的是空上下文。 As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes. 由于Activity中的大多数方法都是在其Context上调用的,因此您将获得空指针异常,这就是您的应用程序崩溃的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android.content.contextwrapper.getsystemservice上的java.lang.nullpointerexception(Contextwrapper.java:386) - java.lang.nullpointerexception at android.content.contextwrapper.getsystemservice(Contextwrapper.java:386) 将数据写入内部文件 - ContextWrapper NullPointerException - Writing Data to Internal File - ContextWrapper NullPointerException ContextWrapper.getFilesDir()中出现NullPointerException的原因 - Reasons for NullPointerException from ContextWrapper.getFilesDir() BackupManager.dataChanged中ContextWrapper.getPackageName上的NullPointerException - NullPointerException on ContextWrapper.getPackageName in BackupManager.dataChanged 使用SQLiteOpenHelper时ContextWrapper.openOrCreateDatabase中的NullPointerException - NullPointerException in ContextWrapper.openOrCreateDatabase when using SQLiteOpenHelper Android Studio 中的 ContextWrapper 和 AssetManager 错误 - ContextWrapper and AssetManager Errors in Android Studio Android ContextWrapper fileList()看不到我的文件 - Android ContextWrapper fileList() doesnt see my files 哪个类在Android中实现ContextWrapper的方法? - Which class is implementing ContextWrapper's methods in Android? 空点异常错误-android.content.ContextWrapper.startService - Null point exception error - android.content.ContextWrapper.startService android.content.contextwrapper.getResources上的Null指针异常 - Null Pointer Exception at android.content.contextwrapper.getResources
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM