使用onArticleSelected传递数据是强制关闭我的应用程序(Passing data with onArticleSelected is force closing my app)

编程入门 行业动态 更新时间:2024-10-27 20:29:29
使用onArticleSelected传递数据是强制关闭我的应用程序(Passing data with onArticleSelected is force closing my app)

我有一个对象列表,进入列表视图。 当单击listview中的项目时,我想要一个新的片段打开来自新片段中我的对象的数据。 当我点击listview中的项目时,我收到此错误:

06-26 11:05:47.564 14384-14384/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.beerportfolio.beerportfoliopro, PID: 14384 java.lang.NullPointerException at com.beerportfolio.beerportfoliopro.GetLocationsJSON$1.onItemClick(GetLocationsJSON.java:132) at android.widget.AdapterView.performItemClick(AdapterView.java) at android.widget.AbsListView.performItemClick(AbsListView.java) at android.widget.AbsListView$PerformClick.run(AbsListView.java) at android.widget.AbsListView$3.run(AbsListView.java) at android.os.Handler.handleCallback(Handler.java) at android.os.Handler.dispatchMessage(Handler.java) at android.os.Looper.loop(Looper.java) at android.app.ActivityThread.main(ActivityThread.java) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) at dalvik.system.NativeStart.main(Native Method)

崩溃发生的代码:

public class GetLocationsJSON extends AsyncTask<String, Void, String> { Context c; private ProgressDialog Dialog; public GetLocationsJSON(Context context) { c = context; Dialog = new ProgressDialog(c); } //****************code for on click OnArticleSelectedListener listenerBeer; public interface OnArticleSelectedListener{ public void onArticleSelected(LocationData d); } public void setOnArticleSelectedListener(OnArticleSelectedListener listener){ this.listenerBeer = listener; } //******************end code for onClick @Override protected String doInBackground(String... arg0) { // TODO Auto-generated method stub return readJSONFeed(arg0[0]); } protected void onPreExecute() { Dialog.setMessage("Searching For Brewery Locations"); Dialog.setTitle("Loading"); Dialog.show(); } protected void onPostExecute(String result){ //decode json here try{ JSONObject json = new JSONObject(result); //acces listview ListView lv = (ListView) ((Activity) c).findViewById(R.id.locationlist); //make array list for beer final List<LocationData> beerList = new ArrayList<LocationData>(); String phone; String hours; //get json items for(int i = 0; i < json.getJSONObject("data").getJSONArray("locations").length(); i++) { String name = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("name"); Log.d("location" , name); try { phone = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("phone"); } catch(JSONException e){ phone = "N/A"; } String lat = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("latitude"); String longitude = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("longitude"); String open = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("openToPublic"); try { hours = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("hoursOfOperation"); } catch (JSONException e){ hours = "N/A"; } String type = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("locationTypeDisplay"); //todo: create location object LocationData thisBeer = new LocationData(name, phone, lat, longitude, open, hours, type); //add beer to list beerList.add(thisBeer); } //update listview LocationAdapter adapter1 = new LocationAdapter(c ,R.layout.listview_item_row, beerList); lv.setAdapter(adapter1); //set up clicks lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { LocationData beerInfo = beerList.get(arg2); //************************Launch listener interface listenerBeer.onArticleSelected(beerInfo); } }); } catch(Exception e){ } Dialog.dismiss(); }

发生崩溃的上述代码中的第132行是:

listenerBeer.onArticleSelected(beerInfo);

听众的其余部分在这里:

public class BreweryLocations extends Fragment implements GetLocationsJSON.OnArticleSelectedListener { String beerId = ""; Dialog dialog = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //set layout here View v = inflater.inflate(R.layout.brewerylocation_page, container, false); setHasOptionsMenu(true); //get user information SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); String userName = prefs.getString("userName", null); String userID = prefs.getString("userID", null); //Todo: code for retrieving brewery location String breweryId = prefs.getString("breweryID", null); String url2 = "myURL"; //call async task now! new GetLocationsJSON(getActivity()).execute(url2); Log.d("location" , url2); // Inflate the layout for this fragment return v; } @Override public void onArticleSelected(LocationData d){ //code to execute on click Fragment Fragment_one; FragmentManager man= getFragmentManager(); FragmentTransaction tran = man.beginTransaction(); //adds beer data to shared prefs for beer tabs SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); SharedPreferences.Editor editor = preferences.edit(); editor.putString("name",d.name); editor.putString("hours",d.hours); editor.putString("lat",d.lat); editor.putString("lng",d.lng); editor.putString("phone",d.phone); editor.commit(); Fragment_one = new BeerTabs(); tran.replace(R.id.main, Fragment_one);//tran. tran.addToBackStack(null); tran.commit(); } }

I have a list of objects, that go into a listview. When an item from the listview is clicked I want a new fragment to open up with the data from my object in the new fragment. When I click on an item in my listview I am getting this error:

06-26 11:05:47.564 14384-14384/com.beerportfolio.beerportfoliopro E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.beerportfolio.beerportfoliopro, PID: 14384 java.lang.NullPointerException at com.beerportfolio.beerportfoliopro.GetLocationsJSON$1.onItemClick(GetLocationsJSON.java:132) at android.widget.AdapterView.performItemClick(AdapterView.java) at android.widget.AbsListView.performItemClick(AbsListView.java) at android.widget.AbsListView$PerformClick.run(AbsListView.java) at android.widget.AbsListView$3.run(AbsListView.java) at android.os.Handler.handleCallback(Handler.java) at android.os.Handler.dispatchMessage(Handler.java) at android.os.Looper.loop(Looper.java) at android.app.ActivityThread.main(ActivityThread.java) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) at dalvik.system.NativeStart.main(Native Method)

The code where the crash is happening:

public class GetLocationsJSON extends AsyncTask<String, Void, String> { Context c; private ProgressDialog Dialog; public GetLocationsJSON(Context context) { c = context; Dialog = new ProgressDialog(c); } //****************code for on click OnArticleSelectedListener listenerBeer; public interface OnArticleSelectedListener{ public void onArticleSelected(LocationData d); } public void setOnArticleSelectedListener(OnArticleSelectedListener listener){ this.listenerBeer = listener; } //******************end code for onClick @Override protected String doInBackground(String... arg0) { // TODO Auto-generated method stub return readJSONFeed(arg0[0]); } protected void onPreExecute() { Dialog.setMessage("Searching For Brewery Locations"); Dialog.setTitle("Loading"); Dialog.show(); } protected void onPostExecute(String result){ //decode json here try{ JSONObject json = new JSONObject(result); //acces listview ListView lv = (ListView) ((Activity) c).findViewById(R.id.locationlist); //make array list for beer final List<LocationData> beerList = new ArrayList<LocationData>(); String phone; String hours; //get json items for(int i = 0; i < json.getJSONObject("data").getJSONArray("locations").length(); i++) { String name = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("name"); Log.d("location" , name); try { phone = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("phone"); } catch(JSONException e){ phone = "N/A"; } String lat = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("latitude"); String longitude = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("longitude"); String open = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("openToPublic"); try { hours = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("hoursOfOperation"); } catch (JSONException e){ hours = "N/A"; } String type = json.getJSONObject("data").getJSONArray("locations").getJSONObject(i).getString("locationTypeDisplay"); //todo: create location object LocationData thisBeer = new LocationData(name, phone, lat, longitude, open, hours, type); //add beer to list beerList.add(thisBeer); } //update listview LocationAdapter adapter1 = new LocationAdapter(c ,R.layout.listview_item_row, beerList); lv.setAdapter(adapter1); //set up clicks lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { LocationData beerInfo = beerList.get(arg2); //************************Launch listener interface listenerBeer.onArticleSelected(beerInfo); } }); } catch(Exception e){ } Dialog.dismiss(); }

Line 132 in the above code where the crash happens is:

listenerBeer.onArticleSelected(beerInfo);

The rest of the listener is here:

public class BreweryLocations extends Fragment implements GetLocationsJSON.OnArticleSelectedListener { String beerId = ""; Dialog dialog = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //set layout here View v = inflater.inflate(R.layout.brewerylocation_page, container, false); setHasOptionsMenu(true); //get user information SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); String userName = prefs.getString("userName", null); String userID = prefs.getString("userID", null); //Todo: code for retrieving brewery location String breweryId = prefs.getString("breweryID", null); String url2 = "myURL"; //call async task now! new GetLocationsJSON(getActivity()).execute(url2); Log.d("location" , url2); // Inflate the layout for this fragment return v; } @Override public void onArticleSelected(LocationData d){ //code to execute on click Fragment Fragment_one; FragmentManager man= getFragmentManager(); FragmentTransaction tran = man.beginTransaction(); //adds beer data to shared prefs for beer tabs SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); SharedPreferences.Editor editor = preferences.edit(); editor.putString("name",d.name); editor.putString("hours",d.hours); editor.putString("lat",d.lat); editor.putString("lng",d.lng); editor.putString("phone",d.phone); editor.commit(); Fragment_one = new BeerTabs(); tran.replace(R.id.main, Fragment_one);//tran. tran.addToBackStack(null); tran.commit(); } }

最满意答案

看起来你的listenerBeer为null。

试着改变

//call async task now! new GetLocationsJSON(getActivity()).execute(url2);

GetLocationsJSON locationJson = new GetLocationJSON(getActivity()); locationJson.setOnArticleSelectedListener(this); locationJson.execute(url2);

It looks like your listenerBeer is null.

Try to change

//call async task now! new GetLocationsJSON(getActivity()).execute(url2);

to

GetLocationsJSON locationJson = new GetLocationJSON(getActivity()); locationJson.setOnArticleSelectedListener(this); locationJson.execute(url2);

更多推荐

String,new,public,java,listview,电脑培训,计算机培训,IT培训"/> <meta name=&qu

本文发布于:2023-08-04 04:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   数据   Passing   onArticleSelected   closing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!