2012-06-29 44 views
0

我正在创建一个应用程序,该应用程序在单击按钮时应显示RSS源。链接到新页面的按钮不起作用

我有一个主类,屏幕1和屏幕2 和一个主要的xml文件和屏幕1和屏幕2的布局。

现在我与主要XML和类是RSS源之前试过它工作得很好

然而,现在,当我试图让屏幕1(屏幕截图1),其中显示饲料的地方,我只是得到一个黑色屏幕。

我想知道为什么会发生这种情况,正如我通过Screen1类告诉链接到screen1 xml文件一样。

主类:

import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 

import android.widget.Button; 


public class SImpleRssReader2Activity extends Activity { 





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


     Button videoNext = (Button) findViewById(R.id.videoButton) ; 
     videoNext.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       setContentView(R.layout.screen2); 



       // Intent i = new Intent(); 
       // i.setClassName("rahul.application.WunApp", "rahul.application.WunApp.screen1"); 
       // startActivity(i); 
      } 
     }); 

     Button newsNext = (Button) findViewById(R.id.newsButton); 
     newsNext.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       setContentView(R.layout.screen1); 

       // Intent i = new Intent(); 
       // i.setClassName("rahul.application.WunApp", "rahul.application.WunApp.screen1"); 
       // startActivity(i); 
      } 
     }); 




} 
} 

屏幕1:

import java.io.IOException; 
    import java.io.InputStream; 
    import java.net.MalformedURLException; 
    import java.net.URL; 
    import java.util.ArrayList; 
    import java.util.List; 

    import org.xmlpull.v1.XmlPullParser; 
    import org.xmlpull.v1.XmlPullParserException; 
    import org.xmlpull.v1.XmlPullParserFactory; 


    import android.app.ListActivity; 
    import android.content.Intent; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.view.View; 

    import android.widget.ArrayAdapter; 

    import android.widget.ListView; 

    public class Screen1 extends ListActivity { 
     /** Called when the activity is first created. */ 
     List<String> headlines; 
     List<String> links; 





    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     Uri uri = Uri.parse((String) links.get(position)); 
     Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
     startActivity(intent); 
    } 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.screen1); 
     // Initializing instance variables 
      headlines = new ArrayList<String>(); 
      links = new ArrayList<String>(); 



      try { 
       URL url = new URL("http://news.google.com/news?ned=us&topic=h&output=rss"); 

       XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
       factory.setNamespaceAware(false); 
       XmlPullParser xpp = factory.newPullParser(); 

        // We will get the XML from an input stream 
       xpp.setInput(getInputStream(url), "UTF_8"); 

        /* We will parse the XML content looking for the "<title>" tag which appears inside the "<item>" tag. 
        * However, we should take in consideration that the rss feed name also is enclosed in a "<title>" tag. 
        * As we know, every feed begins with these lines: "<channel><title>Feed_Name</title>...." 
        * so we should skip the "<title>" tag which is a child of "<channel>" tag, 
        * and take in consideration only "<title>" tag which is a child of "<item>" 
        * 
        * In order to achieve this, we will make use of a boolean variable. 
        */ 
       boolean insideItem = false; 

        // Returns the type of current event: START_TAG, END_TAG, etc.. 
       int eventType = xpp.getEventType(); 
       while (eventType != XmlPullParser.END_DOCUMENT) { 
        if (eventType == XmlPullParser.START_TAG) { 

         if (xpp.getName().equalsIgnoreCase("item")) { 
          insideItem = true; 
         } else if (xpp.getName().equalsIgnoreCase("title")) { 
          if (insideItem) 
           headlines.add(xpp.nextText()); //extract the headline 
         } else if (xpp.getName().equalsIgnoreCase("link")) { 
          if (insideItem) 
           links.add(xpp.nextText()); //extract the link of article 
         } 
        }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){ 
         insideItem=false; 
        } 

        eventType = xpp.next(); //move to next element 
       } 

      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (XmlPullParserException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      // Binding data 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, headlines); 

      setListAdapter(adapter); 



     } 
    public InputStream getInputStream(URL url) { 
      try { 
       return url.openConnection().getInputStream(); 
      } catch (IOException e) { 
       return null; 
      } 
     } 

主要的xml:

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



    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:text="@string/welcome" > 

     <requestFocus /> 

    </EditText> 


    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 


    <TextView 
     android:id="@+id/textView6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 


    <TextView 
     android:id="@+id/textView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 






     <Button 
      android:id="@+id/newsButton" 
      android:layout_width="152dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:text="@string/button1" /> 

    </RelativeLayout> 


    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 


    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 


     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 





     <Button 
      android:id="@+id/videoButton" 
      android:layout_width="156dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:text="@string/button3" /> 

    </RelativeLayout> 

</LinearLayout> 

屏蔽1的xml:

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



<ListView 
android:id="@+android:id/list" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" > 
</ListView> 

</LinearLayout> 

所以总结一下:当我只有主类和xml时它工作,但现在我链接到一个新的类和xml,它不起作用。为什么?

回答

0

点1 - 你不应该做与网络有关的工作,在UI线程使用的AsyncTask或处理线程

点2 - 你必须在main.xml中,但在屏幕截图1仅适用于XML列表视图中的各种意见,可能您就有可能没有在屏幕截图1数据的敌人名单

点3-使用“@android:ID /列表”为ID为ListActivity

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 
+0

我在screen1中的列表数据应该是Screen1.java中提供的RSS源...但是我能够在不使用Asynctask的情况下获得RSS源(不想尝试使用它,因为没有学习了如何使用Asynctask)在我的main.xml中,我所需要的就是能够使用main.xml中的链接在screen1.xml中显示......我知道可能是更有效的方法,但对于我这似乎是最容易理解的。第3点也没有做任何事情。尽管感谢您的帮助。 –

0

是否有任何与这些在你的代码被注释掉

//Intent i = new Intent(); 
      // i.setClassName("rahul.application.WunApp",  "rahul.application.WunApp.screen1"); 
      // startActivity(i);  

你确定在你的清单,你把应用程序块

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
<activity android:name=".SImpleRssReader2Activity" /> // your class names 
<activity android:name=".screen1" /> 
<activity android:name=".screen2" /> 

您使用意图时通常需要这些里面。

侧栏: 真的很有趣,你的工作和Iam一样。我需要一个RSS提要的东西,并尝试相同的一段代码。根本不适合我,不知道你是如何得到它的,但很好。我为我的程序使用了一段不同的代码。并不是真的习惯于像java这样的高级语言,我认为一旦你开始使用C语言就更容易。

相关问题