2012-11-01 70 views
0

我試圖實施檢查Internet連接到我的片段選項卡。它似乎並沒有運行。任何人都可以檢查我的代碼。我希望我的應用程序在加載片段選項卡之前檢查互聯網連接。Android檢查互聯網連接片段標籤

public class FreeFragment 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; 
    private Fragment mFragment; 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 

     checkInternetConnection(); 

     super.onActivityCreated(savedInstanceState); 
    } 




    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); 

        } 
       }); 
      } 

     } 
    } 

    private boolean checkInternetConnection() { 
     ConnectivityManager cm = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE); 
     // test for connection 
     if (cm.getActiveNetworkInfo() != null 
       && cm.getActiveNetworkInfo().isAvailable() 
       && cm.getActiveNetworkInfo().isConnected()) { 
      new loadListView().execute(); 
      return true; 
     } else { 
      Toast.makeText(appContext, "Internet Connection Required" , Toast.LENGTH_LONG).show(); 


      AlertDialog.Builder builder = new AlertDialog.Builder(appContext); 
      builder.setMessage("Internet Connection Required") 
        .setCancelable(false) 
        .setPositiveButton("Exit", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          System.exit(0); 
         } 

         }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 

       } 

     return false; 
    } 

    @Override 
    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     mFragment = new FreeFragment(); 
     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-01 17:06:30.585: E/AndroidRuntime(28090): FATAL EXCEPTION: main 
11-01 17:06:30.585: E/AndroidRuntime(28090): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.actionbarsherlocknavtab/in.wptrafficanalyzer.actionbarsherlocknavtab.MainActivity}: java.lang.NullPointerException 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.os.Looper.loop(Looper.java:130) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.app.ActivityThread.main(ActivityThread.java:3691) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at java.lang.reflect.Method.invokeNative(Native Method) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at java.lang.reflect.Method.invoke(Method.java:507) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at dalvik.system.NativeStart.main(Native Method) 
11-01 17:06:30.585: E/AndroidRuntime(28090): Caused by: java.lang.NullPointerException 
11-01 17:06:30.585: E/AndroidRuntime(28090): at in.wptrafficanalyzer.actionbarsherlocknavtab.FreeFragment.checkInternetConnection(FreeFragment.java:151) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at in.wptrafficanalyzer.actionbarsherlocknavtab.FreeFragment.onActivityCreated(FreeFragment.java:51) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:891) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.app.Activity.performStart(Activity.java:3817) 
11-01 17:06:30.585: E/AndroidRuntime(28090): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1624) 
11-01 17:06:30.585: E/AndroidRuntime(28090): ... 11 more 
+0

不要使用System.exit(0),顯示你的doInBackGround方法 – Yahor10

+0

對不起,我粘貼了錯誤的logcat。 –

+0

當我運行它時,我的應用程序崩潰了。我不顯示任何logcat –

回答

1

在你的XML的問題。

String xml = parser.getXmlFromUrl(URL); // getting XML 
    Document doc = parser.getDomElement(xml); // getting DOM element 

修復此代碼,它應該工作。

// check internet connection 
public class MainActivity extends FragmentActivity { 

    Fragment1 fragment1 = new Fragment1(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if(!checkInternet()){ 
      // show error View 
     } 
     setContentView(R.layout.activity_main);  
    } 
} 
+0

Yahor,我剛纔給你看的logcat是一個錯誤。我想讓我的應用在分片標籤加載前檢查互聯網連接。 –

+0

使用ConnectivityManager將驗證添加到您的Framgent活動(onCreate方法)中,如果沒有連接顯示錯誤對話框或查看 – Yahor10

+0

請檢查我的編輯。我在我的片段活動中添加了一個connectivityManager。但我得到了一個異常 –