因此對於一個班級,我必須使用RSS閱讀器程序。代碼基本上給了我們,但我們必須把它放到android中。該代碼在Java中工作,但是當我嘗試在Android中運行它時修改它以便它將更改TextViews文本時,它會進行編譯,但是在運行時,我收到了一堆我不明白的錯誤。這是我的代碼。我認爲這與我使用setText()或TextView對象有關。Java程序有效,但不會在Android Studio中運行
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
TextView rssFeed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rssFeed = (TextView)findViewById(R.id.txtFeed);
rssFeed.setText(readRSS("http://rss.cnn.com/rss/edition.rss"));
}
public static String readRSS(String urlAddress)
{
try{
URL rssUrl = new URL(urlAddress);
BufferedReader in = new BufferedReader(new InputStreamReader(rssUrl.openStream()));
String sourceCode = "";
String line;
while ((line = in.readLine()) != null) {
int titleEndIndex = 0;
int titleStartIndex = 0;
while (titleStartIndex >= 0) {
titleStartIndex = line.indexOf("<title>", titleEndIndex);
if (titleStartIndex >= 0) {
titleEndIndex = line.indexOf("</title>", titleStartIndex);
sourceCode += line.substring(titleStartIndex + "<title>".length(), titleEndIndex) + "\n";
}
}
}
in.close();
return sourceCode;
}
catch (MalformedURLException ue) {
System.out.print("Malformed URL");
}
catch (IOException ioe)
{
System.out.println("Something went wrong reading the contents");
}
return "Program Failed";
}
的XML是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtFeed" />
</RelativeLayout>
的logcat的是
10-05 23:05:49.839 2087-2087/com.example.andrewsaarima.rssreader I/art: Not late-enabling -Xcheck:jni (already on)
10-05 23:05:49.840 2087-2087/com.example.andrewsaarima.rssreader I/art: Late-enabling JIT
10-05 23:05:49.843 2087-2087/com.example.andrewsaarima.rssreader I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
10-05 23:05:49.996 2087-2087/com.example.andrewsaarima.rssreader W/System: ClassLoader referenced unknown path: /data/app/com.example.andrewsaarima.rssreader-2/lib/x86
10-05 23:05:50.220 2087-2087/com.example.andrewsaarima.rssreader D/AndroidRuntime: Shutting down VM
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: FATAL EXCEPTION: main
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: Process: com.example.andrewsaarima.rssreader, PID: 2087
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.andrewsaarima.rssreader/com.example.andrewsaarima.rssreader.MainActivity}: android.os.NetworkOnMainThreadException
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: Caused by: android.os.NetworkOnMainThreadException
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at java.net.InetAddress.getAllByName(InetAddress.java:215)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at java.net.URL.openStream(URL.java:470)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.example.andrewsaarima.rssreader.MainActivity.readRSS(MainActivity.java:28)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.example.andrewsaarima.rssreader.MainActivity.onCreate(MainActivity.java:21)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6237)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-05 23:05:50.487 2087-2087/com.example.andrewsaarima.rssreader E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-05 23:05:53.950 2087-2094/com.example.andrewsaarima.rssreader W/art: Suspending all threads took: 25.938ms
10-05 23:05:55.994 2087-2087/? I/Process: Sending signal. PID: 2087 SIG: 9
PLZ顯示logcat的.... –
'NetworkOnMainThreadException' - 谷歌認爲。 –