当我尝试打开由listview组成的片段时,我的应用程序崩溃了。
情景:
首次打开fragmentA,listview不显示。
点击fragmentA上的按钮(转到fragmentB)。
在fragmentB上点击后退按钮,片段A上的listview显示。
退出应用,尝试再次打开fragmentA,应用崩溃。
My Fragment课程:
public class FamilyTreeFragment extends Fragment {
// Declare Variables
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
FamilyTreeListViewAdapter adapter;
private List<FamilyTree> familytreelist = null;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_familytree, container, false);
new RemoteDataTask().execute();
// Locate the listview in listview_main.xml
listview = (ListView) rootView.findViewById(R.id.list);
// Pass the results into ListViewAdapter.java
// adapter = new FamilyTreeListViewAdapter(getActivity(), familytreelist);
// Binds the Adapter to the ListView
// listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
// btn_add
Button btn_add = (Button) rootView.findViewById(R.id.btn_add);
btn_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(SuggestionsFragment.this.getActivity(),
// "You have selected Parks.", Toast.LENGTH_SHORT).show();
getFragmentManager().beginTransaction().replace(R.id.container, new FamilyTreeFragment2()).addToBackStack(null).commit();
}
});
return rootView;
}
// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
mProgressDialog.setTitle("");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
protected Void doInBackground(Void... arg0) {
// Create the array
familytreelist = new ArrayList<FamilyTree>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("FamilyTree");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("createdAt");
ob = query.find();
for (ParseObject familytree : ob) {
// Locate images in flag column
ParseFile image = (ParseFile) familytree.get("image");
FamilyTree tree = new FamilyTree();
tree.setName((String) familytree.get("name"));
tree.setRelation((String) familytree.get("relation"));
tree.setImage(image.getUrl());
familytreelist.add(tree);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) rootView.findViewById(R.id.list);
// Pass the results into ListViewAdapter.java
adapter = new FamilyTreeListViewAdapter(getActivity(), familytreelist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
// mProgressDialog.dismiss();
}
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/familytreecontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.parcel.FamilyTreeFragment$PlaceholderFragment" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="407dp"
android:layout_weight="2.86" >
</ListView>
<Button
android:id="@+id/btn_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add" />
logcat的:
11-06 17:01:06.500: E/AndroidRuntime(22072): FATAL EXCEPTION: main
11-06 17:01:06.500: E/AndroidRuntime(22072): Process: com.example.parcel, PID: 22072
11-06 17:01:06.500: E/AndroidRuntime(22072): java.lang.NullPointerException
11-06 17:01:06.500: E/AndroidRuntime(22072): at com.example.parcel.FamilyTreeFragment$RemoteDataTask.onPostExecute(FamilyTreeFragment.java:111)
11-06 17:01:06.500: E/AndroidRuntime(22072): at com.example.parcel.FamilyTreeFragment$RemoteDataTask.onPostExecute(FamilyTreeFragment.java:1)
11-06 17:01:06.500: E/AndroidRuntime(22072): at android.os.AsyncTask.finish(AsyncTask.java:632)
11-06 17:01:06.500: E/AndroidRuntime(22072): at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-06 17:01:06.500: E/AndroidRuntime(22072): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
11-06 17:01:06.500: E/AndroidRuntime(22072): at android.os.Handler.dispatchMessage(Handler.java:102)
11-06 17:01:06.500: E/AndroidRuntime(22072): at android.os.Looper.loop(Looper.java:157)
11-06 17:01:06.500: E/AndroidRuntime(22072): at android.app.ActivityThread.main(ActivityThread.java:5867)
11-06 17:01:06.500: E/AndroidRuntime(22072): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 17:01:06.500: E/AndroidRuntime(22072): at java.lang.reflect.Method.invoke(Method.java:515)
11-06 17:01:06.500: E/AndroidRuntime(22072): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-06 17:01:06.500: E/AndroidRuntime(22072): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
11-06 17:01:06.500: E/AndroidRuntime(22072): at dalvik.system.NativeStart.main(Native Method)
我该如何解决这个问题?任何帮助表示赞赏。谢谢。
答案 0 :(得分:0)
试试这种方式,希望这可以帮助您解决问题。
查看rootView = inflater.inflate(R.layout.fragment_familytree,container,false);
替换为
rootView = inflater.inflate(R.layout.fragment_familytree,container,false);
错误原因:
你在类级别上有decalre rootView但是在onCreateView()上没有初始化,你在onCreateView()中查看rootView作为局部变量,所以onPostExecute()rootView将指向仍未初始化的类级变量。
格式良好的代码:
public class FamilyTreeFragment extends Fragment {
// Declare Variables
private ListView listview;
private Button btn_add;
private FamilyTreeListViewAdapter adapter;
private List<FamilyTree> familytreelist;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_familytree, container, false);
listview = (ListView) rootView.findViewById(R.id.list);
btn_add = (Button) rootView.findViewById(R.id.btn_add);
mProgressDialog.dismiss();
btn_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(SuggestionsFragment.this.getActivity(),
// "You have selected Parks.", Toast.LENGTH_SHORT).show();
getFragmentManager().beginTransaction().replace(R.id.container, new FamilyTreeFragment2()).addToBackStack(null).commit();
}
});
new RemoteDataTask().execute();
return rootView;
}
// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, List<FamilyTree>> {
private ProgressDialog mProgressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
mProgressDialog.setTitle("");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
protected List<FamilyTree> doInBackground(Void... arg0) {
// Create the array
List<FamilyTree> list = new ArrayList<FamilyTree>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("FamilyTree");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("createdAt");
List<ParseObject> ob = query.find();
for (ParseObject familytree : ob) {
// Locate images in flag column
ParseFile image = (ParseFile) familytree.get("image");
FamilyTree tree = new FamilyTree();
tree.setName((String) familytree.get("name"));
tree.setRelation((String) familytree.get("relation"));
tree.setImage(image.getUrl());
list.add(tree);
}
} catch (ParseException e) {
e.printStackTrace();
Log.e("Error", e.getMessage());
}
return list;
}
protected void onPostExecute(List<FamilyTree> result) {
super.onPostExecute(result);
if(mProgressDialog!=null && mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
if(familytreelist==null){
familytreelist = new ArrayList<FamilyTree>();
}
familytreelist.addAll(result);
// Pass the results into ListViewAdapter.java
adapter = new FamilyTreeListViewAdapter(getActivity(), familytreelist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
}
}
}