最后一行有一个NPE,我无法弄清楚它来自哪里。有什么想法吗?
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();
}
}
)
;
}
}
以下是名为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>
答案 0 :(得分:1)
listContent
。
在尝试setContentView
之前,您可能需要致电findViewById
。
更新
基于更多信息和提供的代码,因为您声明的ListView id
android:id="@android:id/list"
它指的是Android ID不是您自己的。所以你需要通过
搜索它findViewById(android.R.id.list)
不是你的包裹。
但是,如果ListActivity提供所有方法,你甚至不需要它,所以你不必直接访问它?例如,
setListAdapter(...);
请参阅此处示例http://www.higherpass.com/Android/Tutorials/Creating-Lists-Using-The-Android-Listactivity/
答案 1 :(得分:0)
包含NPE的堆栈跟踪非常有用。但我怀疑问题可能就在NPE之前的这一行:
mySQLiteAdapter.close();
如果SQLiteAdapter
类与Android Notepad tutorial中的NotesDbAdapter
类相似,那么您不应该在那里关闭它;这会使光标无效。而是在活动的onDestroy方法中关闭它。 (最后一个链接转到我写的关于何时以及如何关闭这些对象的博客文章。)