0

私の友人のウェブサイトの内容でjsoupを使用してフラグメント内にカスタムリストビューを作成しようとしましたが、そのフラグメントを開くたびに私のアプリケーションがクラッシュします。フラグメントを拡張{フラグメント内のリストビューへのjsoup

ListView listview; 
ListViewAdapter adapter; 
ProgressDialog mProgressDialog; 
ArrayList<HashMap<String, String>> arraylist; 
static String TITLE = "title"; 
static String DESCRIPTION = "description"; 
String url = "http://www.writenepal.com/archives/category/poems"; 

public FragmentPoems() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 


    View view = inflater.inflate(R.layout.fragment_poems, container, false); 
    TextView textView = (TextView) view.findViewById(R.id.text_category); 
    textView.setText("Poems"); 

    new JsoupListView().execute(); 

    // Locate the listview in listview_main.xml 
    listview = (ListView) view.findViewById(R.id.my_list_view); 
    // Pass the results into ListViewAdapter.java 
    adapter = new ListViewAdapter(getActivity(), arraylist); 
    // Set the adapter to the ListView 
    listview.setAdapter(adapter); 

    return view; 
} 


private class JsoupListView extends AsyncTask<Void, Void, Void> { 


    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Create a progressdialog 
     mProgressDialog = new ProgressDialog(getContext()); 
     // Set progressdialog title 
     mProgressDialog.setTitle("Write Nepal"); 
     // Set progressdialog message 
     mProgressDialog.setMessage("Loading..."); 
     mProgressDialog.setIndeterminate(false); 
     // Show progressdialog 
     mProgressDialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     arraylist = new ArrayList<HashMap<String, String>>(); 
     try { 
      // Connect to the Website URL 
      /* Document doc = Jsoup.connect(url).get(); 
      // Identify Table Class "worldpopulation" 
      for (Element table : doc.select("table[class=worldpopulation]")) { 

       // Identify all the table row's(tr) 
       for (Element row : table.select("tr:gt(0)")) { 
        HashMap<String, String> map = new HashMap<String, String>(); 

        // Identify all the table cell's(td) 
        Elements tds = row.select("td"); 

        // Identify all img src's 
        Elements imgSrc = row.select("img[src]"); 
        // Get only src from img src 
        String imgSrcStr = imgSrc.attr("src"); 

        // Retrive Jsoup Elements 
        // Get the first td 
        map.put("rank", tds.get(0).text()); 
        // Get the second td 
        map.put("country", tds.get(1).text()); 
        // Get the third td 
        map.put("population", tds.get(2).text()); 
        // Get the image src links 
        map.put("flag", imgSrcStr); 
        // Set all extracted Jsoup Elements into the array 
        */ 
      Document doc = Jsoup.connect(url).get(); 
      Elements titles = doc.select("div.span8 h2"); 
      Elements descriptions = doc.select("div.readmore.archive-inbox i"); 

      int l = titles.size(); 

      for (int i = 0; i < l; i++) { 
       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put("title", titles.get(0).text()); 
       map.put("description", descriptions.get(1).text()); 
       arraylist.add(map); 
       //String tit=titles.get(i).text(); 
       //String des=descriptions.get(i).text(); 
       //data.add(new Contents(tit,des)); 
      } 


      //arraylist.add(map); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 

     return null; 
    } 


    @Override 
    protected void onPostExecute(Void result) { 

     // Close the progressdialog 
     mProgressDialog.dismiss(); 
    } 

} 
package com.bct071.myapplication; 

パブリッククラスListViewAdapterはBaseAdapter {

// Declare Variables 
Context context; 
LayoutInflater inflater; 
ArrayList<HashMap<String, String>> data; 

HashMap<String, String> resultp = new HashMap<String, String>(); 

public ListViewAdapter(Context context, 
         ArrayList<HashMap<String, String>> arraylist) { 
    this.context = context; 
    data = arraylist; 

} 

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

@Override 
public Object getItem(int position) { 
    return null; 
} 

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

public View getView(final int position, View convertView, ViewGroup parent) { 
    // Declare Variables 
    TextView title; 
    TextView description; 
    //TextView population; 


    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View itemView = inflater.inflate(R.layout.abc_show_titles, parent, false); 
    // Get the position 
    resultp = data.get(position); 

    // Locate the TextViews in listview_item.xml 
    title = (TextView) itemView.findViewById(R.id.text_title); 
    description = (TextView) itemView.findViewById(R.id.text_description); 
    //population = (TextView) itemView.findViewById(R.id.population); 

    // Locate the ImageView in listview_item.xml 
    // flag = (ImageView) itemView.findViewById(R.id.flag); 

    // Capture position and set results to the TextViews 
    title.setText(resultp.get(FragmentPoems.TITLE)); 
    description.setText(resultp.get(FragmentPoems.DESCRIPTION)); 
    //population.setText(resultp.get(MainActivity.POPULATION)); 
    // Capture position and set results to the ImageView 
    // Passes flag images URL into ImageLoader.class 
    //imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), flag); 
    // Capture ListView item click 

    /* 
    itemView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // Get the position 
      resultp = data.get(position); 
      Intent intent = new Intent(context, SingleItemView.class); 
      // Pass all data rank 
      intent.putExtra("rank", resultp.get(MainActivity.RANK)); 
      // Pass all data country 
      intent.putExtra("country", resultp.get(MainActivity.COUNTRY)); 
      // Pass all data population 
      intent.putExtra("population",resultp.get(MainActivity.POPULATION)); 
      // Pass all data flag 
      intent.putExtra("flag", resultp.get(MainActivity.FLAG)); 
      // Start SingleItemView Class 
      context.startActivity(intent); 

     } 
    }); 
    */ 
    return itemView; 
} 

}

を拡張しますここでエラーが私は

を取得している
         Process: com.bct071.myapplication, PID: 22075 
                     java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference 
                      at com.bct071.myapplication.ListViewAdapter.getCount(ListViewAdapter.java:37) 
                      at android.widget.ListView.setAdapter(ListView.java:491) 
                      at com.bct071.myapplication.FragmentPoems.onCreateView(FragmentPoems.java:62) 
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) 
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742) 
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

5月28日13:55:37.050 1619年から1633年/ system_processのW/ActivityManager:フォース仕上げ活動com.bct071.myapplicationにしてみてくださいListViewAdapterクラス上/ .MainActivity

答えて

0

ArrayListの初期化..

ArrayList<HashMap<String, String>> data=new ArrayList<Hashmap<String,String>>(); 

または

ArrayList<HashMap<String, String>> data=null; 
+0

を私がすることを試みたが、私はそのFRAGに引き返す場合もう一度、アプリのクラッシュ – prasid444

関連する問題