如果使用连接管理器没有互联网连接,我正在尝试关闭应用。然而,在我实施这些代码之后。我似乎无法让它运行,它只是崩溃与异常。下面是我的代码和我的logcat。
public class BuyFragment extends SherlockListFragment implements
ActionBar.TabListener {
static final String URL = "https://myxml.xml";
static final String KEY_SONG = "song";
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_CAT_ARTIST = "artistcat";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_BIG_URL = "big_url";
static final String KEY_CAT_URL = "cat_url";
static final String KEY_DESC = "cat_desc";
ArrayList<HashMap<String, String>> menuItems;
ListAdapter adapter;
Context appContext;
Activity mAct;
private Fragment mFragment;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
if (!isNetworkAvailable()) {
Toast.makeText(getActivity(),
"Internet Connection Required", Toast.LENGTH_SHORT).show();
//this.finish();
mAct.finish();
} else {
new loadListView().execute();
}
super.onActivityCreated(savedInstanceState);
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
public class loadListView extends AsyncTask<Integer, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Integer... args) {
// updating UI from Background Thread
menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_CAT_ARTIST, parser.getValue(e, KEY_CAT_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
map.put(KEY_BIG_URL, parser.getValue(e, KEY_BIG_URL));
map.put(KEY_CAT_URL, parser.getValue(e, KEY_CAT_URL));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
// adding HashList to ArrayList
menuItems.add(map);
}
return null;
}
@Override
protected void onPostExecute(String args) {
if (getActivity() != null) {
adapter = new MainPageLazyAdapter(getActivity(), menuItems);
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String thumburl;
String bigurl;
String caturl;
String title;
String desc;
String artist;
Intent in;
title = ((TextView) view.findViewById(R.id.title))
.getText().toString();
artist = ((TextView) view.findViewById(R.id.artist))
.getText().toString();
desc = ((TextView) view.findViewById(R.id.desc))
.getText().toString();
thumburl = ((TextView) view
.findViewById(R.id.thumb_image)).getText()
.toString();
caturl = ((TextView) view.findViewById(R.id.cat))
.getText().toString();
bigurl = ((TextView) view.findViewById(R.id.big_image))
.getText().toString();
in = new Intent(getActivity(),
SingleImageViewActivity.class);
in.putExtra(KEY_TITLE, title);
in.putExtra(KEY_DESC, desc);
in.putExtra(KEY_CAT_URL, caturl);
in.putExtra(KEY_THUMB_URL, thumburl);
in.putExtra(KEY_BIG_URL, bigurl);
in.putExtra(KEY_CAT_ARTIST, artist);
startActivity(in);
}
});
}
}
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mFragment = new BuyFragment();
ft.add(android.R.id.content, mFragment);
ft.attach(mFragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
LOGCAT:
11-02 14:56:48.390: E/AndroidRuntime(13446): FATAL EXCEPTION: main
11-02 14:56:48.390: E/AndroidRuntime(13446): java.lang.NullPointerException
11-02 14:56:48.390: E/AndroidRuntime(13446): at in.wptrafficanalyzer.actionbarsherlocknavtab.BuyFragment.onActivityCreated(BuyFragment.java:57)
11-02 14:56:48.390: E/AndroidRuntime(13446): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:891)
11-02 14:56:48.390: E/AndroidRuntime(13446): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
11-02 14:56:48.390: E/AndroidRuntime(13446): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
11-02 14:56:48.390: E/AndroidRuntime(13446): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
11-02 14:56:48.390: E/AndroidRuntime(13446): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
11-02 14:56:48.390: E/AndroidRuntime(13446): at android.os.Handler.handleCallback(Handler.java:587)
11-02 14:56:48.390: E/AndroidRuntime(13446): at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 14:56:48.390: E/AndroidRuntime(13446): at android.os.Looper.loop(Looper.java:130)
11-02 14:56:48.390: E/AndroidRuntime(13446): at android.app.ActivityThread.main(ActivityThread.java:3691)
11-02 14:56:48.390: E/AndroidRuntime(13446): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 14:56:48.390: E/AndroidRuntime(13446): at java.lang.reflect.Method.invoke(Method.java:507)
11-02 14:56:48.390: E/AndroidRuntime(13446): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
11-02 14:56:48.390: E/AndroidRuntime(13446): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
11-02 14:56:48.390: E/AndroidRuntime(13446): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
mAct.finish()
以上行导致了问题。
此处mAct
为null
,您确定mAct
已在某处初始化并且在
mAct.finish()
致电。
答案 1 :(得分:0)
只需写完();或ActivityName.finish(); 代码是好的我见过....你差不多完成..如果出现问题然后回到我身边我会写一些代码
答案 2 :(得分:0)
使用getActivity().finish()
代替mAct.finish()
。在您的代码mAct
未初始化