gpt4 book ai didi

android - Java 程序可以运行,但无法在 Android Studio 中运行

转载 作者:太空宇宙 更新时间:2023-11-03 12:31:24 28 4
gpt4 key购买 nike

所以对于一门课,我必须使用 RSS 阅读器程序。代码基本上给了我们,但我们必须把它放到android中。该代码在 Java 中运行,但是当我尝试在 Android 中运行它并对其进行修改以更改 TextViews 文本时,它会编译但在运行时我会遇到大量我不理解的错误。这是我的代码。我认为这与我使用 setText() 或 TextView 对象有关。

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

最佳答案

您应该在 android 中使用 AsyncTask 来调用 rss 提要 url。实现起来非常简单。您应该做的第一件事是阅读来自 here 的关于 AsyncTask 的内容。如果你不知道的话。然后根据 AsyncTask 实现你的代码,比如:

    private class RSSTask extends AsyncTask<String, Void, String>
{
ProgressDialog pd;

//onPreExecute() will be called before entering into background thread
@Override
protected void onPreExecute()
{
super.onPreExecute();

pd = ProgressDialog.show(context, "RSS Feed", "Fetching Rss Feed Please Wait");

}

//this is background thread called after onPreExecute()
@Override
protected String doInBackground(String... params)
{
String rssResponse = readRSS(params[0])

return rssResponse;
}

//called after doInBackground() finished
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);

pd.dismiss();

rssFeed.setText(result);// here result will be returning string from doInBackground()
}
}

将整个 AsyncTask 类复制到您的 MainActivity 然后

替换:

rssFeed.setText(readRSS("http://rss.cnn.com/rss/edition.rss"));

RSSTask rssT = new RSSTask();
rssT.execute("http://rss.cnn.com/rss/edition.rss");

希望你能得到想要的输出。

关于android - Java 程序可以运行,但无法在 Android Studio 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32961258/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com