2013-02-20 49 views
0

我有commonsware样品NullPointerException异常onLocationChanged GPS

试试这里是

public class ConstantsBrowser extends ListActivity { 
    private LocationManager lm; 
    private LocationListener locListener; 
    private TextView latTxt, lonTxt; 
    private double dLat=0; 
    private double dLon=0; 
    Intent intent = null; 
    private static final int ADD_ID = Menu.FIRST+1; 
    private static final int DELETE_ID = Menu.FIRST+3; 
    private static final int UPDATE_ID = Menu.FIRST+4; 
    public static final int SHOW_SUB_ACTIVITY_VIEW=3; 
    private DatabaseHelper db=null; 
    private Cursor constantsCursor=null; 

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

    latTxt = (TextView) findViewById(R.id.latitudeTxt); 
    lonTxt = (TextView) findViewById(R.id.longitudeTxt); 

    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locListener = new MyLocationListener(); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 10,locListener); 

    db=new DatabaseHelper(this); 
    constantsCursor=db 
         .getReadableDatabase() 
         .rawQuery("SELECT _ID, title, value "+ 
           "FROM constants ORDER BY _ID", 
           null); 

    ListAdapter adapter=new SimpleCursorAdapter(this, 
          R.layout.row, constantsCursor, 
          new String[] { 
             DatabaseHelper.ID, 
             DatabaseHelper.TITLE, 
             DatabaseHelper.VALUE}, 
          new int[] {R.id.id, R.id.alamat, R.id.value}); 

    setListAdapter(adapter); 
    registerForContextMenu(getListView()); 
    } 

    @Override 
    public void onDestroy() { 
    super.onDestroy(); 

    constantsCursor.close(); 
    db.close(); 
    } 

    private class MyLocationListener implements LocationListener { 
     @Override 
     public void onLocationChanged(Location loc) { 
      // TODO Auto-generated method stub 
      if (loc != null) { 
       latTxt.setText(String.valueOf(loc.getLatitude())); 
       lonTxt.setText(String.valueOf(loc.getLongitude())); 
      } 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 

     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    menu.add(Menu.NONE, ADD_ID, Menu.NONE, "Data Baru") 
     .setIcon(R.drawable.add) 
     .setAlphabeticShortcut('a'); 

    return(super.onCreateOptionsMenu(menu)); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case ADD_ID: 
     add(); 
     return(true); 
    } 

    return(super.onOptionsItemSelected(item)); 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
            ContextMenu.ContextMenuInfo menuInfo) { 
    menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete"); 
    menu.add(Menu.NONE, UPDATE_ID, Menu.NONE, "Update") 
     .setAlphabeticShortcut('d'); 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case DELETE_ID: 
     AdapterView.AdapterContextMenuInfo info= 
      (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 

     delete(info.id); 
     return(true); 

     case UPDATE_ID: 
      intent = new Intent(ConstantsBrowser.this, MainActivity.class); 
      startActivityForResult(intent, SHOW_SUB_ACTIVITY_VIEW); 
      return(true); 
    } 

    return(super.onOptionsItemSelected(item)); 
    } 

    private void add() { 
    LayoutInflater inflater=LayoutInflater.from(this); 
    View addView=inflater.inflate(R.layout.add_edit, null); 
    final DialogWrapper wrapper=new DialogWrapper(addView); 

    new AlertDialog.Builder(this) 
     .setTitle(R.string.add_title) 
     .setView(addView) 
     .setPositiveButton(R.string.ok, 
          new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, 
           int whichButton) { 
      processAdd(wrapper); 
     } 
     }) 
     .setNegativeButton(R.string.cancel, 
          new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, 
           int whichButton) { 
      // ignore, just dismiss 
     } 
     }) 
     .show(); 
    } 

    private void delete(final long rowId) { 
    if (rowId>0) { 
     new AlertDialog.Builder(this) 
     .setTitle(R.string.delete_title) 
     .setPositiveButton(R.string.ok, 
          new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, 
           int whichButton) { 
      processDelete(rowId); 
      } 
     }) 
     .setNegativeButton(R.string.cancel, 
          new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, 
           int whichButton) { 
      // ignore, just dismiss 
      } 
     }) 
     .show(); 
    } 
    } 

    private void processAdd(DialogWrapper wrapper) { 
    ContentValues values=new ContentValues(2); 

    values.put(DatabaseHelper.TITLE, wrapper.getTitle()); 
    values.put(DatabaseHelper.VALUE, wrapper.getValue()); 

    db.getWritableDatabase().insert("constants", DatabaseHelper.TITLE, values); 
    constantsCursor.requery(); 
    } 

    private void processDelete(long rowId) { 
    String[] args={String.valueOf(rowId)}; 

    db.getWritableDatabase().delete("constants", "_ID=?", args); 
    constantsCursor.requery(); 
    } 

    class DialogWrapper { 
    EditText titleField=null; 
    EditText valueField=null; 
    View base=null; 

    DialogWrapper(View base) { 
     this.base=base; 
     valueField=(EditText)base.findViewById(R.id.value); 
    } 

    String getTitle() { 
     return(getTitleField().getText().toString()); 
    } 

    String getValue() { 
     return(getValueField().getText().toString()); 
    } 

    private EditText getTitleField() { 
     if (titleField==null) { 
     titleField=(EditText)base.findViewById(R.id.title); 
     } 

     return(titleField); 
    } 

    private EditText getValueField() { 
     if (valueField==null) { 
     valueField=(EditText)base.findViewById(R.id.value); 
     } 

     return(valueField); 
    } 
    } 
} 

我添加GPS在本次活动,但是当我尝试应用程序是强制关闭

这里是清单中的活动

我加个权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.INTERNET" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > 
    </uses-permission> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-permission android:name="android.permission.CAMERA" > 

和 这里是logcat的空指针异常

02-20 12:07:32.203: ERROR/AndroidRuntime(8904): FATAL EXCEPTION: main 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904): java.lang.NullPointerException 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904):  at com.commonsware.android.constants.ConstantsBrowser$MyLocationListener.onLocationChanged(ConstantsBrowser.java:98) 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904):  at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227) 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904):  at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160) 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904):  at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176) 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904):  at android.os.Looper.loop(Looper.java:130) 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904):  at java.lang.reflect.Method.invoke(Method.java:507) 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-20 12:07:32.203: ERROR/AndroidRuntime(8904):  at dalvik.system.NativeStart.main(Native Method) 
02-20 12:07:32.226: WARN/ActivityManager(1308): Force finishing activity com.commonsware.android.constants/.ConstantsBrowser 

空 是 线latTxt.setText(String.valueOf(loc.getLatitude()));

如何解决呢?

+0

发布您的布局文件。 – 2013-02-20 05:16:05

+1

请确保你使用正确的布局的活动,其中你已经添加TextViews与'latitudeTxt'和'longitudeTxt'编号 – 2013-02-20 05:17:36

+0

你检查了禄的值是否为空或不? – Triode 2013-02-20 05:17:59

回答

1

latTxt为空。确保您的布局XML文件中有<TextView>,编号为@+id/latitudeTxt

+0

是的,我忘了把latitudeTxt放在我的main.xml中。但我把我的dialog.xml中的latitudeTxt。因为我尝试在同一活动的对话框中检测到gps。 – 2013-02-20 06:28:24