活动无法启动,Null Pointer Exception

时间:2014-12-05 10:16:06

标签: android

当我尝试调用某个活动时,我的应用程序崩溃了。 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)

1 个答案:

答案 0 :(得分:5)

可能R.id.tvEvent和其他视图属于R.layout.item,因此,您应该使用item来检索其中的对象。 E.g。

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