有很多与我的问题相关的问题,但它们都没有解决我的问题。我几乎尝试了所有这些。
我在从网络源获取数据后尝试更改布局中的值,但我每次都会获得nullpointerexception。
错误我正在
03-25 18:05:43.071: E/AndroidRuntime(4832): FATAL EXCEPTION: main
03-25 18:05:43.071: E/AndroidRuntime(4832): java.lang.NullPointerException
03-25 18:05:43.071: E/AndroidRuntime(4832): at com.wishberg.app.CommunityPageActivity$SendRequest.onPostExecute(CommunityPageActivity.java:158)
03-25 18:05:43.071: E/AndroidRuntime(4832): at com.wishberg.app.CommunityPageActivity$SendRequest.onPostExecute(CommunityPageActivity.java:1)
03-25 18:05:43.071: E/AndroidRuntime(4832): at android.os.AsyncTask.finish(AsyncTask.java:631)
03-25 18:05:43.071: E/AndroidRuntime(4832): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-25 18:05:43.071: E/AndroidRuntime(4832): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-25 18:05:43.071: E/AndroidRuntime(4832): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 18:05:43.071: E/AndroidRuntime(4832): at android.os.Looper.loop(Looper.java:137)
03-25 18:05:43.071: E/AndroidRuntime(4832): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-25 18:05:43.071: E/AndroidRuntime(4832): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 18:05:43.071: E/AndroidRuntime(4832): at java.lang.reflect.Method.invoke(Method.java:525)
03-25 18:05:43.071: E/AndroidRuntime(4832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-25 18:05:43.071: E/AndroidRuntime(4832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-25 18:05:43.071: E/AndroidRuntime(4832): at dalvik.system.NativeStart.main(Native Method)
这是我的活动类
public class CommunityPageActivity extends Activity {
public String wishid = null;
private Context context = null;
public String TAG = "communitypage";
private String cachedName = "community";
private static final int URL_LOADER = 0;
private Menu optionsMenu;
private ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent i = getIntent();
wishid = i.getStringExtra("id");
context = getApplicationContext();
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
Common.setRefreshActionButtonState(true, optionsMenu);
progress = new ProgressDialog(this);
progress.setMessage("Loading...");
progress.setIndeterminate(true);
progress.show();
setContentView(R.layout.community);
}
private class SendRequest extends AsyncTask<String, Integer, String> {
// http://www.mysamplecode.com/2012/11/android-progress-bar-example.html
ViewHolder holder = null;
protected String doInBackground(String... requestURL) {
String data = "";
HttpURLConnection httpUrlConnection = null;
try {
data = Common.readStream(in);
} catch (MalformedURLException exception) {
Log.e(TAG, "MalformedURLException");
} catch (IOException exception) {
Log.e(TAG, "IOException");
} finally {
if (null != httpUrlConnection)
httpUrlConnection.disconnect();
}
return data;
}
protected void onPostExecute(String jsonString) {
progress.dismiss();
System.out.println(jsonString);
JSONArray jsonArray = Common.getArrayFromJsonString(jsonString, 0);
Inflater inflater;
// View v = inflater.inflate(R.layout.community);
holder.txtWishName = (TextView) CommunityPageActivity.this.findViewById(R.id.tv_communitywishname);
System.out.println(holder);
System.out.println(jsonArray);
}
/* private view holder class */
private class ViewHolder {
ImageView imageView;
TextView txtWishName;
ImageButton btn_touchwood;
ImageButton btn_addwish;
TextView tvGesture;
TextView tvNotes;
}
}
}
答案 0 :(得分:2)
删除此行
progress.dismiss();
你还没有初始化,也没有在任何地方使用.. 根据评论
你的
ViewHolder holder is null
答案 1 :(得分:1)
首先,不要在AsyncTask
内初始化您的小部件。不要在那里使用inflater。您应该初始化onCreate(..)
中的所有内容。
然后,您可以在AsyncTask中执行昂贵的操作(HTTP连接等)。然后在onPostExecute
中,您应该更新您的Activity
/ Fragment
中的已初始化的UI小部件。至少这就是AsyncTask
的意思。
正如已经提到的那样,您的holder
变量未初始化,这就是您首先获得异常的原因。
更新:此外,在onPostExecute()
中解析您的JSON数据是一个坏主意,因为它可能会否定您对使用AsyncTask所期望的效果 - 此方法在UI上运行 - 并且可以冻结它。将内容移至doInBackground()
。