Note that there are some explanatory texts on larger screens.

plurals
  1. POButton linking to a new page, doesn't work
    text
    copied!<p>I am creating a program that is supposed to show an RSS Feed once a button is clicked.</p> <p>I have a main class, a Screen 1, and Screen 2 and a main xml file and screen 1 and screen 2 in the layout.</p> <p>Now I tried before with the main xml and class being the RSS Feed and it worked fine</p> <p>However, now when I try to make Screen1 (screen1) the place where the feed is displayed, I just get a black screen.</p> <p>I am wondering why this is happening, as I told by Screen1 class to link to the screen1 xml file.</p> <p>Main Class:</p> <pre><code>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); } }); } } </code></pre> <p>Screen 1:</p> <pre><code> 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&lt;String&gt; headlines; List&lt;String&gt; 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&lt;String&gt;(); links = new ArrayList&lt;String&gt;(); try { URL url = new URL("http://news.google.com/news?ned=us&amp;topic=h&amp;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 "&lt;title&gt;" tag which appears inside the "&lt;item&gt;" tag. * However, we should take in consideration that the rss feed name also is enclosed in a "&lt;title&gt;" tag. * As we know, every feed begins with these lines: "&lt;channel&gt;&lt;title&gt;Feed_Name&lt;/title&gt;...." * so we should skip the "&lt;title&gt;" tag which is a child of "&lt;channel&gt;" tag, * and take in consideration only "&lt;title&gt;" tag which is a child of "&lt;item&gt;" * * 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 &amp;&amp; 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&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(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; } } </code></pre> <p>Main xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:text="@string/welcome" &gt; &lt;requestFocus /&gt; &lt;/EditText&gt; &lt;TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /&gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;/RelativeLayout&gt; &lt;RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; </code></pre> <p>screen1 xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;ListView android:id="@+android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p>So to summarize: It worked when I just had the main class and xml, but now that I am linking to a new class and xml, it doesn't work. Why?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload