0

我有 android Map 应用程序并在 Android Emulator 上对其进行测试。问题是当我在 Emulator 中运行它时它意外停止了。但是 Inernet 在 Emulator 上工作。这是我的 Log Cat。

06-24 21:40:42.285: D/dalvikvm(371): GC_CONCURRENT freed 377K, 48% free 3039K/5831K, external 716K/1038K, paused 7ms+8ms
06-24 21:40:42.755: D/dalvikvm(371): GC_CONCURRENT freed 579K, 50% free 3019K/6023K, external 716K/1038K, paused 6ms+7ms
06-24 21:40:43.015: D/dalvikvm(371): GC_CONCURRENT freed 299K, 47% free 3232K/6023K, external 716K/1038K, paused 7ms+8ms
06-24 21:40:43.285: D/dalvikvm(371): GC_CONCURRENT freed 468K, 47% free 3318K/6215K, external 716K/1038K, paused 6ms+10ms
06-24 21:40:43.575: D/dalvikvm(371): GC_CONCURRENT freed 434K, 46% free 3416K/6279K, external 717K/1038K, paused 5ms+5ms
06-24 21:41:04.119: E/log_tag(371): Error in http connectionjava.net.UnknownHostException: sml.com.pk
06-24 21:41:04.125: E/log_tag(371): Error converting result java.lang.NullPointerException
06-24 21:41:04.145: D/AndroidRuntime(371): Shutting down VM
06-24 21:41:04.145: W/dalvikvm(371): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-24 21:41:04.175: E/AndroidRuntime(371): FATAL EXCEPTION: main
06-24 21:41:04.175: E/AndroidRuntime(371): java.lang.RuntimeException: Unable to start activity ComponentInfo{map2.pkg.pkg/map2.pkg.pkg.GMapsActivity}: java.lang.NullPointerException
06-24 21:41:04.175: E/AndroidRuntime(371):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-24 21:41:04.175: E/AndroidRuntime(371):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-24 21:41:04.175: E/AndroidRuntime(371):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-24 21:41:04.175: E/AndroidRuntime(371):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-24 21:41:04.175: E/AndroidRuntime(371):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 21:41:04.175: E/AndroidRuntime(371):  at android.os.Looper.loop(Looper.java:130)
06-24 21:41:04.175: E/AndroidRuntime(371):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-24 21:41:04.175: E/AndroidRuntime(371):  at java.lang.reflect.Method.invokeNative(Native Method)
06-24 21:41:04.175: E/AndroidRuntime(371):  at java.lang.reflect.Method.invoke(Method.java:507)
06-24 21:41:04.175: E/AndroidRuntime(371):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-24 21:41:04.175: E/AndroidRuntime(371):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-24 21:41:04.175: E/AndroidRuntime(371):  at dalvik.system.NativeStart.main(Native Method)
06-24 21:41:04.175: E/AndroidRuntime(371): Caused by: java.lang.NullPointerException
06-24 21:41:04.175: E/AndroidRuntime(371):  at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
06-24 21:41:04.175: E/AndroidRuntime(371):  at org.json.JSONTokener.nextValue(JSONTokener.java:90)
06-24 21:41:04.175: E/AndroidRuntime(371):  at org.json.JSONArray.<init>(JSONArray.java:87)
06-24 21:41:04.175: E/AndroidRuntime(371):  at org.json.JSONArray.<init>(JSONArray.java:103)
06-24 21:41:04.175: E/AndroidRuntime(371):  at map2.pkg.pkg.GMapsActivity.onCreate(GMapsActivity.java:112)
06-24 21:41:04.175: E/AndroidRuntime(371):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-24 21:41:04.175: E/AndroidRuntime(371):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-24 21:41:04.175: E/AndroidRuntime(371):  ... 11 more
06-24 21:41:11.605: I/Process(371): Sending signal. PID: 371 SIG: 9

这是我的 Manifiest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="map2.pkg.pkg"
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="auto">
    <uses-sdk android:minSdkVersion="10"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".GMapsActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-library android:name="com.google.android.maps" />
    </application>
</manifest> 

谁能告诉我问题是什么以及为什么应用程序没有在模拟器上运行。

更新

GMapsActivity

package map2.pkg.pkg;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.net.ParseException;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;

public class GMapsActivity extends MapActivity {

private MapView mapView;
EditText password;
Button   login;
TextView vwpass;

JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


password=(EditText)findViewById(R.id.pass);
login=(Button)findViewById(R.id.login);
//vwpass=(Button)findViewById(R.id.textView1);
mapView = (MapView) findViewById(R.id.map_view);       
        mapView.setBuiltInZoomControls(true);

        login.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {   
                if(password.getText().toString().equals("tsml")) {

                mapView.setVisibility(View.VISIBLE);
                login.setVisibility(View.INVISIBLE);
                password.setVisibility(View.INVISIBLE);
   //           vwpass.setVisibility(View.INVISIBLE);
                } else {
                Toast.makeText(getBaseContext(), "invalid password - try again",  Toast.LENGTH_SHORT).show();
                }

            }

        });

 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
     HttpClient httpclient = new DefaultHttpClient();
     HttpPost httppost = new HttpPost("http://221.120.216.52/a/map.php");
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
     HttpResponse response = httpclient.execute(httppost);
     HttpEntity entity = response.getEntity();
     is = entity.getContent();
     }catch(Exception e){
         Log.e("log_tag", "Error in http connection"+e.toString());
    }
//convert response to string
try{
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
       sb = new StringBuilder();
       sb.append(reader.readLine() + "\n");

       String line="0";
       while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
        }catch(Exception e){
              Log.e("log_tag", "Error converting result "+e.toString());
        }
//paring data
double LAT;
double LANG;
String INFO;
try{
      jArray = new JSONArray(result);
      JSONObject json_data=null;
      for(int i=0;i<jArray.length();i++){
             json_data = jArray.getJSONObject(i);
             LAT=json_data.getDouble("lat");
             LANG=json_data.getDouble("lang");
             INFO=json_data.getString("info");


//Overlay code
List<Overlay> mapOverlays = mapView.getOverlays();
                Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
        CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this);

        //GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
        GeoPoint point = new GeoPoint((int)(LAT * 1e6),(int)(LANG * 1e6));
        OverlayItem overlayitem = new OverlayItem(point, "Shakarganj", INFO);
        itemizedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedOverlay);

        MapController mapController = mapView.getController();

        mapController.animateTo(point);
        mapController.setZoom(6);

//Overlay code

         }
      }
      catch(JSONException e1){
          Toast.makeText(getBaseContext(), "No Vehicles Found" ,Toast.LENGTH_LONG).show();
      } catch (ParseException e1) {
            e1.printStackTrace();
    }
}
@Override
protected boolean isRouteDisplayed() {
    return false;
}
}

布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.google.android.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="0nSh8sjwyA39DgDkjMLZJYR101K2WKvcldQX1wA"
        android:clickable="true"
        android:enabled="true"
        android:visibility="invisible" />


    <EditText
        android:id="@+id/pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:width="150dp"
        android:gravity="center_horizontal"
        />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/pass"
        android:layout_marginLeft="44dp"
        android:text="@string/log" />

</RelativeLayout>
4

3 回答 3

1

请仔细阅读2、3遍,This Guideline尤其是try-catch部分。它会让你的生活更简单,但更好。

您的问题是您在非常一般的 try-catch 块中捕获了一些错误,之后您没有正确检查响应变量。

当您尝试解析响应(json解析)时,您必须检查响应是否有效;它可能只是空的。您应该在解析部分之前检查来自服务器的请求响应代码等。

于 2013-06-24T17:22:53.497 回答
0

似乎你的问题,根据这个:

06-24 21:41:04.175: E/AndroidRuntime(371): 在 map2.pkg.pkg.GMapsActivity.onCreate(GMapsActivity.java:112)

在这些线上(或周围)

jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){

我个人的赌注是引用jArray.length()是使用空对象jArray

不过,您应该注意其他答案的建议:我的只会让您当前的错误消失,而不是下面的问题。

于 2013-06-24T17:39:52.667 回答
0

几件事

  • 结果变量中的 json 响应为空或不完整。尝试设置断点以检查结果。

  • 在您的布局中,您也可以摆脱重复的命名空间声明 <com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"

  • 改成

    sb = new StringBuilder(); 字符串行=空;

因为while循环就足够了。

代替

sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
于 2013-06-24T17:46:30.190 回答