我有一個主要活動和4個片段。我使用方法ft.replace(R.id.listFragment, myfragment);
在主要活動中的片段之間切換。我的一個片段(片段A)包含一個列表視圖,Iam使用陣列適配器將數據填充到listview。現在的問題是,當我選擇「片段A」時,它是可見的,列表視圖顯示爲正常。但是,當我按Home按鈕並恢復應用程序的應用程序崩潰。錯誤是因爲「片段A」中的陣列適配器。我提供了logcat的,也有「片段」 .Thanks在advance.any幫助將不勝感激......片段崩潰時,恢復主要活動
07-24 14:00:12.695: E/AndroidRuntime(25771): FATAL EXCEPTION: main
07-24 14:00:12.695: E/AndroidRuntime(25771): java.lang.NullPointerException
07-24 14:00:12.695: E/AndroidRuntime(25771): at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
07-24 14:00:12.695: E/AndroidRuntime(25771): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
07-24 14:00:12.695: E/AndroidRuntime(25771): at com.glamzapp.saloon.AppoinAdapter.<init>(AppoinAdapter.java:47)
07-24 14:00:12.695: E/AndroidRuntime(25771): at com.glamzapp.saloon.Fragment_Appointments$GetAppointments.onPostExecute(Fragment_Appointments.java:192)
07-24 14:00:12.695: E/AndroidRuntime(25771): at com.glamzapp.saloon.Fragment_Appointments$GetAppointments.onPostExecute(Fragment_Appointments.java:1)
07-24 14:00:12.695: E/AndroidRuntime(25771): at android.os.AsyncTask.finish(AsyncTask.java:631)
07-24 14:00:12.695: E/AndroidRuntime(25771): at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-24 14:00:12.695: E/AndroidRuntime(25771): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-24 14:00:12.695: E/AndroidRuntime(25771): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 14:00:12.695: E/AndroidRuntime(25771): at android.os.Looper.loop(Looper.java:137)
07-24 14:00:12.695: E/AndroidRuntime(25771): at android.app.ActivityThread.main(ActivityThread.java:5306)
07-24 14:00:12.695: E/AndroidRuntime(25771): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 14:00:12.695: E/AndroidRuntime(25771): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 14:00:12.695: E/AndroidRuntime(25771): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-24 14:00:12.695: E/AndroidRuntime(25771): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-24 14:00:12.695: E/AndroidRuntime(25771): at dalvik.system.NativeStart.main(Native Method)
代碼:
public class Fragment_Appointments extends org.holoeverywhere.app.Fragment {
ListView list;
int ORDERDATE_FLAG = 1;
public static String[] Customer = { "Christian Bale", "Megan Fox",
"BradPitt", "Christian Bale", "Megan Fox", "BradPitt" };
public static String[] Treatment = { "Golden Facial", "Chocolate Manicure",
"Face Massage", "Golden Facial", "Chocolate Manicure",
"Face Massage" };
public static String[] Specialist = { "Jacob", "Joseline", "Linda",
"Jacob", "Joseline", "Linda" };
public static String[] Date = { "20 Feb", "21 Feb", "21 Feb", "20 Feb",
"21 Feb", "21 Feb" };
public static String[] Status = { "Completed", "Not completed",
"Not completed", "Completed", "Not completed", "Not completed" };
public static String[] TimeSlot = { "12PM-1PM", "12PM-1PM", "1PM-2PM",
"12PM-1PM", "12PM-1PM", "1PM-2PM" };
public static String[] CustomerImage = {
"http://shpintv.com/images/2013/05/Christian-Bale-Wallpapers-.jpg",
"http://cdn-media.hollywood.com/images/l/MeganFox_620_092812.jpg",
"http://www.topnews.in/light/files/Brad-Pitt_49.jpg",
"http://shpintv.com/images/2013/05/Christian-Bale-Wallpapers-.jpg",
"http://cdn-media.hollywood.com/images/l/MeganFox_620_092812.jpg",
"http://www.topnews.in/light/files/Brad-Pitt_49.jpg" };
public static String[] Customer_Id = { ",1", "2", "3", "1", "2", "3" };
public static String[] Spec_Id = { ",1", "2", "3", "1", "2", "3" };
// public static String[]
// Mast_CustName={"All","Megan Fox","Christian Bale","BradPitt"};
// public static String[]
// Mast_SpecialistName={"All","Megan Fox","Christian Bale","BradPitt"};
public static String[] Mast_CustId = { "1", "2", "3" };
public static String[] Mast_SpecialId = { "1", "2", "3" };
public static String[] Mast_Status = { "All", "Completed", "Not Completed" };
public static String[] Mast_Treatment = { "All", "Golden Facial",
"Chocolate Manicure", "Face Massage" };
public static String speckey = "0", custkey = "0", statuskey = "0",
treatkey = "0";
ProgressBar progress;
static ArrayList<String> Mast_CustName = new ArrayList<String>() {
{
add("Christian Bale");
add("Megan Fox");
add("BradPitt");
}
};
static ArrayList<String> Mast_SpecialistName = new ArrayList<String>() {
{
add("Jacob");
add("Joseline");
add("Linda");
}
};
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflating layout
View v = inflater.inflate(R.layout.appoin_fragment, container, false);
// We obtain layout references
list = (ListView) v.findViewById(R.id.ARListView);
progress = (ProgressBar) v.findViewById(R.id.arProgressBar);
list.setEmptyView(v.findViewById(R.id.empty));
if (Utils.isNetworkAvailable(getActivity())) {
new GetAppointments(getActivity()).execute();
} else {
AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
.create();
alertDialog.setMessage("No network connection!");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Add your code for the button here.
getActivity().finish();
}
});
alertDialog.show();
}
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Button reset=(Button)findViewById(R.id.reset);
}
public class GetAppointments extends AsyncTask<Void, Integer, Void> {
public GetAppointments(Activity activity) {
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context);
}
/** progress dialog to show user that the backup is processing. */
private ProgressDialog dialog;
/** application context. */
private Activity activity;
private Context context;
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progress.setVisibility(View.GONE);
AppoinAdapter adapter = new AppoinAdapter(getActivity(), Customer,
Treatment, Specialist, Date, Status, TimeSlot,
CustomerImage);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent details = new Intent(getActivity(),
App_Details_Activity.class);
startActivity(details);
}
});
}
}
這裏是我用的方法開關片段主要活動
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
FragmentTransaction ft = MainActivity.fm.beginTransaction();
Fragment_Appointments app = new Fragment_Appointments();
ft.replace(R.id.listFragment, app);
ft.commit();
感謝您的幫助,但我怎麼解決這個問題? –
保持活動實例的跟蹤並使異步任務等待它不爲空.. –
我在maincativity中使用了一個asynctask來替換fragment.And問題得到解決。感謝提示:) –