设置CursorAdapter时,Android应用程序强制关闭

时间:2014-01-15 14:51:27

标签: android nullpointerexception simplecursoradapter android-cursoradapter

我正在关注Udemy.com上的here

视频教程

在搞乱SQL之前,一切都进展顺利。现在,我不是一个完整的SQL新手,我实际上是从命令行使用SQL来学习它的大学课程,但我也不是专业人士。应用程序在编译时不会出错,但会立即强制关闭,我的logcat将我指向lvCountries.setAdapter(适配器)。我感兴趣的部分是“尝试在空对象引用上调用虚方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'”。那么我的适配器是null对象,还是lvCountries?我的最后一个领先是用db.query()实例化国家。 Android Studio向我发出警告,说它可能会产生NullPointerExecption。提前感谢大家。

01-15 08:39:59.560    9056-9056/com.example.sql E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.sql, PID: 9056
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sql/com.example.sql.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at com.example.sql.MainActivity.onCreate(MainActivity.java:44)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

MainActivity.java

package com.example.sql;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends ActionBarActivity {

    private CountryDB countryDatabase = null;
    private Cursor countries = null;
    private ListView lvCountries = null;

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

        //Create the database helper
        SQLiteDatabase.CursorFactory factory = null;
        countryDatabase = new CountryDB(this, "countryDB", factory);

        String[] columnNames = {"Name", "Pop", "Area", BaseColumns._ID};
        lvCountries = (ListView) findViewById(R.id.lvCountries);

        int[] targetLayoutIDs = {R.id.textName, R.id.textPop, R.id.textArea};

        SQLiteDatabase db = countryDatabase.getReadableDatabase();
        countries = db.query("countries", columnNames, null, null, null, null, null);
        CursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.country_item,
                countries, columnNames, targetLayoutIDs, 0);
        lvCountries.setAdapter(adapter);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    public void onPause() {
        super.onPause();
        countryDatabase.close();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

country_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout 
        android:id="@+id/subItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/popLabel" />

        <TextView
            android:id="@+id/textPop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView 
            android:layout_width= "5dp" 
            android:layout_height="wrap_content" 
            android:text="@string/space" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/areaLabel" />

        <TextView
            android:id="@+id/textArea"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView 
            android:layout_width= "5dp" 
            android:layout_height="wrap_content" 
            android:text="@string/space" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/areaDescriptor" />

    </LinearLayout>

</LinearLayout>

如果您感兴趣,以下是Android Studio格式的其余代码。 http://www.mediafire.com/download/zu0x71ysl67b779/SQLLiteAndListViews.tar.gz

2 个答案:

答案 0 :(得分:3)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

显然,lvCountriesnull,这就是此行崩溃的原因:

lvCountries.setAdapter(adapter);

查看您的代码,似乎您没有检索到正确的视图:

lvCountries = (ListView) findViewById(R.id.lvCountries);

确保ListView中有R.layout.activity_main

<ListView
    android:id="@+id/lvCountries"
    ... other parameters here
/>

答案 1 :(得分:2)

看起来像这一行:

lvCountries = (ListView) findViewById(R.id.lvCountries);

返回null。您可以通过在上述行之后放置此行来检查是否是这种情况:

Log.d(getPackageName(), lvCountries != null ? "lvCountries is not null!" : "lvCountries is null!");

然后检查LogCat以查看它是否为空。