我可以在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);
}
这是我面临的错误logcat
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)
答案 0 :(得分:1)
在执行MenuClass.this对象的构造函数时是否创建了MyInAppPurchaseBilling
对象?在这种情况下(可能直到在MenuClass.this上调用onCreate()
,但我不确定这一点)Activity对象内部的上下文(在这种情况下表示MenuClass.this)尚未设置。
答案 1 :(得分:0)
你要做的是
#include <QApplication>
#include <QDialog>
#include <QVBoxLayout>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDialog *_waitQD = new QDialog;
QVBoxLayout *_waitQVBL = new QVBoxLayout;
QLabel *_waitQL = new QLabel("Loading...");
_waitQD->setMinimumSize(QSize(95,35));
_waitQVBL->addWidget(_waitQL);
_waitQD->setLayout(_waitQVBL);
_waitQD->show();
return app.exec();
}
答案 2 :(得分:0)
试试这个:
View view = View.inflate(this, R.layout.activity_main, null); // view Exists after this code
TextView t = (TextView) view.findViewById(R.id.text);
setContentView(view);
t.setText("something");
在这里,您可以看到在findViewById()
之前调用setContentView()
。但是视图存在是因为它被夸大了但它可能对你有帮助。
答案 3 :(得分:0)
这是我的MenuClass Activity代码我在setContentView()之前在MenuClass onCreate()方法中初始化TinyDB,并在从偏好类中获取保存的布尔值后完成我的工作
public class MenuClass extends Activity {
TinyDB tinyDB;
MyInAppPurchaseBilling myInAppPurchaseBilling = new MyInAppPurchaseBilling(MenuClass.this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myInAppPurchaseBilling.onCreate();
tinyDB= new TinyDB(MenuClass.this);
if (!tinyDB.getBoolean(Constant.isAdsDisabled)) {
//do your work
}
setContentView(R.layout.activity_menu_class);
}
}
这是我的MyInAppPurchaseBilling类的代码我在其构造函数中什么也没做,但创建了onCreate的单独方法,就像activity的onCreate一样,并在其中初始化TinyDB,并在超级后调用MenuClass onCreate中此类的onCreate方法。*** line ...这样我成功地在MyInAppPurchaseBilling类中初始化TinyDB,将boolean值放在app的开头,然后在Menuclass中获取该布尔值以进一步处理
public class MyInAppPurchaseBilling {
private TinyDB tinyDB;
Activity activity;
public MyInAppPurchaseBilling(Activity launcher) {
this.activity = launcher;
}
public void onCreate() {
tinyDB= new TinyDB(activity);
}
}