Asynctask with fragment,error关闭VM

时间:2013-10-27 21:52:45

标签: android android-fragments android-asynctask parse-platform

我正在尝试使用AsyncTask查询一个parseobject并在列表中显示图片和文本,但在上面运行时会显示以下错误:

    10-27 16:29:18.335: D/AndroidRuntime(28453): Shutting down VM
10-27 16:29:18.335: W/dalvikvm(28453): threadid=1: thread exiting with uncaught exception (group=0x40018578)
10-27 16:29:18.343: E/AndroidRuntime(28453): FATAL EXCEPTION: main
10-27 16:29:18.343: E/AndroidRuntime(28453): java.lang.NullPointerException
10-27 16:29:18.343: E/AndroidRuntime(28453):    at com.johanseetlabs.panamaviajero.A_su_alrededor$RemoteDataTask.onPostExecute(A_su_alrededor.java:101)
10-27 16:29:18.343: E/AndroidRuntime(28453):    at com.johanseetlabs.panamaviajero.A_su_alrededor$RemoteDataTask.onPostExecute(A_su_alrededor.java:1)
10-27 16:29:18.343: E/AndroidRuntime(28453):    at android.os.AsyncTask.finish(AsyncTask.java:417)
10-27 16:29:18.343: E/AndroidRuntime(28453):    at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-27 16:29:18.343: E/AndroidRuntime(28453):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-27 16:29:18.343: E/AndroidRuntime(28453):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 16:29:18.343: E/AndroidRuntime(28453):    at android.os.Looper.loop(Looper.java:130)
10-27 16:29:18.343: E/AndroidRuntime(28453):    at android.app.ActivityThread.main(ActivityThread.java:3687)
10-27 16:29:18.343: E/AndroidRuntime(28453):    at java.lang.reflect.Method.invokeNative(Native Method)
10-27 16:29:18.343: E/AndroidRuntime(28453):    at java.lang.reflect.Method.invoke(Method.java:507)
10-27 16:29:18.343: E/AndroidRuntime(28453):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-27 16:29:18.343: E/AndroidRuntime(28453):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-27 16:29:18.343: E/AndroidRuntime(28453):    at dalvik.system.NativeStart.main(Native Method)

我的片段:

public class A_su_alrededor extends Fragment {
    ListView listview;
    List<ParseObject> ob;
    ProgressDialog mProgressDialog;
    ListViewAdapter adapter;
    public List<ObtenerData> ObtenerDatalist = null;
    View view;
    private WeakReference<RemoteDataTask> asyncTaskWeakRef;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.a_su_alrededor, container, false);
        startNewAsyncTask();
        return view;
    }

    // RemoteDataTask AsyncTask
    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        private WeakReference<A_su_alrededor> fragmentWeakRef;

        private RemoteDataTask(
                A_su_alrededor a_su_alrededor) {
            this.fragmentWeakRef = new WeakReference<A_su_alrededor>(
                    a_su_alrededor);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(getActivity());
            // Set progressdialog title
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(true);
            // Show progressdialog
            mProgressDialog.show();
            // lockScreenOrientation();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create the array
            ObtenerDatalist = new ArrayList<ObtenerData>();
            try {
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                        "Hotel");
                query.orderByAscending("hotel");
                ob = query.find();
                for (ParseObject hotel : ob) {
                    // localizar imagenes en la columna
                    ParseFile image = (ParseFile) hotel.get("image");

                    ObtenerData map = new ObtenerData();
                    map.setHotel((String) hotel.get("hotel"));
                    map.setImage(image.getUrl());
                    ObtenerDatalist.add(map);
                }
            } catch (ParseException e) {
                Log.d("TAG", "AsynTask no se ejecuto");
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Locate the listview in listview_main.xml
            listview = (ListView) view.findViewById(R.id.listview_a_su_alrededor);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(getActivity(), ObtenerDatalist);
            // Binds the Adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
            // unlockScreenOrientation();
        }
    }

    private void startNewAsyncTask() {
        RemoteDataTask asyncTask = new RemoteDataTask(this);
        this.asyncTaskWeakRef = new WeakReference<RemoteDataTask>(asyncTask);
        asyncTask.execute();
    }
}

选项卡上使用了相同的代码,但我决定删除该选项卡。片段放置在drawerlayout的FrameLayout上。

最后请原谅我的英语。

2 个答案:

答案 0 :(得分:1)

onCreateView方法中,你有这一行:

View view = inflater.inflate(R.layout.a_su_alrededor, container, false);

我认为你应该删除类型名称:

view = inflater.inflate(R.layout.a_su_alrededor, container, false);

因为乍一看,您似乎在失败的view方法中使用了对名为onPostExecute的变量的类范围引用。

答案 1 :(得分:0)

有一个空指针异常,你能从logcat中识别它所在的行吗?它可能是第101行