Android开发:“线程退出未捕获的异常”

时间:2021-04-08 20:32:38

I'm trying to create my first Android App using php but I'm having some difficulties getting started.

我正在尝试使用php创建我的第一个Android应用程序但是我在开始时遇到了一些困难。

When I run my code I get this error log:

当我运行我的代码时,我收到此错误日志:

12-07 19:34:22.095: D/AndroidRuntime(13777): Shutting down VM
12-07 19:34:22.095: W/dalvikvm(13777): threadid=1: thread exiting with uncaught exception (group=0x42300c08)
12-07 19:34:22.095: E/AndroidRuntime(13777): FATAL EXCEPTION: main
12-07 19:34:22.095: E/AndroidRuntime(13777): Process: com.example.parents_gps, PID: 13777
12-07 19:34:22.095: E/AndroidRuntime(13777): java.lang.NullPointerException
12-07 19:34:22.095: E/AndroidRuntime(13777):    at com.example.parents_gps.MainActivity$1.onClick(MainActivity.java:42)
12-07 19:34:22.095: E/AndroidRuntime(13777):    at android.view.View.performClick(View.java:4633)
12-07 19:34:22.095: E/AndroidRuntime(13777):    at android.view.View$PerformClick.run(View.java:19270)
12-07 19:34:22.095: E/AndroidRuntime(13777):    at android.os.Handler.handleCallback(Handler.java:733)
12-07 19:34:22.095: E/AndroidRuntime(13777):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-07 19:34:22.095: E/AndroidRuntime(13777):    at android.os.Looper.loop(Looper.java:146)
12-07 19:34:22.095: E/AndroidRuntime(13777):    at android.app.ActivityThread.main(ActivityThread.java:5602)
12-07 19:34:22.095: E/AndroidRuntime(13777):    at java.lang.reflect.Method.invokeNative(Native Method)
12-07 19:34:22.095: E/AndroidRuntime(13777):    at java.lang.reflect.Method.invoke(Method.java:515)
12-07 19:34:22.095: E/AndroidRuntime(13777):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
12-07 19:34:22.095: E/AndroidRuntime(13777):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
12-07 19:34:22.095: E/AndroidRuntime(13777):    at dalvik.system.NativeStart.main(Native Method)

I think the problem is this "thread exiting with uncaught exception" part, I have no idea what the exception could be or what's causing it.

我认为问题是这个“线程退出未捕获的异常”部分,我不知道异常是什么或导致它的原因。

Here is my code:

这是我的代码:

MainActivity.java: package com.example.parents_gps;

MainActivity.java:package com.example.parents_gps;

package com.example.parents_gps;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
private final String SERVER_ADDRESS = "http://210.119.84.108/Gps";
AsyncTask<Void, Integer, Void> myTask;
ArrayList<String> identity, password;
int dataSize = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button loginBtn, registerBtn;
    final EditText id, pw;
    loginBtn = (Button) findViewById(R.id.loginBtn);
    registerBtn = (Button) findViewById(R.id.registerBtn);
    id = (EditText) findViewById(R.id.id);
    pw = (EditText) findViewById(R.id.pw);

    loginBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (String.valueOf(id.getText()) == String.valueOf(identity.get(0))
                    && String.valueOf(pw.getText()) == String.valueOf(password.get(0))) {
                Toast.makeText(MainActivity.this, "로그인 완료", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, LoginSuccess.class);
                startActivity(intent);
            } else
                Toast.makeText(MainActivity.this, "ID 혹은 P/W가 올바르지 않습니다.", Toast.LENGTH_SHORT).show();
            myTask = new AsyncTask<Void, Integer, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    try {
                        URL url = new URL(SERVER_ADDRESS + "/search_regpar.php");
                        url.openStream();
                        identity = getXmlDataList("searchresult_p.xml", "identity");
                        password = getXmlDataList("searchresult_p.xml", "password");
                        dataSize = identity.size();
                        Log.i("Test", String.valueOf(identity.get(0)));
                        Log.i("Test2", String.valueOf(id.getText()));
                        for (int i = 0; i < dataSize; i++)
                            Log.d("size", String.valueOf(identity.get(i)));
                    } catch (Exception e) {
                        Log.e("Error", e.getMessage());
                    }
                    return null;
                }

                private ArrayList<String> getXmlDataList(String filename, String str) {
                    String rss = SERVER_ADDRESS + "/";
                    ArrayList<String> ret = new ArrayList<String>();

                    try {
                        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                        factory.setNamespaceAware(true);
                        XmlPullParser xpp = factory.newPullParser();
                        URL server = new URL(rss + filename);
                        InputStream is = server.openStream();
                        xpp.setInput(is, "UTF-8");

                        int eventType = xpp.getEventType();

                        while (eventType != XmlPullParser.END_DOCUMENT) {
                            if (eventType == XmlPullParser.START_TAG) {
                                if (xpp.getName().equals(str)) {
                                    ret.add(xpp.nextText());
                                }
                            }
                            eventType = xpp.next();
                        }
                    } catch (Exception e) {
                        Log.e("Error", e.getMessage());
                    }

                    return ret;
                }
            };
            myTask.execute();
        }
    });

    registerBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
            startActivity(intent);
        }
    });
    }
}

Here is my activity_main.xml and AndroidManifest.xml:

这是我的activity_main.xml和AndroidManifest.xml:

activity_main.xml:

<LinearLayout 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:gravity="center"
android:orientation="vertical"
tools:context="com.example.parents_gps.MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="어서오세요"
    android:textSize="20sp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="ID"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="xxx@mail.com"
        android:textSize="20sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="P/W"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/pw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Password를 입력하세요"
        android:textSize="20sp" />
</LinearLayout>

<Button
    android:id="@+id/loginBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="로그인"
    android:textSize="20sp" />

<Button
    android:id="@+id/registerBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="회원가입"
    android:textSize="20sp" />

</LinearLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.parents_gps"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="21" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".InfoTraced"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".InfoParents"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".InfoEdit"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".RegisterTraced"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".RegisterParents"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".LoginSuccess"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".MapView"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".RegisterActivity"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".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>
     <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBJe2YzPGrINQM88R8yXpVisnC1sl1TpT8" />
</application>

</manifest>

1 个解决方案

#1


0  

It looks like you didn't initalize your 'identity' and 'password' ArrayLists. They are null when you compare it here:

看起来你没有把你的'身份'和'密码'ArrayLists初始化。在这里比较它们时它们为空:

if (String.valueOf(id.getText()) == String.valueOf(identity.get(0))
                && String.valueOf(pw.getText()) == String.valueOf(password.get(0))) {

#1


0  

It looks like you didn't initalize your 'identity' and 'password' ArrayLists. They are null when you compare it here:

看起来你没有把你的'身份'和'密码'ArrayLists初始化。在这里比较它们时它们为空:

if (String.valueOf(id.getText()) == String.valueOf(identity.get(0))
                && String.valueOf(pw.getText()) == String.valueOf(password.get(0))) {