我制作了以下活动类来制作列表视图。
但是我在启动它的时候有NPE。 java文件和logcat如下:
活动类
public class CategoryListActivity extends Activity implements OnItemClickListener{
//URL to get JSON Array
private static String url = "http://code-explore.com/app/webroot/sp-codeexplore/getCategoryList.php?platform=Android";
//JSON Node Names
private static final String TAG_CATEGORY = "categories";
private static final String TAG_ID = "cat_id";
private static final String TAG_TITLE = "cat_title";
private static final String TAG_NO = "total_apps";
JSONArray categories = null;
public final static String CAT_ID = "com.smartmux.codeexplore.MESSAGE";
ArrayList<String> list = new ArrayList<String>();
ProgressDialog pDialog = new ProgressDialog(this);
ListView listView;
List<RowItem> rowItems = new ArrayList<RowItem>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.category);
new SM_AsyncTask().execute(url);
}
public void returnHome(View view) {
onBackPressed();
}
class SM_AsyncTask extends AsyncTask<String, Integer, List<RowItem>> {
ProgressDialog pDialog=null;
@Override
protected void onPreExecute() {
pDialog.setMessage("Please wait..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected List<RowItem> doInBackground(
String... params) {
// Creating new JSON Parser
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting JSON Array
categories = json.getJSONArray(TAG_CATEGORY);
for (int i = 0; i < categories.length(); i++) {
JSONObject c = categories.getJSONObject(i);
String title = c.getString(TAG_TITLE);
String number = c.getString(TAG_NO);
String id = c.getString(TAG_ID);
list.add(id);
RowItem item = new RowItem(title, number);
rowItems.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
return rowItems;
}
}
protected void onPostExecute(List<RowItem> result) {
pDialog.dismiss();
listView = (ListView) findViewById(R.id.listCategory);
ArrayAdapterForCategory adapter = new ArrayAdapterForCategory(this,
R.layout.row_cat_list, result);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String catId = list.get(position);
Intent intent = new Intent(CategoryListActivity.this, AppListActivity.class);
intent.putExtra(CAT_ID, catId);
startActivity(intent);
}
}
logcat的
12-03 08:36:59.370: E/AndroidRuntime(2082): FATAL EXCEPTION: main
12-03 08:36:59.370: E/AndroidRuntime(2082): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.smartmux.codeexplore/com.smartmux.codeexplore.CategoryListActivity}: java.lang.NullPointerException
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.os.Looper.loop(Looper.java:137)
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-03 08:36:59.370: E/AndroidRuntime(2082): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 08:36:59.370: E/AndroidRuntime(2082): at java.lang.reflect.Method.invoke(Method.java:525)
12-03 08:36:59.370: E/AndroidRuntime(2082): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-03 08:36:59.370: E/AndroidRuntime(2082): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-03 08:36:59.370: E/AndroidRuntime(2082): at dalvik.system.NativeStart.main(Native Method)
12-03 08:36:59.370: E/AndroidRuntime(2082): Caused by: java.lang.NullPointerException
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:146)
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103)
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.app.AlertDialog.<init>(AlertDialog.java:98)
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
12-03 08:36:59.370: E/AndroidRuntime(2082): at com.smartmux.codeexplore.CategoryListActivity.<init>(CategoryListActivity.java:39)
12-03 08:36:59.370: E/AndroidRuntime(2082): at java.lang.Class.newInstanceImpl(Native Method)
12-03 08:36:59.370: E/AndroidRuntime(2082): at java.lang.Class.newInstance(Class.java:1130)
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
12-03 08:36:59.370: E/AndroidRuntime(2082): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
12-03 08:36:59.370: E/AndroidRuntime(2082): ... 11 more
基本上,调用AsyncTask
时会出现问题。
答案 0 :(得分:3)
更改为
ProgressDialog pDialog;
ListView listView;
List<RowItem> rowItems = new ArrayList<RowItem>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.category);
pDialog = new ProgressDialog(this); // initialize in onCreate
删除
ProgressDialog pDialog=null; // already declared
在Asynctask中
另外
@Override
protected void onPreExecute() {
super.onPreExcute()
和
@Override
protected void onPostExecute(List<RowItem> result) {
super.onPostExecute(result);
listView = (ListView) findViewById(R.id.listCategory);
ArrayAdapterForCategory adapter = new ArrayAdapterForCategory(CategoryListActivity.this,
R.layout.row_cat_list, result);
listView.setAdapter(adapter);
listView.setOnItemClickListener(CategoryListActivity.this);
}
更改此
return rowItems;
}
}
要
return rowItems;
}
也改变。你错放了}
startActivity(intent);
}
}
要
startActivity(intent);
}
}
}
答案 1 :(得分:1)
在Asynctask中删除此行:
ProgressDialog pDialog = null;或者
之后的init进度对话框