android中异步任务期间的NullPointerException
我制作了下面的活动类来制作listview 但我在启动它的时候得到了NPE。java文件和logcat如下所示: 活动课android中异步任务期间的NullPointerException,android,android-asynctask,nullpointerexception,Android,Android Asynctask,Nullpointerexception,我制作了下面的活动类来制作listview 但我在启动它的时候得到了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-codee
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);
}
}
公共类CategoryStactivity扩展活动实现McClickListener{
//获取JSON数组的URL
专用静态字符串url=”http://code-explore.com/app/webroot/sp-codeexplore/getCategoryList.php?platform=Android";
//JSON节点名称
私有静态最终字符串标记_CATEGORY=“categories”;
私有静态最终字符串标记\u ID=“cat\u ID”;
私有静态最终字符串TAG_TITLE=“cat_TITLE”;
私有静态最终字符串TAG_NO=“total_apps”;
JSONArray categories=null;
公共最终静态字符串CAT_ID=“com.smartmux.codeexplore.MESSAGE”;
ArrayList=新建ArrayList();
ProgressDialog pDialog=新建ProgressDialog(此);
列表视图列表视图;
List rowItems=新建ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.category);
新建SM_AsyncTask().execute(url);
}
公共void returnHome(视图){
onBackPressed();
}
类SM_AsyncTask扩展了AsyncTask{
ProgressDialog pDialog=null;
@凌驾
受保护的void onPreExecute(){
pDialog.setMessage(“请稍候…”);
pDialog.setUndeterminate(真);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护列表背景(
字符串…参数){
//创建新的JSON解析器
JSONParser jParser=新的JSONParser();
//从URL获取JSON
JSONObject json=jParser.getJSONFromUrl(url);
试一试{
//获取JSON数组
categories=json.getJSONArray(TAG_CATEGORY);
对于(int i=0;i
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
12-03 08:36:59.370:E/AndroidRuntime(2082):致命异常:main
12-03 08:36:59.370:E/AndroidRuntime(2082):java.lang.RuntimeException:无法实例化活动组件信息{com.smartmux.codeexplore/com.smartmux.codeexplore.CategoryStactivity}:java.lang.NullPointerException
12-03 08:36:59.370:E/AndroidRuntime(2082):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
12-03 08:36:59.370:E/AndroidRuntime(2082):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-03 08:36:59.370:E/AndroidRuntime(2082):在android.app.ActivityThread.access$600(ActivityThread.java:141)
12-03 08:36:59.370:E/AndroidRuntime(2082):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-03 08:36:59.370:E/AndroidRuntime(2082):在android.os.Handler.dispatchMessage(Handler.java:99)上
12-03 08:36:59.370:E/AndroidRuntime(2082):在android.os.Looper.loop(Looper.java:137)
12-03 08:36:59.370:E/AndroidRuntime(2082):位于android.app.ActivityThread.main(ActivityThread.java:5103)
12-03 08:36:59.370:E/AndroidRuntime(2082):位于java.lang.reflect.Method.Invokenactive(本机方法)
12-03 08:36:59.370:E/AndroidRuntime(2082):位于java.lang.reflect.Method.invoke(Method.java:525)
12-03 08:36:59.370:E/AndroidRuntime(2082):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-03 08:36:59.370:E/AndroidRuntime(2082):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-03 08:36:59.370:E/AndroidRuntime(2082):在dalvik.system.NativeStart.main(本机方法)
12-03 08:36:59.370:E/AndroidRuntime(2082):由以下原因引起:java.lang.NullPointerException
12-03 08:36:59.370:E/AndroidRuntime(2082):位于android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:146)
12-03 08:36:59.370:E/AndroidRuntime(2082):在android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:103)
12-03 08:36:59.370:E/AndroidRuntime(2082):在android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)上
12-03 08:36:59.370:E/AndroidRuntime(2082):在android.app.AlertDialog.(AlertDialog.java:98)
12-03 08:36:59.370:E/AndroidRuntime(2082):在android.app.ProgressDialog.(ProgressDialog.java:77)
12-03 08:36:59.370:E/AndroidRuntime(2082):位于com.smartmux.codeexplore.CategoryStativity.(categoryStativity.java:39)
12-03 08:36:59.370:E/AndroidRuntime(2082):位于java.lang.Class.newInstanceImpl(本机方法)
12-03 08:36:59.370:E/AndroidRuntime(2082):位于java.lang.Class.newInstance(Class.java:1130)
12-03 08:36:59.370:E/AndroidRuntime(2082):在android.app.Instrumentation.newActivity(Instrumentation.java:1061)上
12-0
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
@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);
}
}
}