2014-12-05 79 views
1

當我嘗試調用活動時,我的應用程序崩潰。 IT也是使用數據庫,但問題可能不在那裏。它使用setText方法顯示錯誤。有人可以提出一些解決方案在此先感謝活動不啓動,空指針異常

這裏是我的代碼

public class EventsShower extends BaseActivity{ 

    private DBHelper dbHelper; 
    private SQLiteDatabase db; 
    private Cursor cursor; 
    private int[] colors = new int[2]; 
    private String eventS, dateS, timeS, done; 

// @Override 
// protected void onDestroy() { 
//  db.close(); 
//  dbHelper.close(); 
//  cursor.close(); 
//  super.onDestroy(); 
// } 

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

     colors[0] = Color.parseColor("#DF5D5D"); 
     colors[1] = Color.parseColor("#5077C9"); 


     LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linLayout); 
     LayoutInflater inflater = getLayoutInflater(); 

//  TextView tvEvent = (TextView)findViewById(R.id.tvEvent); 
//  TextView tvDate = (TextView)findViewById(R.id.tvDate); 
//  TextView tvTime = (TextView)findViewById(R.id.tvTime); 


     dbHelper = new DBHelper(this); 
     db = dbHelper.getWritableDatabase(); 

     String[] columns = new String[]{DBHelper.DATE, DBHelper.TIME, DBHelper.EVENT, DBHelper.DONE}; 
     cursor = db.query(DBHelper.TABLE_NAME, columns, null, null, null, null, null); 

     cursor.moveToFirst(); 
     do{ 
      View item = inflater.inflate(R.layout.item, linearLayout, false); 
//   eventS = cursor.getString(cursor.getColumnIndex(DBHelper.EVENT)); 
//   dateS = cursor.getString(cursor.getColumnIndex(DBHelper.DATE)); 
//   timeS = cursor.getString(cursor.getColumnIndex(DBHelper.TIME)); 
      Log.i("myLogs", eventS + "--" + dateS + "--" + timeS); 
      TextView tvEvent = (TextView)findViewById(R.id.tvEvent); 
      tvEvent.setText(cursor.getString(cursor.getColumnIndex(DBHelper.EVENT))); 
      TextView tvDate = (TextView)findViewById(R.id.tvDate); 
      tvDate.setText(cursor.getString(cursor.getColumnIndex(DBHelper.DATE))); 
      TextView tvTime = (TextView)findViewById(R.id.tvTime); 
      tvTime.setText(cursor.getString(cursor.getColumnIndex(DBHelper.TIME))); 

      if(cursor.getString(cursor.getColumnIndex(DBHelper.DONE)).equals("1")) { 
       done = "Done"; 
      }else { 
       done = "Pending"; 
      } 

      if(cursor.getString(cursor.getColumnIndex(DBHelper.DONE)).equals("1")) { 
       item.setBackgroundColor(colors[0]); 
      } else { 
       item.setBackgroundColor(colors[1]); 
      } 
      linearLayout.addView(item); 
      if(cursor.isLast()) { 
//    eventS = cursor.getString(cursor.getColumnIndex(DBHelper.EVENT)); 
//    dateS = cursor.getString(cursor.getColumnIndex(DBHelper.DATE)); 
//    timeS = cursor.getString(cursor.getColumnIndex(DBHelper.TIME)); 

       tvEvent.setText(cursor.getString(cursor.getColumnIndex(DBHelper.EVENT))); 

       tvDate.setText(cursor.getString(cursor.getColumnIndex(DBHelper.DATE))); 

       tvTime.setText(cursor.getString(cursor.getColumnIndex(DBHelper.TIME))); 
       if(cursor.getString(cursor.getColumnIndex(DBHelper.DONE)).equals("1")) { 
        done = "Done"; 
       }else { 
        done = "Pending"; 
       } 


       if(cursor.getString(cursor.getColumnIndex(DBHelper.DONE)).equals("1")) { 
        item.setBackgroundColor(colors[0]); 
       } else { 
        item.setBackgroundColor(colors[1]); 
       } 
       linearLayout.addView(item); 
       cursor.close(); 
      } 
      cursor.moveToNext(); 
     }while(cursor.moveToNext()); 


    } 

} 

而且我的日誌

12-05 09:46:08.386 2449-2449/com.example.kg2152.example E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kg2152.example/com.example.kg2152.example.EventsShower}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
      at android.app.ActivityThread.access$600(ActivityThread.java:141) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:5103) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:525) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at com.example.kg2152.example.EventsShower.onCreate(EventsShower.java:60) 
      at android.app.Activity.performCreate(Activity.java:5133) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
            at android.app.ActivityThread.access$600(ActivityThread.java:141) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:5103) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:525) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
            at dalvik.system.NativeStart.main(Native Method) 
+0

什麼是60系列的EventsShower? – Suvitruf 2014-12-05 10:17:21

+0

@Suvitruf tvEvent.setText(cursor.getString(cursor.getColumnIndex(DBHelper.EVENT))); – kg2152 2014-12-05 10:18:20

回答

5

大概R.id.tvEvent和其他視圖屬於R.layout.item,因此,你應該使用item來獲取裏面的對象。例如。

View item = inflater.inflate(R.layout.item, linearLayout, false); 
    TextView tvEvent = (TextView)item.findViewById(R.id.tvEvent); 
+0

非常感謝你的伴侶,它現在的作品:) – kg2152 2014-12-05 10:22:16

+0

不客氣 – Blackbelt 2014-12-05 10:22:47