应用程序AndroidAspect意外停止。请再试一次

时间:2014-03-31 20:09:31

标签: android

我刚从上周开始学习android,我也非常喜欢它。但是我在过去两天遇到了一个问题,我找不到任何解决方案。实际上我甚至不知道问题是什么。所以我在这里张贴代码和一切。我希望你们帮助我。

这是活动。

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

1 个答案:

答案 0 :(得分:0)

您应该在应用崩溃时发布您的logcat输出。它使得查找错误变得更加容易。无论如何,我马上看到两件大事错了。您无法初始化View,直到 后,您的布局已经膨胀,这意味着它应该> setContentView()之后

public class MainActivity extends Activity {

TextView tv; // you can declare here


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView)findViewById(R.id.tvRes);  // but initialize here

您拥有它的方式,当您尝试在NPE上调用方法时,您将获得View

我看到的另一个大问题是你需要在后台Thread上进行网络工作。 AsyncTask可以非常方便。

Example of setting up an AsyncTask

AsyncTask Docs