我想在Android.But中获取Android应用程序安装日期,当我运行应用程序获取NullPointerException时。有人帮助我。
这是我的代码
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_details);
try
{ long installed =
context.getPackageManager().getPackageInfo("package.com.sqlitedemo",
PackageManager.GET_META_DATA).firstInstallTime;
Log.e("installed = "," " + installed);
}
catch (NameNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这是我的Log Cat错误
08-06 11:40:34.758: E/AndroidRuntime(335): FATAL EXCEPTION: main
08-06 11:40:34.758: E/AndroidRuntime(335): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sqlitedemo/com.sqlitedemo.Registration_Form}: java.lang.NullPointerException
08-06 11:40:34.758: E/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-06 11:40:34.758: E/AndroidRuntime(335): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-06 11:40:34.758: E/AndroidRuntime(335): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-06 11:40:34.758: E/AndroidRuntime(335): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-06 11:40:34.758: E/AndroidRuntime(335): at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 11:40:34.758: E/AndroidRuntime(335): at android.os.Looper.loop(Looper.java:123)
08-06 11:40:34.758: E/AndroidRuntime(335): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-06 11:40:34.758: E/AndroidRuntime(335): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 11:40:34.758: E/AndroidRuntime(335): at java.lang.reflect.Method.invoke(Method.java:507)
08-06 11:40:34.758: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-06 11:40:34.758: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-06 11:40:34.758: E/AndroidRuntime(335): at dalvik.system.NativeStart.main(Native Method)
08-06 11:40:34.758: E/AndroidRuntime(335): Caused by: java.lang.NullPointerException
08-06 11:40:34.758: E/AndroidRuntime(335): at com.sqlitedemo.Registration_Form.onCreate(Registration_Form.java:61)
08-06 11:40:34.758: E/AndroidRuntime(335): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-06 11:40:34.758: E/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-06 11:40:34.758: E/AndroidRuntime(335): ... 11 more
感谢您的光临。
答案 0 :(得分:3)
您确定需要吗
context.getPackageManager().getPackageInfo("package.com.sqlitedemo"
而不是
context.getPackageManager().getPackageInfo("com.sqlitedemo"
答案 1 :(得分:1)
您可以在启动或主要活动中使用日历实例,并将其保存在共享首选项中,如下所示 -
SharedPreference shared=getSharedPreferences(Preference.KEY_PREF,MODE_PRIVATE);
Editor edit=shared.edit();
if(shared.getBoolean(Preference.KEY_FIRST_TIME, false)==false){
edit.putBoolean(Preference.KEY_FIRST_TIME,true );
Calendar first=Calendar.getInstance();
edit.putLong(Preference.KEY_FIRST_DATE, first.getTimeInMillis());
这将在您的应用首次运行时存储日期,时间等
答案 2 :(得分:0)
您尚未初始化context
,因此返回null。
将您的代码更改为:
setContentView(R.layout.registration_details);
context = this;
try
{
long installed = context.getPackageManager().getPackageInfo("package.com.sqlitedemo",
PackageManager.GET_META_DATA).firstInstallTime;
Log.e("installed = "," " + installed);
}
因此,您的上下文将不再返回null。
答案 3 :(得分:0)
我认为你的上下文是空的..
Context context;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_details);
context=getBaseContext();
try
{ long installed =
context.getPackageManager().getPackageInfo("package.com.sqlitedemo",
PackageManager.GET_META_DATA).firstInstallTime;
Log.e("installed = "," " + installed);
}
catch (NameNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
可能有帮助
此致