执行代码时出现以下错误

时间:2014-05-17 06:05:46

标签: android

  

致命的例外:主要   java.lang.RuntimeException:无法启动活动ComponentInfo {com.bugs3.munishgupta.cabup / com.bugs3.munishgupta.cabup.WannaHier}:java.lang.NullPointerException     在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)     在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)     在android.app.ActivityThread.access $ 600(ActivityThread.java:123)     在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)     在android.os.Handler.dispatchMessage(Handler.java:99)     在android.os.Looper.loop(Looper.java:137)     在android.app.ActivityThread.main(ActivityThread.java:4424)     at java.lang.reflect.Method.invokeNative(Native Method)     在java.lang.reflect.Method.invoke(Method.java:511)     在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:825)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)     at dalvik.system.NativeStart.main(Native Method)   引起:java.lang.NullPointerException     在com.bugs3.munishgupta.cabup.WannaHier.onCreate(WannaHier.java:44)     在android.app.Activity.performCreate(Activity.java:4465)     在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)     在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)     ......还有11个

  • Wanna Hier.java

            package com.bugs3.munishgupta.cabup;
        import java.util.concurrent.ExecutionException;
        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;
        import android.app.Activity;
        import android.content.Intent;
        import android.location.Location;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.Menu;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.Toast;
        import com.google.android.gms.maps.GoogleMap;
        import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
        import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
        import com.google.android.gms.maps.MapFragment;
        import com.google.android.gms.maps.model.LatLng;
        import com.google.android.gms.maps.model.Marker;
        import com.google.android.gms.maps.model.MarkerOptions;
    
        public class WannaHier extends Activity {
            GoogleMap map;
            Intent info;
            String mob,lat,lon,title=null;
            Bundle b;
            Button btVCab;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.wanna_hier);
                info = getIntent();
                b  = info.getExtras();
                mob = b.getString("mob");
                btVCab=(Button) findViewById(R.id.btVCab);
                map=((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
                map.setMyLocationEnabled(true);
                lat=Double.toString(map.getMyLocation().getLatitude());
                lon=Double.toString(map.getMyLocation().getLongitude());
                map.setOnMarkerClickListener(new OnMarkerClickListener() {
    
                    @Override
                    public boolean onMarkerClick(Marker marker) {
                        // TODO Auto-generated method stub
                    try
                    {
                        title=marker.getTitle().toString();
                        request(title,lat,lon,mob);
                    }
                    catch(Exception e)
                    {
                        Toast.makeText(getApplicationContext(), "Please turn on your location", Toast.LENGTH_LONG).show();
                    }
                        return false;
                    }
                });
                map.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
    
                    @Override
                    public void onMyLocationChange(Location location) {
                        // TODO Auto-generated method stub
                        JSONGetRequest jRequest = JSONGetRequest
                                .getSingleInstance(WannaHier.this);
                        try {
    
                            String url = (Tags.url + "op=update&query=update requestpick set carNo='" +title+
                                  "',latitude='" + lat + "', longitude='" + lon +"'where cust_mob='"+ mob +"'").replaceAll(
                                            " ", "%20");
    
                            JSONObject json = jRequest.execute(url).get();
    
                            if (json.getInt("success")==1) {
                                Log.d("Requested Successfully!", json.toString());
                            } else {
                                Log.d("Request failed!", json.getString("message"));
                            }
    
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });
                btVCab.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        JSONGetRequest jRequest = JSONGetRequest
                                .getSingleInstance(WannaHier.this);
                        try {
    
                            String url = (Tags.url + "op=select&query=select * from driverloc")
                                    .replaceAll(" ", "%20");
    
                            JSONObject json = jRequest.execute(url).get();
    
                            if (json.getInt("success")==1) {
                                Log.d("Requested Successfully!", json.toString());
                                JSONArray js=json.getJSONArray("entities");
                                try{
                                    for(int co=js.length(), i=0; i<co;i++){
                                        json=js.getJSONObject(i);
                                        title=json.getString("carNo");
                                        lat=json.getString("latitude");
                                        lon=json.getString("longitude");
                                        map.addMarker(new MarkerOptions()
                                        .position(new LatLng(Double.parseDouble(lat),Double.parseDouble(lon)))
                                        .title(title));
                                    }
                                }
                                catch(JSONException e) {
                                    e.printStackTrace();
                                }           
                            } else {
                                Log.d("Request failed!", json.getString("message"));
                            }
    
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
    
                    }
                });
            }
            @Override
            protected void onStart() {
                // TODO Auto-generated method stub
                super.onStart();
                Toast.makeText(getApplicationContext(), "Please Turn on your location and click on view cabs", Toast.LENGTH_LONG).show();
            }
    
            public void request(String title, String lat, String lon, String mob )
            {
                JSONGetRequest jRequest = JSONGetRequest
                        .getSingleInstance(WannaHier.this);
                try {
    
                    String url = (Tags.url + "op=insert&query=insert into requestpick values ('"
                            +title+ "','" + lat + "','" + lon +"','"+ mob +"')").replaceAll(
                                    " ", "%20");
    
                    JSONObject json = jRequest.execute(url).get();
    
                    if (json.getInt("success")==1) {
                        Log.d("Requested Successfully!", json.toString());
                    } else {
                        Log.d("Request failed!", json.getString("message"));
                    }
    
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            @Override
            protected void onPause() {
                // TODO Auto-generated method stub
                super.onPause();
                JSONGetRequest jRequest = JSONGetRequest
                        .getSingleInstance(WannaHier.this);
                try {
    
                    String url = (Tags.url + "op=delete&query=delete from requestpick where cust_mob='"
                    + mob +"'").replaceAll(" ", "%20");
    
                    JSONObject json = jRequest.execute(url).get();
    
                    if (json.getInt("success")==1) {
                        Log.d("Requested Successfully!", json.toString());
                    } else {
                        Log.d("Request failed!", json.getString("message"));
                    }
    
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                finish();
            }
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.wanna_hier, menu);
                return true;
            }
    
        }
    
  • Profile.java

    package com.bugs3.munishgupta.cabup;
    import com.bugs3.munishgupta.cabup.R;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class Profile extends Activity {
    
    Button obhier, obshare, obsettings;
    String mob,carno,table;
    Intent info;
    Bundle b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);
        obhier=(Button) findViewById(R.id.btnHier);
        obshare=(Button) findViewById(R.id.btnShare);
        obsettings=(Button) findViewById(R.id.btnSetting);
        info = getIntent();
        b  = info.getExtras();
        table=b.getString("table");
    
        if((carno=b.getString("carno"))!=null )
        {
            Toast.makeText(getApplicationContext(), carno, Toast.LENGTH_SHORT).show();
            obhier.setVisibility(View.INVISIBLE);       
        }
        else
        {
            obshare.setVisibility(View.INVISIBLE);
        }
    
        obshare.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent wshare=new Intent(Profile.this,WannaShare.class);
                    mob = b.getString("mob");
                    wshare.putExtra("mob", mob);
                    wshare.putExtra("carno", carno);
                    wshare.putExtra("table", table);
                    startActivity(wshare);
            }
        });
        obhier.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent whier=new Intent(Profile.this,WannaHier.class);
                    mob = b.getString("mob");
                    whier.putExtra("mob", mob);
                    whier.putExtra("table", table);
                    startActivity(whier);
            }
        });
        obsettings.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent isetting = new Intent(Profile.this,ChangeSetting.class);
                if(b.getString("mob")!=null)
                {
                    mob = b.get("mob").toString();
                    isetting.putExtra("mob", mob);
                    isetting.putExtra("table", table);
                }
                startActivity(isetting);
            }      
        });       
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.profile, menu);
        return true;
    }
    }
    

1 个答案:

答案 0 :(得分:0)

答案是WannaHier: line 44 我怀疑它是

lat=Double.toString(map.getMyLocation().getLatitude());

如果没有可用的位置,getMyLocation()可以返回null,因此您必须检查此内容。

例如:

Location location = map.getMyLocation();
if(location != null){
    lat=Double.toString(location.getLatitude());
    lon=Double.toString(location.getLongitude()); 
}