Java SQLite+;setAdapter=NPE
在最后一行有一个NPE,我不知道它来自哪里。有什么想法吗Java SQLite+;setAdapter=NPE,java,Java,在最后一行有一个NPE,我不知道它来自哪里。有什么想法吗 mySQLiteAdapter = new SQLiteAdapter(this); mySQLiteAdapter.openToRead(); listContent = (ListView)findViewById(R.id.contentlist); Cursor cursor = mySQLiteAdapter.queueAll(); startManagingCursor(cursor); String
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
listContent = (ListView)findViewById(R.id.contentlist);
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{SQLiteAdapter.KEY_CHOICE,
SQLiteAdapter.KEY_AMOUNT};
int[] to = new int[]{R.id.txtChoice, R.id.txtAmtSpent};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row2, cursor, from, to);
mySQLiteAdapter.close();
listContent.setAdapter(cursorAdapter);
这里是错误
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): FATAL EXCEPTION: main
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whycom.idontknow/com.whycom.idontknow.ListSpent}: java.lang.NullPointerException
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at android.os.Looper.loop(Looper.java:123)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at java.lang.reflect.Method.invokeNative(Native Method)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at java.lang.reflect.Method.invoke(Method.java:521)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at dalvik.system.NativeStart.main(Native Method)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): Caused by: java.lang.NullPointerException
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at com.whycom.idontknow.ListSpent.makeList(ListSpent.java:161)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at com.whycom.idontknow.ListSpent.onCreate(ListSpent.java:79)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-19 01:27:50.832: ERROR/AndroidRuntime(1425): ... 11 more
整个代码如下:
package com.whycom.idontknow;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ListSpent extends ListActivity {
private SQLiteAdapter mySQLiteAdapter;
//Intent splat;
//Bundle b;
// String[] amounts;
// String[] expenditureArray;
String choice;
String amount;
int count;
String[] dataAry= new String[100];
//String[] choices;
String Count;
SharedPreferences prefs;
TextView txtAmtSpent;
TextView txtChoice;
ListAdapter adapter;
ListView listContent;
int j;
// ListView listContent;
// PreferenceManager.getDefaultSharedPreferences(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlist);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
//ListView listContent = (ListView)findViewById(R.id.contentlist);
initVars();
try {
buildArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
makeList();
}
static class ViewHolder{
TextView text;
TextView button;
}
public void buildArray() throws IOException{
//ArrayList<String> al = new ArrayList<String>();
InputStream instream;
try {
instream = openFileInput("mySpends.txt");
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
Count = prefs.getString("Count" , "0");
count = Integer.parseInt(Count);
for (int i = 0; i < ((count)*2); i=i+2){
choice = buffreader.readLine();
amount = buffreader.readLine();
dataAry[i] = choice;
dataAry[i+1] = amount;
//trial
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private void initVars(){
txtAmtSpent = (TextView)findViewById(R.id.txtAmtSpent);
txtChoice = (TextView)findViewById(R.id.txtChoice);
listContent = (ListView)findViewById(R.id.contentlist);
}
private void makeList(){
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.deleteAll();
ListView listContent = (ListView)findViewById(R.id.contentlist);
String [] dataAryArray = new String[(count)*2];
j = 0;
int ind = (count)*2-1;
for (int i = -1; i < ind; ind = ind - 2){
dataAryArray[j] = dataAry[ind];
dataAryArray[j+1] = dataAry[ind-1];
mySQLiteAdapter.insert(dataAryArray[j],dataAryArray[j+1]);
j=j+2;
}
mySQLiteAdapter.close();
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String c=SQLiteAdapter.KEY_CHOICE;
String a=SQLiteAdapter.KEY_AMOUNT;
String[] from = new String[]{SQLiteAdapter.KEY_CHOICE, SQLiteAdapter.KEY_AMOUNT};
int[] to = new int[]{R.id.txtChoice, R.id.txtAmtSpent};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.id.text, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
// adapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1, dataAryArray);
// setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
// Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
// Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Toast",
Toast.LENGTH_SHORT).show();
}
}
)
;
}
}
package com.whycom.idonknow;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.io.BufferedReader;
导入java.io.EOFException;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.lang.reflect.Array;
导入java.util.ArrayList;
导入android.app.ListActivity;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.database.Cursor;
导入android.os.Bundle;
导入android.preference.PreferenceManager;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入android.widget.SimpleCursorAdapter;
导入android.widget.TextView;
导入android.widget.Toast;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
公共类ListActivity扩展了ListActivity{
私有SQLiteAdapter mySQLiteAdapter;
//意向性喷溅;
//束b;
//字符串[]金额;
//字符串[]重新排列;
字符串选择;
字符串数量;
整数计数;
字符串[]数据元=新字符串[100];
//字符串[]选项;
字符串计数;
共享引用优先权;
TextView txtAmtSpent;
TextView-txtChoice;
列表适配器;
列表视图列表内容;
int j;
//列表视图列表内容;
//PreferenceManager.GetDefaultSharedReferences(此文件);
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlist);
prefs=PreferenceManager.getDefaultSharedPreferences(此);
//ListView listContent=(ListView)findViewById(R.id.contentlist);
initVars();
试一试{
buildArray();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
makeList();
}
静态类视窗夹{
文本查看文本;
文本视图按钮;
}
public void buildArray()引发IOException{
//ArrayList al=新的ArrayList();
流内输入流;
试一试{
instream=openFileInput(“mySpends.txt”);
InputStreamReader inputreader=新的InputStreamReader(流内);
BufferedReader buffreader=新的BufferedReader(inputreader);
Count=prefs.getString(“Count”,“0”);
count=Integer.parseInt(count);
对于(整数i=0;i<((计数)*2);i=i+2){
choice=buffreader.readLine();
amount=buffreader.readLine();
数据元[i]=选择;
数据元[i+1]=金额;
//审判
}
}捕获(FileNotFoundException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
}
私有void initVars(){
txtAmtSpent=(TextView)findViewById(R.id.txtAmtSpent);
txtChoice=(TextView)findViewById(R.id.txtChoice);
listContent=(ListView)findViewById(R.id.contentlist);
}
私有void生成列表(){
mySQLiteAdapter=新的SQLiteAdapter(此);
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.deleteAll();
ListView listContent=(ListView)findViewById(R.id.contentlist);
字符串[]数据数组=新字符串[(计数)*2];
j=0;
int ind=(计数)*2-1;
对于(int i=-1;i
以下是名为mainlist.XML的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
未找到
列表内容
。
在尝试findViewById
之前,您可能需要调用setContentView
更新 基于更多信息和提供的代码,由于您声明的ListView id
android:id="@android:id/list"
它指的是Android id,而不是你自己的。所以你需要通过
findViewById(android.R.id.list)
不是你的包裹
但你为什么还要这么做
setListAdapter(...);
mySQLiteAdapter.close();