2014-04-04 14 views
0

我有一個XML文件,用於在我的應用中填充ListView。 在開始時,我使用了一個臨時網站來儲存我的文件; http://www.tempsend.com/7CC3EEA1CA/6335/test6.xml,但作爲字符串在不斷變化,我不能在我的應用程序使用此我將每天需要更新文件。在XML解析器中使用Internet位置

因此,我有權訪問我放置XML文件的服務器; http://www.smashing72.nl/test6.xml

該應用程序與第一個URL完全吻合,但它在第二個應用程序啓動時關閉,這怎麼可能?

更重要的是,我該如何解決這個問題?

public class Zoeken extends ActionBarActivity { 

    // Declare Variables 
    ListView listview; 
    LVA adapter; 
    ProgressDialog mProgressDialog; 
    EditText editsearch; 
    static String ARTNR = "ArtNr"; 
    static String ARTOM = "ArtOm"; 
    static String OPSLAGLOC = "OpslagLoc"; 
    static String AANTAL = "Aantal"; 
    private List<Test4> test4List = null; 

    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listview_main); 
     // Execute DownloadXML AsyncTask 
     new DownloadXML().execute(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu items for use in the action bar 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.zoeken, menu); 
     return super.onCreateOptionsMenu(menu); 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      // action with ID action_refresh was selected 
      case R.id.refresh: 
       new DownloadXML().execute(); 
       break; 
       default: 
       break; 
     } 
     return true; 
    } 



    @Override 
    public void onResume(){ 
     super.onResume(); 
     setContentView(R.layout.listview_main); 

     // Locate the listview in listview_main.xml 
     listview = (ListView) findViewById(R.id.listviewartnr); 
     //pass results to 
     adapter = new LVA(Zoeken.this, 
       test4List); 
     // Binds the Adapter to the ListView 
     listview.setAdapter(adapter); 
     // Locate the EditText in listview_main.xml 
     editsearch = (EditText) findViewById(R.id.searchartnr); 

     editsearch.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void afterTextChanged(Editable arg0) { 
       // TODO Auto-generated method stub 
       String text = editsearch.getText().toString() 
         .toLowerCase(Locale.getDefault()); 
       adapter.filter(text); 
      } 

      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, 
              int arg2, int arg3) { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void onTextChanged(CharSequence arg0, int arg1, 
             int arg2, int arg3) { 
       // TODO Auto-generated method stub 
      } 
     }); 

    } 



    // DownloadXML AsyncTask 
    private class DownloadXML extends AsyncTask<Void, Void, Void> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Create a progressdialog 
      mProgressDialog = new ProgressDialog(Zoeken.this); 
      // Set progressdialog title 
      mProgressDialog.setTitle("Lexro Artikelnummers Zoeken"); 
      // Set progressdialog message 
      mProgressDialog.setMessage("Laden..."); 
      mProgressDialog.setIndeterminate(false); 
      // Show progressdialog 
      mProgressDialog.show(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      // Create the array 
      test4List = new ArrayList<Test4>(); 
      XMLParser parser = new XMLParser(); 
      // Retrive nodes from the given website URL in XMLParser.class 
      String xml = parser 
        .getXmlFromUrl("http://www.tempsend.com/7CC3EEA1CA/6335/test6.xml"); 
      // Retrive DOM element 
      Document doc = parser.getDomElement(xml); 

      try { 
       // Locate the NodeList name 
       NodeList nl = doc.getElementsByTagName("record"); 
       for (int i = 0; i < nl.getLength(); i++) { 
        Element e = (Element) nl.item(i); 
        Test4 map = new Test4(); 
        map.setArtNr(parser.getValue(e, ARTNR)); 
        map.setArtOm(parser.getValue(e, ARTOM)); 
        map.setOpslagLoc(parser.getValue(e, OPSLAGLOC)); 
        map.setAantal(parser.getValue(e, AANTAL)); 

        test4List.add(map); 
       } 
      } catch (Exception e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 
      return null; 
     } 
     protected void onPostExecute(Void args) { 
      // Locate the listview in listview_main.xml 
      listview = (ListView) findViewById(R.id.listviewartnr); 
      //pass results to 
      adapter = new LVA(Zoeken.this, 
        test4List); 
      // Binds the Adapter to the ListView 
      listview.setAdapter(adapter); 
      // Close the progressdialog 
      mProgressDialog.dismiss(); 
      // Locate the EditText in listview_main.xml 
      editsearch = (EditText) findViewById(R.id.searchartnr); 

      // Capture Text in EditText 
      editsearch.addTextChangedListener(new TextWatcher() { 

       @Override 
       public void afterTextChanged(Editable arg0) { 
        // TODO Auto-generated method stub 
        String text = editsearch.getText().toString() 
          .toLowerCase(Locale.getDefault()); 
        adapter.filter(text); 
       } 

       @Override 
       public void beforeTextChanged(CharSequence arg0, int arg1, 
               int arg2, int arg3) { 
        // TODO Auto-generated method stub 
       } 

       @Override 
       public void onTextChanged(CharSequence arg0, int arg1, 
              int arg2, int arg3) { 
        // TODO Auto-generated method stub 
       } 
      }); 
     } 


    } 

    // DownloadXML AsyncTask 
    private class recall extends Activity { 

     @Override 
     public void onCreate (Bundle savedInstanceState){ 
      super.onCreate(savedInstanceState); 
      // Locate the listview in listview_main.xml 
      listview = (ListView) findViewById(R.id.listviewartnr); 
      //pass results to 
      adapter = new LVA(Zoeken.this, 
        test4List); 
      // Binds the Adapter to the ListView 
      listview.setAdapter(adapter); 
      // Close the progressdialog 
      mProgressDialog.dismiss(); 
      // Locate the EditText in listview_main.xml 
      editsearch = (EditText) findViewById(R.id.searchartnr); 

      // Capture Text in EditText 
      editsearch.addTextChangedListener(new TextWatcher() { 

       @Override 
       public void afterTextChanged(Editable arg0) { 
        // TODO Auto-generated method stub 
        String text = editsearch.getText().toString() 
          .toLowerCase(Locale.getDefault()); 
        adapter.filter(text); 
       } 

       @Override 
       public void beforeTextChanged(CharSequence arg0, int arg1, 
               int arg2, int arg3) { 
        // TODO Auto-generated method stub 
       } 

       @Override 
       public void onTextChanged(CharSequence arg0, int arg1, 
              int arg2, int arg3) { 
        // TODO Auto-generated method stub 
       } 
      }); 
     } 


    } 



} 

LVA類:

public class LVA extends BaseAdapter { 

    // Declare Variables 
    Context context; 
    LayoutInflater inflater; 
    ArrayList<HashMap<String, String>> overzicht; 
    private List<Test4> test4List = null; 
    private ArrayList<Test4> arraylist; 

    public LVA(Context context, 
       List<Test4> test4List) { 
     this.context = context; 
     this.test4List = test4List; 
     inflater = LayoutInflater.from(context); 
     this.arraylist = new ArrayList<Test4>(); 
     for (Test4 item : test4List) { 
      if (item != null) { 
       arraylist.add(item); 
      } 
     } 
    } 

    public class ViewHolder { 
     TextView ArtNr; 
     TextView ArtOm; 
     TextView OpslagLoc; 
     TextView Aantal; 

    } 

    @Override 
    public int getCount() { 
     return test4List.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return test4List.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(final int position, View view, ViewGroup parent) { 
     final ViewHolder holder; 
     if (view == null) { 
      holder = new ViewHolder(); 
      view = inflater.inflate(R.layout.listview_item, null); 
      // Locate the TextViews in listview_item.xml 
      holder.ArtNr = (TextView) view.findViewById(ArtNr); 
      holder.ArtOm = (TextView) view.findViewById(ArtOm); 
      holder.OpslagLoc = (TextView) view.findViewById(OpslagLoc); 
      holder.Aantal = (TextView) view.findViewById(Aantal); 
      view.setTag(holder); 
     } else { 
      holder = (ViewHolder) view.getTag(); 
     } 
     // Set the results into TextViews 
     holder.ArtNr.setText(test4List.get(position).getArtNr()); 
     holder.ArtOm.setText(test4List.get(position).getArtOm()); 
     holder.OpslagLoc.setText(test4List.get(position).getOpslagLoc()); 
     holder.Aantal.setText(test4List.get(position).getAantal()); 


     // Listen for ListView Item Click 
     view.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // Send single item click data to SingleItemView Class 
       Intent intent = new Intent(context, SingleItemView.class); 
       // Pass all data rank 
       intent.putExtra("ArtNr", 
         (test4List.get(position).getArtNr())); 
       // Pass all data country 
       intent.putExtra("ArtOm", 
         (test4List.get(position).getArtOm())); 
       // Pass all data population 
       intent.putExtra("OpslagLoc", 
         (test4List.get(position).getOpslagLoc())); 
       // Pass all data flag 
       intent.putExtra("Aantal", 
         (test4List.get(position).getAantal())); 
       // Pass all data population 
       // Start SingleItemView Class 
       context.startActivity(intent); 
      } 
     }); 
     return view; 
    } 

    // Filter Class ; Kijk naar getArtNr voor aanpassen zoekvariabele 
    public void filter(String charText) { 
     charText = charText.toLowerCase(Locale.getDefault()); 
     test4List.clear(); 
     if (charText.length() == 0) { 
      for (Test4 item : test4List) { 
       if (item != null) { 
        arraylist.add(item); 
       } else { 
        for (Test4 wp : arraylist) { 
         if (wp.getArtNr().toLowerCase(Locale.getDefault()) 
           .contains(charText)) { 
          test4List.add(wp); 
         } 
         if (wp.getArtOm().toLowerCase(Locale.getDefault()) 
           .contains(charText)) { 
          test4List.add(wp); 
         } 
         if (wp.getOpslagLoc().toLowerCase(Locale.getDefault()) 
           .contains(charText)) { 
          test4List.add(wp); 
         } 
        } 
       } 
       notifyDataSetChanged(); 
      } 
     } 
    } 
} 

的logcat:

04-11 10:37:04.371 18219-18251/com.androidbegin.searchxmlparse E/DataScheduler﹕ isDataSchedulerEnabled():false 
04-11 10:37:04.641 18219-18251/com.androidbegin.searchxmlparse E/Error:﹕ expected: /META read: HEAD (position:END_TAG </HEAD>@11:8 in [email protected]) 
04-11 10:37:04.641 18219-18251/com.androidbegin.searchxmlparse W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x41818e48) 
04-11 10:37:04.641 18219-18251/com.androidbegin.searchxmlparse E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1 
    Process: com.androidbegin.searchxmlparse, PID: 18219 
    java.lang.RuntimeException: An error occured while executing doInBackground() 
      at android.os.AsyncTask$3.done(AsyncTask.java:300) 
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
      at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
      at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
      at java.lang.Thread.run(Thread.java:841) 
    Caused by: java.lang.NullPointerException: println needs a message 
      at android.util.Log.println_native(Native Method) 
      at android.util.Log.e(Log.java:232) 
      at com.androidbegin.searchxmlparse.Zoeken$DownloadXML.doInBackground(Zoeken.java:194) 
      at com.androidbegin.searchxmlparse.Zoeken$DownloadXML.doInBackground(Zoeken.java:154) 
      at android.os.AsyncTask$2.call(AsyncTask.java:288) 
      at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
            at java.lang.Thread.run(Thread.java:841) 
04-11 10:37:05.551 18219-18219/com.androidbegin.searchxmlparse E/WindowManager﹕ android.view.WindowLeaked: Activity com.androidbegin.searchxmlparse.Zoeken has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{428ba790 V.E..... R......D 0,0-1026,486} that was originally added here 
      at android.view.ViewRootImpl.<init>(ViewRootImpl.java:377) 
      at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248) 
      at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
      at android.app.Dialog.show(Dialog.java:296) 
      at com.androidbegin.searchxmlparse.Zoeken$DownloadXML.onPreExecute(Zoeken.java:166) 
      at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) 
      at android.os.AsyncTask.execute(AsyncTask.java:535) 
      at com.androidbegin.searchxmlparse.Zoeken.onCreate(Zoeken.java:49) 
      at android.app.Activity.performCreate(Activity.java:5275) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250) 
      at android.app.ActivityThread.access$800(ActivityThread.java:139) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5105) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608) 
      at dalvik.system.NativeStart.main(Native Method) 
+0

請發佈您的logcat輸出,否則將難以查明問題。 –

+0

好了,所以回顧這段我可以告訴一個發生錯誤,它拋出一個NullPointerException。但爲什麼它會這樣做,雖然它是完全相同的文件,但存儲在另一個位置?! – user2881877

回答

0

的logcat的告訴你,幾乎什麼是錯的。有趣的部分是:

Caused by: java.lang.NullPointerException 
      at java.util.ArrayList.addAll(ArrayList.java:188) 
      at com.androidbegin.searchxmlparse.XMLParser.LVA.<init>(LVA.java:38) 

有一個NullPointerException當您嘗試添加對象的ArrayList。所以問題只能是:

  1. 您的列表爲空。這是不可能的,因爲它與其他rss提要一起工作。
  2. Rss Feed未正確解析,並且列表中有一些對象或這些對象的某些字段爲空。

你應該把一個斷點放到你的LVA類的構造函數中(我認爲它是你的適配器)並調試到底發生了什麼。您撥打addAll(...)的行是問題發生的地方。由於您尚未發佈LVA類的代碼,因此我無法進一步幫助您。

編輯:

嘗試用這種替代線路this.arraylist.addAll(test4List);在LVA類的構造函數:

for(Test4 item : test4List) { 
    if(item != null) { 
     arrayList.add(item); 
    } 
} 

如果這不起作用,斷點添加到LVA的構造並調試它,直到找出問題所在。

+0

現在我開始瞭解它,我發現奇怪的是,它確實在一個feed上工作,但不在另一個feed上,所以它必須對XML文件存儲在的位置做些什麼?!我無法找到關於如何存儲XML文件進行解析的任何信息。 不管怎麼說,我添加了LVA類原職,這的確是我的ListViewAdapter。謝謝你的協助!! – user2881877

+0

向構造函數添加一個斷點並對其進行調試。看看變量的所有值。很可能列表test4List中的某個元素爲空。我個人有一條規則:永遠不要相信'addAll()'。過去,我在addAll()方面遇到了很多問題,因爲它有時無法正常工作。我會用建議編輯我的答案。 –

+0

我編輯了我的答案。 –