0

我目前正在开发一个 RSS 阅读器,它似乎正在崩溃,我似乎无法弄清楚如何修复它。以前,当单击 ListView 中的项目(文章)时,它会在 Web 视图中打开文章 url。当我尝试打开文章的 web 视图时,它没有崩溃。但是,在进行以下更改后,当我单击 ListView 中的项目(文章)时它会崩溃。

  1. 我删除了 Web View 类和布局文件
  2. 我已经用普通的相对布局替换了布局文件,并设置了新创建的 Activity 的内容视图,我称之为“ArticleActivity”

我打算使用从文章中解析的数据,并能够在表格布局或相对布局视图上显示该数据。

ListRSSItems活动:

public class ListRSSItemsActivity extends ListActivity {

    // Progress Dialog
    private ProgressDialog pDialog;

    // Array list for list view
    ArrayList<HashMap<String, String>> rssItemList = new ArrayList<HashMap<String,String>>();

    RSSParser rssParser = new RSSParser();

    List<RSSItem> rssItems = new ArrayList<RSSItem>();

    RSSFeed rssFeed;

    private static String TAG_TITLE = "title";
    private static String TAG_LINK = "link";
    private static String TAG_DESCRIPTION = "description";
    private static String TAG_PUBDATE = "pub_date";
    private static String TAG_GUID = "guid"; // not used

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rss_item_list);

        // get intent data
        Intent i = getIntent();

        // SQLite Row id
        Integer site_id = Integer.parseInt(i.getStringExtra("id"));

        // Getting Single website from SQLite
        RSSDatabaseHandler rssDB = new RSSDatabaseHandler(getApplicationContext());


        WebSite site = rssDB.getSite(site_id);
        String rss_link = site.getRSSLink();

        /**
         * Calling a backgroung thread will loads recent articles of a website
         * @param rss url of website
         * */
        new loadRSSFeedItems().execute(rss_link);

        // selecting single ListView item
        ListView lv = getListView();

        // Launching new screen on Selecting Single ListItem
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                // getting listview
                View v = (View)view.getParent(); 
                TextView tv = (TextView)v.findViewById(R.id.title);
                String title = tv.getText().toString();
                String description = ((TextView) view.findViewById(R.id.link)).getText().toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(), ArticleActivity.class);
                in.putExtra(TAG_TITLE, title);
                in.putExtra(TAG_DESCRIPTION, description);
                startActivity(in);
            }
        });
    }

    /**
     * Background Async Task to get RSS Feed Items data from URL
     * */
    class loadRSSFeedItems extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(
                    ListRSSItemsActivity.this);
            pDialog.setMessage("Loading recent articles...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting all recent articles and showing them in listview
         * */
        @Override
        protected String doInBackground(String... args) {
            // rss link url
            String rss_url = args[0];

            // list of rss items
            rssItems = rssParser.getRSSFeedItems(rss_url);

            // looping through each item
            for(RSSItem item : rssItems){
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_TITLE, item.getTitle());
                map.put(TAG_LINK, item.getLink());
                map.put(TAG_PUBDATE, item.getPubDate());


                // adding HashList to ArrayList
                rssItemList.add(map);
            }

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed items into listview
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            ListRSSItemsActivity.this,
                            rssItemList, R.layout.rss_item_list_row,
                            new String[] { TAG_LINK, TAG_TITLE, TAG_PUBDATE, TAG_DESCRIPTION },
                            new int[] { R.id.page_url, R.id.title, R.id.pub_date, R.id.link });

                    // updating listview
                    setListAdapter(adapter);
                }
            });
            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String args) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
        }
    }
}

rss_item_list_row.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:padding="8dip">

    <TextView android:id="@+id/page_url"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"/>

    <!-- Article title -->
    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="1dip"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textColor="#303030" />

    <!-- published date -->
    <TextView
        android:id="@+id/pub_date"
        android:layout_below="@id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="3dip"
        android:textSize="14sp"
        android:textColor="#b70400"/>

    <!-- article description -->
    <TextView android:id="@+id/link"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:paddingBottom="6dip"
        android:textSize="15sp"
        android:textColor="#8d8d8d"
        android:layout_below="@id/pub_date"/>
</RelativeLayout>

堆栈跟踪:

    USER_COMMENT=null
ANDROID_VERSION=4.1.2
APP_VERSION_NAME=1.0
BRAND=samsung
PHONE_MODEL=GT-N8000
CUSTOM_DATA=
STACK_TRACE=java.lang.NullPointerException
at com.j.infographx.ListRSSItemsActivity$1.onItemClick(ListRSSItemsActivity.java:74)
at android.widget.AdapterView.performItemClick(AdapterView.java:301)
at android.widget.AbsListView.performItemClick(AbsListView.java:1287)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3078)
at android.widget.AbsListView$1.run(AbsListView.java:4161)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)

据我所知,我的 ListRSSItemsActivity 类的第 74 行似乎存在问题,即

String title = ((TextView) view.findViewById(R.id.view_title)).getText().toString();

我不知道如何解决它。有人可以帮忙吗?

4

2 回答 2

1

TextView 文本未初始化,因此 getText() 返回空值。

于 2013-09-23T12:52:46.283 回答
0

试试这个

   View v =(View) view.getParent();
   TextView tv =  (TextView)v.findViewById(R.id.title). //id is title
   String title = tv.getText().toString(); 

你有这个

  <TextView
    android:id="@+id/title"

<!-- article description -->
<TextView android:id="@+id/link"

你有

  (TextView) view.findViewById(R.id.article_content)
  //There is no view with id article_content in your xml

应该

 TextView des =  (TextView)v.findViewById(R.id.link);
 String description = des.getText().toString();  
于 2013-09-23T13:00:48.843 回答