1

ListFragmentRSS 데이터로 작성되었습니다. 유일한 문제는 내가 NullPointerException 그 강제로 내 애플 리케이션을 받고있다. 이 코드에ListFragment NullPointerException 오류

03-30 15:43:44.584: E/AndroidRuntime(28703): at com.example.app.TestFragment$MyCustomAdapter.getView(TestFragment.java:157) 

그 오류 사항 : 다음은 로그 캣입니다,이 클래스에서

listTitle.setText(feed.getList().get(position).getTitle()); 

는; ListFragment. 이 코드에

public final class TestFragment extends ListFragment { 
private static final String KEY_CONTENT = "TestFragment:Content"; 

public static TestFragment newInstance(String content) { 
    TestFragment fragment = new TestFragment(); 

    return fragment; 
} 

private RSSFeed myRssFeed = null; 

private String mContent = "???"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    if ((savedInstanceState != null) 
      && savedInstanceState.containsKey(KEY_CONTENT)) { 
     mContent = savedInstanceState.getString(KEY_CONTENT); 
    } 
} 

private RSSFeed feed = null; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    fillData(); 
    return inflater.inflate(R.layout.list_fragment, container, false); 

} 

public void fillData() { 

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
      .permitAll().build(); 

    StrictMode.setThreadPolicy(policy); 

    try { 
     URL rssUrl = new URL("http://allthingsd.com/feed/"); 
     SAXParserFactory mySAXParserFactory = SAXParserFactory 
       .newInstance(); 
     SAXParser mySAXParser = mySAXParserFactory.newSAXParser(); 
     XMLReader myXMLReader = mySAXParser.getXMLReader(); 
     RSSHandler myRSSHandler = new RSSHandler(); 
     myXMLReader.setContentHandler(myRSSHandler); 
     InputSource myInputSource = new InputSource(rssUrl.openStream()); 
     myXMLReader.parse(myInputSource); 

     myRssFeed = myRSSHandler.getFeed(); 

    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ParserConfigurationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    if (myRssFeed != null) { 
     /* 
     * TextView feedTitle = (TextView) findViewById(R.id.feedtitle); 
     * TextView feedDescribtion = (TextView) 
     * findViewById(R.id.feeddescribtion); TextView feedPubdate = 
     * (TextView) findViewById(R.id.feedpubdate); TextView feedLink = 
     * (TextView) findViewById(R.id.feedlink); 
     * feedTitle.setText(myRssFeed.getTitle()); 
     * feedDescribtion.setText(myRssFeed.getDescription()); 
     * feedPubdate.setText(myRssFeed.getPubdate()); 
     * feedLink.setText(myRssFeed.getLink()); 
     */ 
     MyCustomAdapter adapter = new MyCustomAdapter(getActivity(), 
       R.layout.row, myRssFeed.getList()); 
     setListAdapter(adapter); 

    } 

} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putString(KEY_CONTENT, mContent); 
} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    /* 
    * Intent intent = new Intent(this,ShowDetails.class); Bundle bundle = 
    * new Bundle(); bundle.putString("keyTitle", 
    * myRssFeed.getItem(position).getTitle()); 
    * bundle.putString("keyDescription", 
    * myRssFeed.getItem(position).getDescription()); 
    * bundle.putString("keyLink", myRssFeed.getItem(position).getLink()); 
    * bundle.putString("keyPubdate", 
    * myRssFeed.getItem(position).getPubdate()); intent.putExtras(bundle); 
    * startActivity(intent); 
    */ 

} 

public class MyCustomAdapter extends ArrayAdapter<RSSItem> { 

    public MyCustomAdapter(Context context, int textViewResourceId, 
      List<RSSItem> list) { 
     super(context, textViewResourceId, list); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     View row = convertView; 

     if (row == null) { 
      LayoutInflater inflater = getActivity().getLayoutInflater(); 
      row = inflater.inflate(R.layout.row, parent, false); 
     } 

     TextView listTitle = (TextView) row.findViewById(R.id.textView2); 
     listTitle.setText(feed.getList().get(position).getTitle()); 
     TextView listPubdate = (TextView) row.findViewById(R.id.textView1); 
     listPubdate.setText(myRssFeed.getList().get(position).getPubdate()); 

     if (position % 2 == 0) { 
      listTitle.setBackgroundColor(0xff101010); 
      listPubdate.setBackgroundColor(0xff101010); 
     } else { 
      listTitle.setBackgroundColor(0xff080808); 
      listPubdate.setBackgroundColor(0xff080808); 
     } 

     return super.getView(position, convertView, parent); 

    } 
} 

    } 
+0

해당 행의 각 변수를 확인 했습니까? 먼저'row.xml'은 id가'@ + id/textView2' 인 TextView를 가지고 있습니까? – Sam

답변

2

그 오류 지점 :

listTitle.setText(feed.getList().get(position).getTitle()); 

비교적 쉽게, 간단하게 참조하고 각 변수를 확인하는 NullPointerException이 찾기. 예를 들어, 당신이 feed를 인스턴스화하는 경우, 당신은 여기를 선언 볼 수 없습니다 :

private RSSFeed feed = null; 

하지만 null 이외의 값을 수신하지, 어딘가에 당신이 ... feed = new RSSFeed(); 같은 코드가 필요합니다 (당신이에 의미했다 myRSSFeed을 사용 하시겠습니까?)

+0

여기에 피드를 인스턴스화합니다. myRssFeed = myRSSHandler.getFeed(); 이것은 fillData() 문의 try catch 블록에 있습니다. – AndroidDev

+1

이것은'myRssFeed'가 아닌'feed'입니다. 차이가 있습니까? – Sam

+1

감사합니다. +1하고 답변을 수락했습니다. – AndroidDev

0

null.xml을 참조하십시오. null 포인터가 원인 일 수 있습니다.