恢复为mainactivity时片段崩溃 [英] Fragment crashes when resume to mainactivity

查看:178
本文介绍了恢复为mainactivity时片段崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要活动和4个片段,我正在使用 ft.replace(R.id.listFragment,myfragment); 方法在主要活动中的片段之间进行切换.我的一个片段(片段A)包含一个列表视图,而Iam使用数组适配器将数据填充到列表视图.现在的问题是,当我选择片段A"时,它是可见的,并且列表视图显示为正常.但是,当我按下主页按钮并恢复应用程序时,应用程序崩溃了.该错误是由于片段A"中的阵列适配器引起的.我提供了logcat和"fragment A".在此先感谢您的帮助.

I have a main activity and 4 fragments.I am using the method ft.replace(R.id.listFragment, myfragment); to switch between the fragments in main activity. One of my fragment (fragment A) contains a list view and Iam using array adapter to fill the data to listview. Now the problem is, when I select "fragment A" it is visible and the listview is shown as normal. But when I press home button and resume the application the app crashes. And the error is because of the array adapter in "fragment A". I am providing the logcat and also "fragment A".Thanks in advance.any help would be appreciated...

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);

            }
        });



    }
}

这是我在主要活动中切换片段的方法

And here is the method I used to switch fragment in main activity

fm = getSupportFragmentManager();
    ft = fm.beginTransaction();

    FragmentTransaction ft = MainActivity.fm.beginTransaction();

    Fragment_Appointments app = new Fragment_Appointments();
    ft.replace(R.id.listFragment, app);
    ft.commit();

推荐答案

在onPostExecute方法中,使用getActivity().

In your onPostExecute methods you use getActivity().

即使在应用程序的主线程(uithread)上执行此方法,也无法保证将在此瞬间创建活动.

Even if this method is executed on the main thread of your application (the uithread), there is no guaranty that the activity will be created at this instant.

实际上是在活动仍处于不确定状态甚至是null时调用应用程序重新启动asynctask onPostExecute

Indeed if the application restart the asynctask onPostExecute could be called when the activity is still in an indeterminate state or even null

这篇关于恢复为mainactivity时片段崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆