我希望能够将服务器上的文本文件内容显示为textview。请有人告诉我为什么当我尝试运行这个应用程序时,我一直收到NullPointerException。我甚至在manifest.xml文件中添加了Internet权限。
package com.example.httpclient_examples;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity implements Runnable {
final String textSource = "http://www.website.com/text.txt";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
TextView textPrompt = (TextView)findViewById(R.id.textprompt);
textPrompt.setText("Wait...");
//thread
Runnable r = new MainActivity();
Thread t = new Thread(r);
t.start();
textPrompt.setText("Finished!");
}
@Override
public void run() {
TextView textMsg = (TextView)findViewById(R.id.textmsg);
try {
URL textUrl = new URL(textSource);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream()));
String StringBuffer;
String stringText = "";
while ((StringBuffer = bufferReader.readLine()) != null) {
stringText += StringBuffer;
}
bufferReader.close();
textMsg.setText(stringText);
}
catch (MalformedURLException e) {
e.printStackTrace();
textMsg.setText(e.toString());
} catch (IOException e) {
e.printStackTrace();
textMsg.setText(e.toString());
}
}
}
07-28 12:06:29.477: I/Choreographer(7101): Skipped 121 frames! The application may be doing too much work on its main thread.
07-28 12:06:31.087: I/Choreographer(7101): Skipped 271 frames! The application may be doing too much work on its main thread.
07-28 12:06:32.387: D/gralloc_goldfish(7101): Emulator without GPU emulation detected.
07-28 12:06:34.817: I/Process(7101): Sending signal. PID: 7101 SIG: 9
07-28 17:25:58.658: D/AndroidRuntime(14851): Shutting down VM
07-28 17:25:58.658: W/dalvikvm(14851): threadid=1: thread exiting with uncaught exception (group=0xb3ab7ba8)
07-28 17:25:58.858: E/AndroidRuntime(14851): FATAL EXCEPTION: main
07-28 17:25:58.858: E/AndroidRuntime(14851): Process: com.example.httpclient_examples, PID: 14851
07-28 17:25:58.858: E/AndroidRuntime(14851): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.httpclient_examples/com.example.httpclient_examples.MainActivity}: java.lang.NullPointerException
07-28 17:25:58.858: E/AndroidRuntime(14851): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-28 17:25:58.858: E/AndroidRuntime(14851): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-28 17:25:58.858: E/AndroidRuntime(14851): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-28 17:25:58.858: E/AndroidRuntime(14851): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-28 17:25:58.858: E/AndroidRuntime(14851): at android.os.Handler.dispatchMessage(Handler.java:102)
07-28 17:25:58.858: E/AndroidRuntime(14851): at android.os.Looper.loop(Looper.java:136)
07-28 17:25:58.858: E/AndroidRuntime(14851): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-28 17:25:58.858: E/AndroidRuntime(14851): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 17:25:58.858: E/AndroidRuntime(14851): at java.lang.reflect.Method.invoke(Method.java:515)
07-28 17:25:58.858: E/AndroidRuntime(14851): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-28 17:25:58.858: E/AndroidRuntime(14851): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-28 17:25:58.858: E/AndroidRuntime(14851): at dalvik.system.NativeStart.main(Native Method)
07-28 17:25:58.858: E/AndroidRuntime(14851): Caused by: java.lang.NullPointerException
07-28 17:25:58.858: E/AndroidRuntime(14851): at android.app.Activity.findViewById(Activity.java:1884)
07-28 17:25:58.858: E/AndroidRuntime(14851): at com.example.httpclient_examples.MainActivity.<init>(MainActivity.java:16)
07-28 17:25:58.858: E/AndroidRuntime(14851): at java.lang.Class.newInstanceImpl(Native Method)
07-28 17:25:58.858: E/AndroidRuntime(14851): at java.lang.Class.newInstance(Class.java:1208)
07-28 17:25:58.858: E/AndroidRuntime(14851): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-28 17:25:58.858: E/AndroidRuntime(14851): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-28 17:25:58.858: E/AndroidRuntime(14851): ... 11 more
07-28 17:26:06.898: I/Process(14851): Sending signal. PID: 14851 SIG: 9
答案 0 :(得分:0)
07-28 17:25:58.858: E/AndroidRuntime(14851): Caused by: java.lang.NullPointerException
07-28 17:25:58.858: E/AndroidRuntime(14851): at android.app.Activity.findViewById(Activity.java:1884)
07-28 17:25:58.858: E/AndroidRuntime(14851): at com.example.httpclient_examples.MainActivity.<init>(MainActivity.java:16)
您发布的代码与堆栈跟踪不匹配。但是,此堆栈跟踪表示您在初始化findViewById()
时过早地调用MainActivity
,可能尝试在第16行初始化成员变量。这就是NPE。要解决此问题,请在findViewById()
之后将onCreate()
移至setContentView()
。
还有其他问题。
这是完全错误的:
Runnable r = new MainActivity();
永远不要使用new
实例化活动。在这种情况下,您可以使用this
来获取对当前活动实例的引用。
在后台帖子run()
中,您无法触及您的用户界面视图,例如setText()
上的TextView
来电。{/ p>
请阅读AsyncTask
上的一些介绍性文字 - 它是在后台线程上工作的基本构建块,同时在UI中提供更新和结果。
答案 1 :(得分:0)
它现在正在工作。我将textview声明移动到oncreate()并更改了Runnable r = new MainActivity();到Runnable r = this;我还使用runOnUiThread()将更新后的文本发布到屏幕上。
package com.example.httpclient_examples;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity implements Runnable {
TextView textPrompt, textMsg;
String stringText = "";
final String textSource = "http://www.website.com/text.txt";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
textPrompt = (TextView)findViewById(R.id.textprompt);
textMsg = (TextView)findViewById(R.id.textmsg);
textPrompt.setText("Wait...");
//thread
Runnable r = this;
Thread t = new Thread(r);
t.start();
textPrompt.setText("Finished!");
}
@Override
public void run() {
try {
URL textUrl = new URL(textSource);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream()));
String StringBuffer;
while ((StringBuffer = bufferReader.readLine()) != null) {
stringText += StringBuffer;
}
bufferReader.close();
runOnUiThread(new Runnable(){
@Override
public void run(){
textMsg.setText(stringText);
}
});
}
catch (MalformedURLException e) {
e.printStackTrace();
textMsg.setText(e.toString());
} catch (IOException e) {
e.printStackTrace();
textMsg.setText(e.toString());
}
}
}