[英]Android App crashes after Splash Screen showing FATAL EXCEPTION: main
[英]FATAL EXCEPTION: main - app crashes after a while
很長時間以來,我在將XML解析為Fragment NavigationTabs時遇到問題。 當我運行我的項目時,它會顯示2-4秒,不會查看我的列表視圖(空白片段),然后崩潰。 我認為onPostExecute方法有問題。 我可以請你幫忙嗎?
LogCat:
E/AndroidRuntime(473): FATAL EXCEPTION: main
E/AndroidRuntime(473): java.lang.NullPointerException
E/AndroidRuntime(473): at android.widget.ArrayAdapter.init(ArrayAdapter.java:271)
12-12 15:53:59.730: E/AndroidRuntime(473): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:150)
E/AndroidRuntime(473): at com.example.premierleague.clubsAdapter.<init>(clubsAdapter.java:37)
E/AndroidRuntime(473): at com.example.premierleague.Fragment1$ClubsDownloadTask.onPostExecute(Fragment1.java:113)
E/AndroidRuntime(473): at com.example.premierleague.Fragment1$ClubsDownloadTask.onPostExecute(Fragment1.java:1)
E/AndroidRuntime(473): at android.os.AsyncTask.finish(AsyncTask.java:417)
E/AndroidRuntime(473): at android.os.AsyncTask.access$300(AsyncTask.java:127)
12-12 15:53:59.730: E/AndroidRuntime(473): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
E/AndroidRuntime(473): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(473): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(473): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(473): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(473): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(473): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(473): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(473): at dalvik.system.NativeStart.main(Native Method)
Fragment1.java
public class Fragment1 extends Fragment {
MainActivity activity = new MainActivity();
private clubsAdapter mAdapter;
private ListView clubs;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_1, container, false);
Log.i("PremierLeague", "OnCreateView()");
clubs = (ListView)rootView.findViewById(R.id.clubsList);
clubs.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
if(isNetworkAvailable()){
Log.i("PremierLeague", "starting download Task");
ClubsDownloadTask download = new ClubsDownloadTask();
download.execute();
}else{
mAdapter = new clubsAdapter(getActivity().getApplicationContext(), -1,
ClubsXmlPullParser.getItemsFromFile(getActivity()));
clubs.setAdapter(mAdapter);
}
return super.onCreateView(inflater, container, savedInstanceState);
}
//Helper method to determine if Internet connection is available.
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
private class ClubsDownloadTask extends AsyncTask<Void, Void, Void>{
private clubsAdapter mAdapter;
private List<LeagueClub> clubs;
Fragment1 ctx;
Context context = getActivity();
private Context mContext;
@Override
protected Void doInBackground(Void... arg0) {
try {
Downloader.DownloadFromUrl("http://dl.dropboxusercontent.com/s/h2qc41k2yy3c1ir/clubs.xml", getActivity().openFileOutput("clubs.xml", Context.MODE_PRIVATE));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
mAdapter = new clubsAdapter(mContext, -1, ClubsXmlPullParser.getItemsFromFile(getActivity()));
((ListView) clubs).setAdapter(mAdapter);
Log.i("PremierLeague", "adapter size = "+ mAdapter.getCount());
}
}
}
定制適配器
public class clubsAdapter extends ArrayAdapter<LeagueClub> {
ImageLoader imageLoader;
DisplayImageOptions options;
@SuppressWarnings("deprecation")
public clubsAdapter(Context class1, int textViewResourceId, List<LeagueClub> clubs) {
super(class1, textViewResourceId, clubs);
//Setup the ImageLoader, we'll use this to display our images
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(class1).build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
//Setup options for ImageLoader so it will handle caching for us.
options = new DisplayImageOptions.Builder()
.cacheInMemory()
.cacheOnDisc()
.build();
}
// TODO Auto-generated constructor stu
/*
* (non-Javadoc)
* @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
*
* This method is responsible for creating row views out of a StackSite object that can be put
* into our ListView
*/
@Override
public View getView(int pos, View convertView, ViewGroup parent){
RelativeLayout row = (RelativeLayout)convertView;
Log.i("PremierLeague", "getView pos = " + pos);
if(null == row){
//No recycled View, we have to inflate one.
LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (RelativeLayout)inflater.inflate(R.layout.row_site, null);
}
//Get our View References
final ImageView clubLogo = (ImageView)row.findViewById(R.id.clubLogo);
TextView nameTxt = (TextView)row.findViewById(R.id.nameTxt);
TextView aboutTxt = (TextView)row.findViewById(R.id.aboutTxt);
TextView stadiumTxt = (TextView)row.findViewById(R.id.stadiumTxt);
final ProgressBar indicator = (ProgressBar)row.findViewById(R.id.progress);
//Initially we want the progress indicator visible, and the image invisible
indicator.setVisibility(View.VISIBLE);
clubLogo.setVisibility(View.INVISIBLE);
//Setup a listener we can use to switch from the loading indicator to the Image once it's ready
ImageLoadingListener listener = new ImageLoadingListener(){
@Override
public void onLoadingStarted(String arg0, View arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoadingCancelled(String arg0, View arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
indicator.setVisibility(View.INVISIBLE);
clubLogo.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String arg0, View view, FailReason arg2) {
indicator.setVisibility(View.INVISIBLE);
ImageView imageView = (ImageView) view.findViewById(R.id.clubLogo);
imageView.setVisibility(View.VISIBLE);
}
};
//Load the image and use our options so caching is handled.
imageLoader.displayImage(getItem(pos).getLogo(), clubLogo,options, listener);
//Set the relevant text in our TextViews
nameTxt.setText(getItem(pos).getName());
aboutTxt.setText(getItem(pos).getAbout());
stadiumTxt.setText(getItem(pos).getStadium());
return row;
}
}
傳遞給您的類clubsAdapter
時, Context
為null
,該類在父類ArrayAdaptor
使用,並拋出NullPointerException
。 可能是mContext
成員ClubsDownloadTask
。
另外
課程應以大寫字母開頭: ClubsAdapter
而不是clubsAdapter
將您的onCreateView更改為以下內容:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_1, container, false);
Log.i("PremierLeague", "OnCreateView()");
clubs = (ListView)rootView.findViewById(R.id.clubsList);
clubs.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
return rootView;
}
還覆蓋此功能:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(isNetworkAvailable()){
Log.i("PremierLeague", "starting download Task");
ClubsDownloadTask download = new ClubsDownloadTask();
download.execute();
}else{
mAdapter = new clubsAdapter(getActivity().getApplicationContext(), -1,
ClubsXmlPullParser.getItemsFromFile(getActivity()));
clubs.setAdapter(mAdapter);
}
}
問題在於onCreateView getActivity返回null,因為此片段尚未附加到活動。
您需要覆蓋onActivityCreated函數並將適配器設置為其中的列表。
ListView clubs = (ListView) getView().findViewById(R.id.clubsList);
我通過將上面的代碼添加到onPostExecute來解決了該問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.