我在android中实现Collection时遇到了一些问题。代码如下。我比较了在警告对话框中选择的位置以及相应的调用集合及其相关功能。但似乎存在问题,并且始终发生java.lang NullPointerException。我在Applciation Start-Up Activity的OnCreate方法中调用这些Collection函数。
private void onStartPref() {
// TODO Auto-generated method stub
// try {
SharedPreferences pref2 = getApplication().getSharedPreferences(
"MyPref", MODE_PRIVATE);
int loadPosition = pref2.getInt("MyKey", isChecked);
Toast.makeText(getApplicationContext(), "Position : " + loadPosition,
Toast.LENGTH_LONG).show();
if (pref2.contains("MyKey")) {
Toast.makeText(getApplicationContext(), "IF" + loadPosition,
Toast.LENGTH_LONG).show();
if (loadPosition == 0) {
Collections.reverse(applist);
Toast.makeText(getApplicationContext(),
"Reverses the present order list", Toast.LENGTH_SHORT)
.show();
} else if (loadPosition == 1) {
Collections.sort(applist,
new ApplicationInfo.DisplayNameComparator(
packageManager));
Toast.makeText(getApplicationContext(), "Sorts Alphabetically",
Toast.LENGTH_SHORT).show();
} else if (loadPosition == 2) {
Collections.shuffle(applist);
Toast.makeText(getApplicationContext(),
"Shuffles the present order selected",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "NO PREF" + loadPosition,
Toast.LENGTH_LONG).show();
}
// } catch (Exception e) {
// // TODO: handle exception
// Toast.makeText(getApplicationContext(), "ERROR : " + e,
// Toast.LENGTH_LONG).show();
// Log.i("ERROR", "" + e);
// e.printStackTrace();
// }
}
我正在倒车,改组,排序我的列表顺序,但应用程序部队关闭。任何人都可以帮我解决这个例外吗?任何人都可以告诉我如何在这些更改后动态加载列表而无需重新加载活动或滚动列表? 谢谢
LOG -----------------
01-26 01:29:15.872: I/ERROR(26192): java.lang.NullPointerException
01-26 01:29:15.872: W/System.err(26192): java.lang.NullPointerException
01-26 01:29:15.882: W/System.err(26192): at java.util.Collections.reverse(Collections.java:1719)
01-26 01:29:15.882: W/System.err(26192): at com.example.allapps.AllAppsActivity.onStartPref(AllAppsActivity.java:462)
01-26 01:29:15.882: W/System.err(26192): at com.example.allpps.AllAppsActivity.onCreate(AllAppsActivity.java:252)
01-26 01:29:15.882: W/System.err(26192): at android.app.Activity.performCreate(Activity.java:5283)
01-26 01:29:15.882: W/System.err(26192): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-26 01:29:15.882: W/System.err(26192): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
01-26 01:29:15.882: W/System.err(26192): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
01-26 01:29:15.882: W/System.err(26192): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-26 01:29:15.882: W/System.err(26192): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-26 01:29:15.882: W/System.err(26192): at android.os.Handler.dispatchMessage(Handler.java:102)
01-26 01:29:15.882: W/System.err(26192): at android.os.Looper.loop(Looper.java:136)
01-26 01:29:15.882: W/System.err(26192): at android.app.ActivityThread.main(ActivityThread.java:5001)
01-26 01:29:15.882: W/System.err(26192): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 01:29:15.882: W/System.err(26192): at java.lang.reflect.Method.invoke(Method.java:515)
01-26 01:29:15.882: W/System.err(26192): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-26 01:29:15.882: W/System.err(26192): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-26 01:29:15.882: W/System.err(26192): at dalvik.system.NativeStart.main(Native Method)
我使用OnCreate中的方法
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
packageManager = getPackageManager();
new LoadApplications().execute();
onStartPref();
}
LoadApplication是关于applist的主要方法。我在OnCreate方法中使用它,方法函数如下。
private class LoadApplications extends AsyncTask<Void, Void, Void> {
private ProgressDialog progress = null;
@Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(packageManager
.getInstalledApplications(PackageManager.GET_META_DATA));
listadaptor = new ApplicationAdapter(AllAppsActivity.this,
R.layout.snippet_list_row, applist);
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(Void result) {
setListAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(AllAppsActivity.this, null,
"Loading Please Wait...");
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
答案 0 :(得分:0)
您似乎在AsyncTask的doInBackground方法中初始化applist。即无法保证当您调用onStartPref时,它将被初始化。
我建议在onPostExecute中调用onStartPref。