我可以在Android中setContentView(R.layout.activity_main);
之前以编程方式初始化ImageView或任何其他视图吗?
实际上我正在获取自定义类的实例(不是活动)
在全局onCreate方法之前
像这样
MyInAppPurchaseBilling myInAppPurchaseBilling = new MyInAppPurchaseBilling(MenuClass.this);
在该类构造函数中,我必须使用该上下文来初始化我的首选项类
public MyInAppPurchaseBilling(Activity launcher) {
this.activity = launcher;
tinyDB= new TinyDB(launcher);
}
这是我面临的错误日志
07-05 17:22:01.852 9660-9660/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.write.urdu.poetry.urdutexteditor, PID: 9660
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.write.urdu.poetry.urdutexteditor/com.write.urdu.poetry.urdutexteditor.MenuClass}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5551)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:133)
at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:375)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:370)
at com.write.urdu.poetry.urdutexteditor.TinyDB.<init>(TinyDB.java:55)
at com.write.urdu.poetry.urdutexteditor.inapppurchase.MyInAppPurchaseBilling.<init>(MyInAppPurchaseBilling.java:48)
at com.write.urdu.poetry.urdutexteditor.MenuClass.<init>(MenuClass.java:38)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5551)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
最佳答案
在仍执行MenuClass.this对象的构造函数的同时创建了MyInAppPurchaseBilling
对象吗?在这种情况下(可能直到在MenuClass.this上调用onCreate()
为止,但我不确定),尚未设置Activity对象(在这种情况下称为MenuClass.this)内部的上下文。