启动具有异步任务的新片段时强制关闭

时间:2014-02-10 22:06:46

标签: android android-fragments android-asynctask

我有一个加载一个新片段的按钮,但它是强制关闭时出现此错误:

java.lang.NullPointerException
        at com.beerportfolio.beerportfoliopro.BPTopTastes.onCreateView(BPTopTastes.java:29)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:5789)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
        at dalvik.system.NativeStart.main(Native Method)

加载新片段的按钮是:

public class Discover extends Fragment {
    Fragment Fragment_one;
    Fragment Fragment_two;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.activity_discover, container, false);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        String userID = prefs.getString("userID", null);

        //add button onclick fo top beers
        Button bt = (Button)v.findViewById(R.id.discoverBeers);
        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // do whatever stuff you wanna do here
                FragmentManager man=getFragmentManager();
                FragmentTransaction tran=man.beginTransaction();
                Fragment_one=new BPTopBeers();
                tran.replace(R.id.main, Fragment_one);//tran.
                tran.addToBackStack(null);
                tran.commit();
            }
        });

        //todo: add button onclick fo top tastes
        Button bt2 = (Button)v.findViewById(R.id.discoverTaste);
        bt2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // do whatever stuff you wanna do here
                FragmentManager man=getFragmentManager();
                FragmentTransaction tran=man.beginTransaction();
                Fragment_two=new BPTopTastes();
                tran.replace(R.id.main, Fragment_two);//tran.
                tran.addToBackStack(null);
                tran.commit();
            }
        });

        // Inflate the layout for this fragment
        //todo: change to discover layout
        return v;

    }

正在启动的片段是:

public class BPTopTastes extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.taste_statistics_layout, container, false);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        String userID = prefs.getString("userID", null);

        String title = "Top Tastes on Beer Portfolio";
        TextView topTitle = (TextView) v.findViewById(R.id.topTasteTitle);
        topTitle.setText(title);


        //construct url
        String url = "myURL";


        //async task goes here
        new GetTasteStatisticsJSON(getActivity()).execute(url);

        // Inflate the layout for this fragment
        return v;

    }



}

并且正在调用的异步任务是:

public class GetTasteStatisticsJSON extends AsyncTask<String, Void, String> {

    Context c;
    private ProgressDialog Dialog;

    public GetTasteStatisticsJSON(Context context)
    {
        c = context;
        Dialog = new ProgressDialog(c);
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return readJSONFeed(arg0[0]);
    }

    protected void onPreExecute() {
        Dialog.setMessage("Getting  tastes");

        Dialog.setTitle("Loading");
        Dialog.setCancelable(false);
        Dialog.show();
    }

    protected void onPostExecute(String result){
        //decode json here
        try{
            JSONArray jsonArray = new JSONArray(result);


            //acces listview
            ListView lv = (ListView) ((Activity) c).findViewById(R.id.yourTasteStatistics);

            //make array list for beer
            final List<TasteInfo> tasteList = new ArrayList<TasteInfo>();



            for(int i = 0; i < jsonArray.length(); i++) {

                String style = jsonArray.getJSONObject(i).getString("taste");
                String rate = jsonArray.getJSONObject(i).getString("rate");

                int count = i + 1;

                style = count + ". " + style;

                Log.d("brewery stats", style);

                //create object
                TasteInfo tempTaste = new TasteInfo(style, rate);

                //add to arraylist
                tasteList.add(tempTaste);


                //add items to listview
                TasteInfoAdapter adapter1 = new TasteInfoAdapter(c ,R.layout.brewer_stats_listview, tasteList);
                lv.setAdapter(adapter1);


                //set up clicks
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                                            int arg2, long arg3) {
                        TasteInfo o=(TasteInfo)arg0.getItemAtPosition(arg2);

                        String tempTaste = o.taste;



                        //todo: load taste page fragment
                        //Intent myIntent = new Intent(c, TastePage.class);
                       // myIntent.putExtra("taste", tempTaste);
                        //c.startActivity(myIntent);


                    }
                });








            }

        }
        catch(Exception e){

        }

        Dialog.dismiss();

    }

    public String readJSONFeed(String URL) {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream inputStream = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }
                inputStream.close();
            } else {
                Log.d("JSON", "Failed to download file");
            }
        } catch (Exception e) {
            Log.d("readJSONFeed", e.getLocalizedMessage());
        }
        return stringBuilder.toString();
    }

}

我有点困惑,因为我在同一个片段中有另一个按钮以类似的方式启动一个新片段就好......

2 个答案:

答案 0 :(得分:1)

我在梳理完代码后发现了自己的错误。我在这行中有错误的ID:

 TextView topTitle = (TextView) v.findViewById(R.id.topTasteTitle);

因此无法找到,设置标题。

答案 1 :(得分:0)

你能检查BPTopTastes布局是否正确膨胀,因此当你调用setText()方法时topTitle不为null。

删除异步任务时是否有效?

Eclipse调试器可以设置为在异常时暂停,这可能会给你一些指示。