0

我有一个主要活动和4个片段。我正在使用该方法ft.replace(R.id.listFragment, myfragment);在主要活动中的片段之间切换。我的一个片段(片段A)包含一个列表视图,并且我使用数组适配器将数据填充到列表视图。现在的问题是,当我选择“片段 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();
4

2 回答 2

1

问题是当 asynctask 调用 postExecute 时,您的片段未添加到您的活动中

于 2013-07-24T08:57:47.580 回答
1

在您的 onPostExecute 方法中,您使用 getActivity()。

即使此方法在应用程序的主线程(uithread)上执行,也不能保证此时将创建活动。

实际上,如果应用程序重新启动,则可以在活动仍处于不确定状态甚至为 null 时调用 asynctask onPostExecute

于 2013-07-24T09:03:45.630 回答