2013-03-30 20 views
0

我在數據輸出類型應用程序中構建一個簡單的數據,我得到一個致命的異常NullPointerException。我已經查看了源代碼,並沒有發現任何問題。有人可以解釋我可能忽略的內容嗎?我知道它與我的一個字符串/變量有關 - 但我似乎無法查明確切的問題。數據輸出Android應用程序 - 空指針異常 - 需要實現intent.getLongExtra()

的logcat:

03-29 20:02:37.047: D/OpenGLRenderer(13437): Enabling debug mode 0 
03-29 20:02:38.957: D/AndroidRuntime(13437): Shutting down VM 
03-29 20:02:38.957: W/dalvikvm(13437): threadid=1: thread exiting with uncaught exception (group=0x41f7b930) 
03-29 20:02:38.967: E/AndroidRuntime(13437): FATAL EXCEPTION: main 
03-29 20:02:38.967: E/AndroidRuntime(13437): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nfc.linkingmanager/com.nfc.linkingmanager.ViewCountry}: java.lang.NullPointerException 
03-29 20:02:38.967: E/AndroidRuntime(13437): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at android.os.Looper.loop(Looper.java:137) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at android.app.ActivityThread.main(ActivityThread.java:5041) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at java.lang.reflect.Method.invokeNative(Native Method) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at java.lang.reflect.Method.invoke(Method.java:511) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at dalvik.system.NativeStart.main(Native Method) 
03-29 20:02:38.967: E/AndroidRuntime(13437): Caused by: java.lang.NullPointerException 
03-29 20:02:38.967: E/AndroidRuntime(13437): at com.nfc.linkingmanager.ViewCountry.onCreate(ViewCountry.java:32) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at android.app.Activity.performCreate(Activity.java:5104) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
03-29 20:02:38.967: E/AndroidRuntime(13437): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
03-29 20:02:38.967: E/AndroidRuntime(13437): ... 11 more 

JAVA:

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.TextView; 
import android.widget.TimePicker; 

public class ViewCountry extends Activity { 

     private long rowID; 
     private TextView nameTv; 
     private TextView capTv; 
     private TextView codeTv; 
     private TextView timeTv; 

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

      setUpViews(); 
      Bundle extras = getIntent().getExtras(); 
      rowID = extras.getLong(CountryList.ROW_ID); 
     } 

     private void setUpViews() { 
      nameTv = (TextView) findViewById(R.id.nameText); 
      capTv = (TextView) findViewById(R.id.capText); 
      timeTv = (TextView) findViewById(R.id.timeEdit); 
      codeTv = (TextView) findViewById(R.id.codeText); 
     } 

     @Override 
     protected void onResume() 
     { 
      super.onResume(); 
      new LoadContacts().execute(rowID); 
     } 

     private class LoadContacts extends AsyncTask<Long, Object, Cursor> 
     { 
      DatabaseConnector dbConnector = new DatabaseConnector(ViewCountry.this); 

      @Override 
      protected Cursor doInBackground(Long... params) 
      { 
      dbConnector.open(); 
      return dbConnector.getOneContact(params[0]); 
      } 

      @Override 
      protected void onPostExecute(Cursor result) 
      { 
      super.onPostExecute(result); 

      result.moveToFirst(); 
      // get the column index for each data item 
      int nameIndex = result.getColumnIndex("name"); 
      int capIndex = result.getColumnIndex("cap"); 
      int codeIndex = result.getColumnIndex("code"); 
      int timeIndex = result.getColumnIndex("time"); 

      nameTv.setText(result.getString(nameIndex)); 
      capTv.setText(result.getString(capIndex)); 
//   timeTv.setText(result.getInt(timeIndex)); // <--- HERE WAS AN ERROR 
      timeTv.setText(result.getString(timeIndex)); // time was stored as Sting all the time 
      codeTv.setText(result.getString(codeIndex)); 

      result.close(); 
      dbConnector.close(); 
      } 
     } 


     @Override 
     public boolean onCreateOptionsMenu(Menu menu) 
     { 
      super.onCreateOptionsMenu(menu); 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.view_country_menu, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) 
     { 
      switch (item.getItemId()) 
      { 
      case R.id.editItem: 
       Intent addEditContact = 
        new Intent(this, AddEditCountry.class); 

       addEditContact.putExtra(CountryList.ROW_ID, rowID); 
       addEditContact.putExtra("name", nameTv.getText()); 
       addEditContact.putExtra("cap", capTv.getText()); 
       addEditContact.putExtra("code", codeTv.getText()); 
       startActivity(addEditContact); 
       return true; 

      case R.id.deleteItem: 
       deleteContact(); 
       return true; 

      default: 
       return super.onOptionsItemSelected(item); 
      } 
     } 

     private void deleteContact() 
     { 

      AlertDialog.Builder alert = new AlertDialog.Builder(ViewCountry.this); 

      alert.setTitle(R.string.confirmTitle); 
      alert.setMessage(R.string.confirmMessage); 

      alert.setPositiveButton(R.string.delete_btn, 
      new DialogInterface.OnClickListener() 
      { 
       public void onClick(DialogInterface dialog, int button) 
       { 
        final DatabaseConnector dbConnector = 
         new DatabaseConnector(ViewCountry.this); 

        AsyncTask<Long, Object, Object> deleteTask = 
         new AsyncTask<Long, Object, Object>() 
         { 
         @Override 
         protected Object doInBackground(Long... params) 
         { 
          dbConnector.deleteContact(params[0]); 
          return null; 
         } 

         @Override 
         protected void onPostExecute(Object result) 
         { 
          finish(); 
         } 
         }; 

        deleteTask.execute(new Long[] { rowID });    
       } 
      } 
     ); 

      alert.setNegativeButton(R.string.cancel_btn, null).show(); 
     } 
    } 
+0

你應該學會如何找出錯誤的位置。在logcat窗口雙擊com.nfc.linkingmanager.ViewCountry.onCreate(ViewCountry.java:32) –

回答

2

一個NullPointerException異常被拋出在onCreate()方法。我懷疑getIntent().getExtras()返回null,所以您致電extras.getLong()是拋出異常。

+0

這一行就知道了!我該如何解決這個問題? (我是新來的Android開發) –

+0

我有一種感覺,我應該啓用一個斷點(從我讀過的判斷 - 但我不知道我在找什麼) –

+1

做額外的空檢查:'if( extras!= null){...}'。或者,使用'intent.getLongExtra()',如果意圖附加項爲空,它將自動返回您提供的默認值。 NullPointerException真的是一件Java事情。這意味着你正試圖調用一個空對象的東西。 – Karakuri