0
我剛剛從上週開始學習android,我也非常享受。但我從過去兩天面臨一個問題,我找不到任何解決方案。其實我甚至不知道問題是什麼。所以我在這裏張貼代碼和一切。我希望你們幫助我。應用程序AndroidAspect已意外停止。請再試一次
這是活動。
package com.example.androidaspect;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv = (TextView)findViewById(R.id.tvRes);
HttpClient httpclient;
HttpGet request;
HttpResponse response;
String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
url = "https://www.google.co.in/?gfe_rd=cr&ei=o7s5U8fxBIrV8gfbzIHYCw";
try {
httpclient = new DefaultHttpClient();
request = new HttpGet(url);
response = httpclient.execute(request);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while((line=rd.readLine())!=null){
tv.append(line);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
這是xml文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvRes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
這裏是清單文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidaspect"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidaspect.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
這裏是日誌文件
04-01 01:52:51.291: D/AndroidRuntime(457): Shutting down VM
04-01 01:52:51.291: W/dalvikvm(457): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-01 01:52:51.402: E/AndroidRuntime(457): FATAL EXCEPTION: main
04-01 01:52:51.402: E/AndroidRuntime(457): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.androidaspect/com.example.androidaspect.MainActivity}: java.lang.NullPointerException
04-01 01:52:51.402: E/AndroidRuntime(457): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
04-01 01:52:51.402: E/AndroidRuntime(457): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-01 01:52:51.402: E/AndroidRuntime(457): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-01 01:52:51.402: E/AndroidRuntime(457): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-01 01:52:51.402: E/AndroidRuntime(457): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 01:52:51.402: E/AndroidRuntime(457): at android.os.Looper.loop(Looper.java:123)
04-01 01:52:51.402: E/AndroidRuntime(457): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-01 01:52:51.402: E/AndroidRuntime(457): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 01:52:51.402: E/AndroidRuntime(457): at java.lang.reflect.Method.invoke(Method.java:521)
04-01 01:52:51.402: E/AndroidRuntime(457): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-01 01:52:51.402: E/AndroidRuntime(457): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-01 01:52:51.402: E/AndroidRuntime(457): at dalvik.system.NativeStart.main(Native Method)
04-01 01:52:51.402: E/AndroidRuntime(457): Caused by: java.lang.NullPointerException
04-01 01:52:51.402: E/AndroidRuntime(457): at android.app.Activity.findViewById(Activity.java:1637)
04-01 01:52:51.402: E/AndroidRuntime(457): at com.example.androidaspect.MainActivity.<init>(MainActivity.java:17)
04-01 01:52:51.402: E/AndroidRuntime(457): at java.lang.Class.newInstanceImpl(Native Method)
04-01 01:52:51.402: E/AndroidRuntime(457): at java.lang.Class.newInstance(Class.java:1429)
04-01 01:52:51.402: E/AndroidRuntime(457): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-01 01:52:51.402: E/AndroidRuntime(457): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
04-01 01:52:51.402: E/AndroidRuntime(457): ... 11 more
我已經添加了日誌文件太是沒有任何用處的。 – Tyagi
是的,這是因爲你在哪裏調用findViewById()。看到我的答案。另外,在我的答案中也指出了在主線程上做一些關於網絡的事情。 – codeMagic
謝謝你的時間。和你的建議,你是說我應該添加緩衝閱讀器的東西到AsyncTask呢? – Tyagi