我有一个主要活动和4个片段。我使用方法ft.replace(R.id.listFragment, myfragment);
在主要活动中切换片段。我的一个片段(片段A)包含一个列表视图,Iam使用数组适配器将数据填充到listview。现在的问题是,当我选择“片段A”时,它是可见的,列表视图显示为正常。但是,当我按下主页按钮并恢复应用程序时,应用程序崩溃了。错误是因为“片段A”中的数组适配器。我提供了logcat以及“片段A”。谢谢。如果有任何帮助,我们将不胜感激......
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();
答案 0 :(得分:1)
问题是当asynctask调用postExecute
时,你的片段没有添加到你的活动中答案 1 :(得分:1)
在onPostExecute方法中,您使用getActivity()。
即使在您的应用程序的主线程(uithread)上执行此方法,也无法保证此时将创建活动。
确实如果应用程序重新启动asynctask onPostExecute,当活动仍处于不确定状态或甚至为null时,可以调用