Android的空指针异常 [英] Android Null pointer exception
问题描述
即时得到一个NullPointerException异常当我尝试加载我的意图来处理我的数据的列表视图从我的数据库中的类。我是一个新手,总当它涉及到列表视图,所以希望有人能告诉我在哪里伊夫出了错!
我在想,空指针是由于这一事实,在第一行posistion光标是在没有数据被人发现,但我的认为的看logcat的,它的使用问题我的XML布局项?
我的继承人类为ListView:
包com.example.sqliteexample;进口android.app.Activity;
进口android.app.ListActivity;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.support.v4.widget.SimpleCursorAdapter;
进口android.widget.ListView;公共类SQLView延伸活动{保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); HotOrNot H =新HotOrNot(本,NULL,NULL); 的setContentView(R.layout.layout);
ListView控件的listContent =(ListView控件)findViewById(R.id.contentList); HotOrNot内容=新HotOrNot(本,NULL,NULL); 光标光标= Content.getData(); startManagingCursor(光标); @燮pressWarnings(静态访问)
的String [] =由新的String [] {H.KEY_NAME,H.KEY_HOTNESS};
INT []为= INT新[] {R.id.txtName,R.id.txtAge}; @燮pressWarnings(德precation)
SimpleCursorAdapter的CursorAdapter =新SimpleCursorAdapter(这一点,R.layout.entries,光标,从,到); listContent.setAdapter(CursorAdapter的);
从我的数据库处理类的getData的方法:
公共光标的getData(){ 的String [] =列新的String [] {KEY_ROWID,KEY_NAME,KEY_HOTNESS};
光标C = ourDatabase.query(DATABASE_TABLE,列,NULL,NULL,NULL,NULL,NULL);
返回℃;
HotOrNot DB处理类:
包com.example.sqliteexample;进口android.app.ListActivity;
进口android.content.ContentValues;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.widget.ListView;公共类HotOrNot {公共静态最后弦乐KEY_ROWID =_id;
公共静态最后弦乐KEY_NAME =persons_name;
公共静态最后弦乐KEY_HOTNESS =persons_hotness;私人静态字符串DATABASE_NAME =HotOrNotdb;
私人静态字符串DATABASE_TABLE =peopleTable;
私有静态诠释DATABASE_VERSION = 30;私有静态DBHelper ourHelper;
私有静态语境ourContext;
私有静态SQLiteDatabase ourDatabase;ListView控件列表视图;
公共无效DB_NAME(字符串数据库名)
{
DATABASE_NAME =数据库名;}公共字符串returnDB_NAME()
{
返回DATABASE_NAME;
}
公共无效DB_tableNAME(字符串DBtName)
{
DATABASE_TABLE = DBtName;}公共字符串returnDB_tNAME()
{
返回DATABASE_TABLE;
}
公共无效DB_NAME(INT DBVersion)
{
DATABASE_VERSION = DBVersion;}公众诠释returnDB_VERSION()
{
返回DATABASE_VERSION;
}//类的构造函数上下文。当对象在主程序被构造
//这个类即上下文这个被送到这个构造函数来设置对象上下文。
在SQLiteDatabase和DBhelper设置//空值,因为其中没有从所谓的通
//对象。
公共HotOrNot(上下文C,SQLiteDatabase newSQL,DBHelper BD)
{
ourContext = C;
ourDatabase = newSQL;
ourHelper = BD;}私有静态类DBHelper扩展SQLiteOpenHelper
{
公共DBHelper(上下文的背景下){
超(背景下,KEY_NAME,空,DATABASE_VERSION); } @覆盖
公共无效的onCreate(SQLiteDatabase DB){
db.execSQL(CREATE TABLE+ DATABASE_TABLE +(+
KEY_ROWID +INTEGER PRIMARY KEY AUTOINCREMENT,+
KEY_NAME +TEXT NOT NULL,+
KEY_HOTNESS +TEXT NOT NULL); );
} @覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){ db.execSQL(DROP TABLE IF EXISTS+ DATABASE_TABLE);
的onCreate(DB);
}
} 公共HotOrNot的open()抛出的SQLException
{ ourHelper =新DBHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
返回此; } 公共无效的close()
{
ourHelper.close(); } 众长createEntry(字符串名称,字符串辣味){
ContentValues CV =新ContentValues();
cv.put(KEY_NAME,名);
cv.put(KEY_HOTNESS,辣味);
返回ourDatabase.insert(DATABASE_TABLE,空,CV); } 公共光标的getData(){ 的String [] =列新的String [] {KEY_ROWID,KEY_NAME,KEY_HOTNESS};
光标C = ourDatabase.query(DATABASE_TABLE,列,NULL,NULL,NULL,NULL,NULL);
//字符串结果=; // INT iRow = c.getColumnIndex(KEY_ROWID);
// INT INAME = c.getColumnIndex(KEY_NAME);
// INT ihotness = c.getColumnIndex(KEY_HOTNESS); //对于(c.moveToFirst();!c.isAfterLast(); c.moveToNext())
// {
//结果=结果+ c.getString(iRow)++ c.getString(INAME)++ c.getString(ihotness); //} //c.close(); 返回℃;
} 公共字符串的getName(长升){ 的String [] =列新的String [] {KEY_ROWID,KEY_NAME,KEY_HOTNESS};
光标C = ourDatabase.query(DATABASE_TABLE,列KEY_ROWID +=+ 1,NULL,NULL,NULL,NULL);
如果(C!= NULL)
{
//移到所选择的行
c.moveToFirst();
字符串名称= c.getString(1);
返回名称;
}
返回null; }
公共字符串getHotness(长升){ 的String [] =列新的String [] {KEY_ROWID,KEY_NAME,KEY_HOTNESS};
光标C = ourDatabase.query(DATABASE_TABLE,列KEY_ROWID +=+ 1,NULL,NULL,NULL,NULL);
如果(C!= NULL)
{
//移到所选择的行
c.moveToFirst();
串辣味= c.getString(2);
返回的辣味; }
返回null;
} 公共无效updateEntry(长的NewL,字符串nameEdit,字符串hotnessEdit){
ContentValues cvUpdate =新ContentValues(); cvUpdate.put(KEY_NAME,nameEdit);
cvUpdate.put(KEY_HOTNESS,hotnessEdit); ourDatabase.update(DATABASE_TABLE,cvUpdate,KEY_ROWID +=+的NewL,NULL);
} 公共无效delID(长升){
ourDatabase.delete(DATABASE_TABLE,KEY_ROWID +=+ 1,空); }
}
为ListView我的XML布局:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直><的TextView
机器人:ID =@ + ID / txtName的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=名称:/><的TextView
机器人:ID =@ + ID / txtAge
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=年龄:/>< / LinearLayout中>
ListView的XML布局:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>< ListView控件
机器人:ID =@ + ID / contentList
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
< /&的ListView GT;< / LinearLayout中>
logcat的:
01-11 00:23:26.655:D / AndroidRuntime(272):关闭VM
01-11 00:23:26.655:W / dalvikvm(272):主题ID = 1:螺纹未捕获的异常退出(组= 0x4001d800)
01-11 00:23:26.675:E / AndroidRuntime(272):致命异常:主要
01-11 00:23:26.675:E / AndroidRuntime(272):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.sqliteexample / com.example.sqliteexample.SQLView}:显示java.lang.NullPointerException
01-11 00:23:26.675:E / AndroidRuntime(272):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-11 00:23:26.675:E / AndroidRuntime(272):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-11 00:23:26.675:E / AndroidRuntime(272):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
01-11 00:23:26.675:E / AndroidRuntime(272):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
01-11 00:23:26.675:E / AndroidRuntime(272):在android.os.Handler.dispatchMessage(Handler.java:99)
01-11 00:23:26.675:E / AndroidRuntime(272):在android.os.Looper.loop(Looper.java:123)
01-11 00:23:26.675:E / AndroidRuntime(272):在android.app.ActivityThread.main(ActivityThread.java:4627)
01-11 00:23:26.675:E / AndroidRuntime(272):在java.lang.reflect.Method.invokeNative(本机方法)
01-11 00:23:26.675:E / AndroidRuntime(272):在java.lang.reflect.Method.invoke(Method.java:521)
01-11 00:23:26.675:E / AndroidRuntime(272):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
01-11 00:23:26.675:E / AndroidRuntime(272):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-11 00:23:26.675:E / AndroidRuntime(272):在dalvik.system.NativeStart.main(本机方法)
01-11 00:23:26.675:E / AndroidRuntime(272):致:显示java.lang.NullPointerException
01-11 00:23:26.675:E / AndroidRuntime(272):在com.example.sqliteexample.HotOrNot.getData(HotOrNot.java:143)
01-11 00:23:26.675:E / AndroidRuntime(272):在com.example.sqliteexample.SQLView.onCreate(SQLView.java:24)
01-11 00:23:26.675:E / AndroidRuntime(272):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-11 00:23:26.675:E / AndroidRuntime(272):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
您只需忘了打电话的open()
。
HotOrNot内容=新HotOrNot(本,NULL,NULL);
Content.open();
光标光标= Content.getData();
(但请阅读关于Java命名约定,该状态变量必须以小写字母开头。)
Im getting a 'NullPointerException' when I try and load my intent to a class that handles my listview of the data from my database. I'm a total newbie when it comes to listviews so hopefully someone can tell me where Ive gone wrong!
I was thinking that the null pointer was due to the fact no data was being found at the first row posistion that the cursor is at, but I think looking at the logcat, its an issue with my XML layout 'entries'?
Heres my class for the ListView:
package com.example.sqliteexample;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;
public class SQLView extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HotOrNot H = new HotOrNot(this, null, null);
setContentView(R.layout.layout);
ListView listContent = (ListView)findViewById(R.id.contentList);
HotOrNot Content = new HotOrNot(this, null, null);
Cursor cursor = Content.getData();
startManagingCursor(cursor);
@SuppressWarnings("static-access")
String [] from = new String [] {H.KEY_NAME, H.KEY_HOTNESS};
int [] to = new int [] {R.id.txtName, R.id.txtAge};
@SuppressWarnings("deprecation")
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.entries, cursor, from, to);
listContent.setAdapter(cursorAdapter);
The 'getData' method from my DB handler class:
public Cursor getData() {
String [] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
return c;
HotOrNot DB handler class:
package com.example.sqliteexample;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.ListView;
public class HotOrNot {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_HOTNESS = "persons_hotness";
private static String DATABASE_NAME = "HotOrNotdb";
private static String DATABASE_TABLE = "peopleTable";
private static int DATABASE_VERSION = 30;
private static DBHelper ourHelper;
private static Context ourContext;
private static SQLiteDatabase ourDatabase;
ListView listview;
public void DB_NAME(String DBName)
{
DATABASE_NAME = DBName;
}
public String returnDB_NAME()
{
return DATABASE_NAME;
}
public void DB_tableNAME(String DBtName)
{
DATABASE_TABLE = DBtName;
}
public String returnDB_tNAME()
{
return DATABASE_TABLE;
}
public void DB_NAME(int DBVersion)
{
DATABASE_VERSION = DBVersion;
}
public int returnDB_VERSION()
{
return DATABASE_VERSION;
}
// class constructor for context. when the object is constructed in the main programme
// the context of that class i.e 'this' is sent to this constructor to set the object context.
// null values set on SQLiteDatabase and DBhelper as there is nothing to pass from the called
// objects.
public HotOrNot (Context c, SQLiteDatabase newSQL, DBHelper BD)
{
ourContext = c;
ourDatabase = newSQL;
ourHelper = BD;
}
private static class DBHelper extends SQLiteOpenHelper
{
public DBHelper(Context context) {
super(context, KEY_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_HOTNESS + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public HotOrNot open() throws SQLException
{
ourHelper = new DBHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close()
{
ourHelper.close();
}
public long createEntry(String name, String hotness) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_HOTNESS, hotness);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public Cursor getData() {
String [] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
//String result = "";
//int iRow = c.getColumnIndex(KEY_ROWID);
//int iName = c.getColumnIndex(KEY_NAME);
//int ihotness = c.getColumnIndex(KEY_HOTNESS);
//for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
//{
//result = result + c.getString(iRow) + "" + c.getString(iName) + "" + c.getString(ihotness);
//}
//c.close();
return c;
}
public String getName(long l) {
String [] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
if(c != null)
{
// move to the selected row
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
public String getHotness(long l) {
String [] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
if(c != null)
{
// move to the selected row
c.moveToFirst();
String hotness = c.getString(2);
return hotness;
}
return null;
}
public void updateEntry(long newl, String nameEdit, String hotnessEdit) {
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_NAME, nameEdit);
cvUpdate.put(KEY_HOTNESS, hotnessEdit);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "=" + newl, null);
}
public void delID(long l) {
ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + l, null);
}
}
My XML layout for the listview:
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:" />
<TextView
android:id="@+id/txtAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age:" />
</LinearLayout>
The listview XML layout:
<?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="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/contentList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Logcat:
01-11 00:23:26.655: D/AndroidRuntime(272): Shutting down VM
01-11 00:23:26.655: W/dalvikvm(272): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-11 00:23:26.675: E/AndroidRuntime(272): FATAL EXCEPTION: main
01-11 00:23:26.675: E/AndroidRuntime(272): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqliteexample/com.example.sqliteexample.SQLView}: java.lang.NullPointerException
01-11 00:23:26.675: E/AndroidRuntime(272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-11 00:23:26.675: E/AndroidRuntime(272): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-11 00:23:26.675: E/AndroidRuntime(272): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-11 00:23:26.675: E/AndroidRuntime(272): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-11 00:23:26.675: E/AndroidRuntime(272): at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 00:23:26.675: E/AndroidRuntime(272): at android.os.Looper.loop(Looper.java:123)
01-11 00:23:26.675: E/AndroidRuntime(272): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-11 00:23:26.675: E/AndroidRuntime(272): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 00:23:26.675: E/AndroidRuntime(272): at java.lang.reflect.Method.invoke(Method.java:521)
01-11 00:23:26.675: E/AndroidRuntime(272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-11 00:23:26.675: E/AndroidRuntime(272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-11 00:23:26.675: E/AndroidRuntime(272): at dalvik.system.NativeStart.main(Native Method)
01-11 00:23:26.675: E/AndroidRuntime(272): Caused by: java.lang.NullPointerException
01-11 00:23:26.675: E/AndroidRuntime(272): at com.example.sqliteexample.HotOrNot.getData(HotOrNot.java:143)
01-11 00:23:26.675: E/AndroidRuntime(272): at com.example.sqliteexample.SQLView.onCreate(SQLView.java:24)
01-11 00:23:26.675: E/AndroidRuntime(272): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-11 00:23:26.675: E/AndroidRuntime(272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
You simply forgot to call open()
.
HotOrNot Content = new HotOrNot(this, null, null);
Content.open();
Cursor cursor = Content.getData();
(But please read about Java naming conventions, which state variables should start with a lowercase letter.)
这篇关于Android的空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!