java.lang.RuntimeException:无法启动活动ComponentInfo {...}:java.lang.NullPointerException

时间:2014-03-06 12:31:05

标签: android nullpointerexception

当您点击从一个活动转到另一个活动时,我有一个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo {...}: java.lang.NullPointerException

我错过了一些方法?请帮助我理解我做错了什么,这就是为什么你看到这个错误?

我的活动:Zaselenie.java

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class Zaselenie extends Activity {

    ContactDBmoy cqh;
    KvartDB kdb;
    SQLiteDatabase sdb;

    private Long rowId;

    Spinner spZaselenie;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zaselenie);

    spZaselenie  = (Spinner)findViewById(R.id.spZaselenie);

    // Инициализируем наш класс-обёртку
            cqh = new ContactDBmoy(Zaselenie.this);
            kdb = new KvartDB(Zaselenie.this);

            // База нам нужна для записи и чтения
            sdb = cqh.getWritableDatabase();
            sdb = kdb.getWritableDatabase();

            Bundle extras = getIntent().getExtras();
            rowId = extras.getLong(ContactMain.rowId);



            Cursor cursorKvart = sdb.query(KvartDB.TABLE_NAME, new String[] {
                    KvartDB._ID, KvartDB.NAME_COLUMN }, null, // The
                                                                    // columns
                                                                    // for the
                                                                    // WHERE
                                                                    // clause
                    null, // The values for the WHERE clause
                    null, // don't group the rows
                    null, // don't filter by row groups
                    null // The sort order
                    );


            ArrayList<String> labels = new ArrayList<String>();
            while (cursorKvart.moveToNext()) {
                // GET COLUMN INDICES + VALUES OF THOSE COLUMNS
                int idKvart = cursorKvart.getInt(cursorKvart.getColumnIndex(KvartDB._ID));
                String nameKvart = cursorKvart.getString(cursorKvart.getColumnIndex(KvartDB.NAME_COLUMN));
                Log.i("LOG_TAG", "ROW " + idKvart + " HAS NAME " + nameKvart);



                    labels.add(nameKvart);
                    // Creating adapter for spinner
                    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                            android.R.layout.simple_spinner_item, labels);

                    // Drop down layout style - list view with radio button
                    dataAdapter
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    dataAdapter.notifyDataSetChanged();

                    // attaching data adapter to spinner
                    spZaselenie.setAdapter(dataAdapter);
            }
            cursorKvart.close();

    }

    public Zaselenie() {
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onStop() {
        super.onStop();
        // закрываем соединения с базой данных
        sdb.close();
        kdb.close();
    }
}

Logcat错误:

03-06 12:04:43.802: E/AndroidRuntime(20861): FATAL EXCEPTION: main
03-06 12:04:43.802: E/AndroidRuntime(20861): java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.zloyel.manager_sutochnoy_arendy_1/ru.zloyel.manager_sutochnoy_arendy_1.Zaselenie}: java.lang.NullPointerException
03-06 12:04:43.802: E/AndroidRuntime(20861):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at android.os.Looper.loop(Looper.java:137)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at android.app.ActivityThread.main(ActivityThread.java:4745)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at java.lang.reflect.Method.invokeNative(Native Method)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at java.lang.reflect.Method.invoke(Method.java:511)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at dalvik.system.NativeStart.main(Native Method)
03-06 12:04:43.802: E/AndroidRuntime(20861): Caused by: java.lang.NullPointerException
03-06 12:04:43.802: E/AndroidRuntime(20861):    at ru.zloyel.manager_sutochnoy_arendy_1.Zaselenie.onCreate(Zaselenie.java:39)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at android.app.Activity.performCreate(Activity.java:5008)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-06 12:04:43.802: E/AndroidRuntime(20861):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-06 12:04:43.802: E/AndroidRuntime(20861):    ... 11 more

3 个答案:

答案 0 :(得分:2)

检查extras不是null,如下所示......

if(extras != null) {
    rowId = extras.getLong(ContactMain.rowId);
}

同时检查您的Cursor对象是否为空,如下所示......

if(cursorKvart != null && cursorKvart.getCount() > 0) {

    while (cursorKvart.moveToNext()) {

        ......

    }

}

答案 1 :(得分:2)

首先检查rowId非空

并设置ArrayAdapterWhile()循环

 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                        android.R.layout.simple_spinner_item, labels);

 dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

 spZaselenie.setAdapter(dataAdapter);

答案 2 :(得分:1)

您应该在调试模式下运行代码,然后您可以轻松识别您获取空指针的行。我认为找出异常和错误是最好的方法。