我正在使用片段,下面是我的代码。当我尝试多次更改方向时,我的应用程序崩溃了。
这是我的代码:
public void Reservation_Dashboardinfo_execute() {
String url_main = getActivity().getIntent().getStringExtra(
"Detail_Bundle");
url = url_main + "&action=getDashboardInfoDay";
flag = 1;
updateList clist = new updateList();
clist.execute(new String[] { url });
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// setRetainInstance(true);
String url_main = getActivity().getIntent().getStringExtra(
"Detail_Bundle");
url = url_main + "&action=getDashboardInfoDay";
updateList clist = new updateList();
clist.execute(new String[] { url });
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.reserv_dashboardinfo, container,
false);
return view;
}
class updateList
extends
AsyncTask<String, ArrayList<HashMap<String, String>>, ArrayList<HashMap<String, String>>> {
TableRow row, row_yield, row_status;
TextView t1, t2, t3, t4, t5, t6, t7;
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
TableLayout tl1;
tl1 = (TableLayout) getActivity().findViewById(R.id.table_dashboard);
if (flag == 1) {
tl1 = (TableLayout) getActivity()
.findViewById(R.id.table_dashboard);
tl1.removeAllViews();
tl1.invalidate();
row = new TableRow(getActivity());
t1 = new TextView(getActivity());
t2 = new TextView(getActivity());
t3 = new TextView(getActivity());
t1.setText("Daypart");
t2.setText("Reservation");
t3.setText("Guest");
t1.setTextSize(15);
t2.setTextSize(15);
t3.setTextSize(15);
t1.setTypeface(null,Typeface.BOLD);
t2.setTypeface(null,Typeface.BOLD);
t3.setTypeface(null,Typeface.BOLD);
row.addView(t1);
row.addView(t2);
row.addView(t3);
tl1.setStretchAllColumns(true);
tl1.addView(row, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
for (int current = 0; current < result.size(); current++) {
row = new TableRow(getActivity());
t1 = new TextView(getActivity());
t2 = new TextView(getActivity());
t3 = new TextView(getActivity());
t1.setText(result.get(current).get("Daypart"));
t2.setText(result.get(current).get("Reservation_Today"));
t3.setText(result.get(current).get("Nop_Today"));
t1.setTextSize(15);
t2.setTextSize(15);
t3.setTextSize(15);
row.addView(t1);
row.addView(t2);
row.addView(t3);
tl1.setStretchAllColumns(true);
tl1.addView(row, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
row_yield = new TableRow(getActivity());
t4 = new TextView(getActivity());
t5 = new TextView(getActivity());
t4.setText("Current Yield");
// String
// total_seated=getActivity().getIntent().getStringExtra("total_seated");
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
contactList = (ArrayList<HashMap<String, String>>) getActivity()
.getIntent().getSerializableExtra("logindetails");
String maxcapacity = contactList.get(0).get("MaxCapacity");
int max_value = Integer.parseInt(maxcapacity);
int total_seated_value = Integer.parseInt("2");
float yield = (total_seated_value * 100) / max_value;
t5.setText("" + yield + "% (" + total_seated_value + " seated)");
t4.setTextSize(15);
t5.setTextSize(15);
row_yield.addView(t4);
row_yield.addView(t5);
tl1.setStretchAllColumns(true);
tl1.addView(row_yield, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
row_status = new TableRow(getActivity());
t6 = new TextView(getActivity());
t7 = new TextView(getActivity());
t6.setText("Connection Status");
Drawable img = getActivity().getApplicationContext().getResources()
.getDrawable(R.drawable.green_rnd);
t7.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
t6.setTextSize(15);
row_status.addView(t6);
row_status.addView(t7);
tl1.setStretchAllColumns(true);
tl1.addView(row_status, new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(
String... params) {
// TODO Auto-generated method stub
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url, postParameters);
try {
// Getting Array of Contacts
contacts = json.getJSONArray("result");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
// String id = c.getString(TAG_ID);
String daypart = c.getString("daypart");
String reservation = c.getString("reservations");
String reservation7 = c.getString("reservations7");
String reservation14 = c.getString("reservations14");
String guests = c.getString("nop");
String guests7 = c.getString("nop7");
String guests14 = c.getString("nop14");
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
// map.put(TAG_ID, id);
map.put("Daypart", daypart);
map.put("Reservation_Today", reservation);
map.put("Reservation_Next7", reservation7);
map.put("Reservation_Next14", reservation14);
map.put("Nop_Today", guests);
map.put("Nop_Next7", guests7);
map.put("Nop_Next14", guests14);
// map.put(TAG_EMAIL, email);
// map.put(TAG_PHONE_MOBILE, mobile);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return contactList;
}
// TODO Auto-generated method stub
}
}
这是我的logcat
03-12 18:49:26.946: E/PhonePolicy(1208): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
03-12 18:49:27.036: I/dalvikvm-heap(1208): Grow heap (frag case) to 7.944MB for 3145744-byte allocation
03-12 18:49:27.296: D/libEGL(1208): loaded /system/lib/egl/libEGL_mali.so
03-12 18:49:27.296: D/libEGL(1208): loaded /system/lib/egl/libGLESv1_CM_mali.so
03-12 18:49:27.306: D/libEGL(1208): loaded /system/lib/egl/libGLESv2_mali.so
03-12 18:49:27.346: D/OpenGLRenderer(1208): Enabling debug mode 0
03-12 18:49:27.856: I/dalvikvm-heap(1208): Grow heap (frag case) to 11.000MB for 3145744-byte allocation
03-12 18:49:28.006: D/TextLayoutCache(1208): Using debug level: 0 - Debug Enabled: 0
03-12 18:49:28.376: D/OpenGLRenderer(1208): Flushing caches (mode 0)
03-12 18:49:35.936: V/response code(1208): 200
03-12 18:49:42.316: D/OpenGLRenderer(1208): Flushing caches (mode 0)
03-12 18:49:44.226: D/OpenGLRenderer(1208): Flushing caches (mode 0)
03-12 18:49:44.476: W/PhoneWindow(1208): Previously focused view reported id 2131230886 during save, but can't be found during restore.
03-12 18:49:46.066: D/AndroidRuntime(1208): Shutting down VM
03-12 18:49:46.066: W/dalvikvm(1208): threadid=1: thread exiting with uncaught exception (group=0x40a3e1f8)
03-12 18:49:46.076: E/AndroidRuntime(1208): FATAL EXCEPTION: main
03-12 18:49:46.076: E/AndroidRuntime(1208): java.lang.NullPointerException
03-12 18:49:46.076: E/AndroidRuntime(1208): at com.demo.fragmentdemo.Reservation_Dashboardinfo$updateList.onPostExecute(Reservation_Dashbo ardinfo.java:116)
03-12 18:49:46.076: E/AndroidRuntime(1208): at com.demo.fragmentdemo.Reservation_Dashboardinfo$updateList.onPostExecute(Reservation_Dashboardinfo.java:1)
03-12 18:49:46.076: E/AndroidRuntime(1208): at android.os.AsyncTask.finish(AsyncTask.java:602)
03-12 18:49:46.076: E/AndroidRuntime(1208): at android.os.AsyncTask.access$600(AsyncTask.java:156)
03-12 18:49:46.076: E/AndroidRuntime(1208): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
03-12 18:49:46.076: E/AndroidRuntime(1208): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 18:49:46.076: E/AndroidRuntime(1208): at android.os.Looper.loop(Looper.java:137)
03-12 18:49:46.076: E/AndroidRuntime(1208): at android.app.ActivityThread.main(ActivityThread.java:4429)
03-12 18:49:46.076: E/AndroidRuntime(1208): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 18:49:46.076: E/AndroidRuntime(1208): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 18:49:46.076: E/AndroidRuntime(1208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
03-12 18:49:46.076: E/AndroidRuntime(1208): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
我无法理解我的错误。我在这里做错了什么?
答案 0 :(得分:1)
你的Asynctask正在获得一个NPE(空指针异常)。不应该为null的东西(指针)是。
此外,由于您使用的是片段,因此您应该保留旋转数据either by saving them in the onSavedInstance
,或者保留片段的实例using the setRetainInstance