我想在 ListView(ListFragment) 中插入一个新的视图数据行,我已经在方法中的 setAdapter() 中声明了数组,但是它有 NullPointerException 错误我应该如何adapter.notifyDataSetChanged();
在新方法中使用?
我再次尝试在新方法中声明游标,或在类中声明 SimpleCursorAdapter,但它们有一些错误
日志猫:
03-21 02:41:30.099: W/dalvikvm(4130): threadid=1: thread exiting with uncaught exception (group=0xa4b60648)
03-21 02:41:30.103: E/AndroidRuntime(4130): FATAL EXCEPTION: main
03-21 02:41:30.103: E/AndroidRuntime(4130): java.lang.NullPointerException
03-21 02:41:30.103: E/AndroidRuntime(4130): at com.example.mycalc.NoteFragment.updateView(NoteFragment.java:80)
03-21 02:41:30.103: E/AndroidRuntime(4130): at com.example.mycalc.MainActivity.updateResult(MainActivity.java:34)
03-21 02:41:30.103: E/AndroidRuntime(4130): at com.example.mycalc.CalcFragment$1.onClick(CalcFragment.java:95)
03-21 02:41:30.103: E/AndroidRuntime(4130): at android.view.View.performClick(View.java:4240)
03-21 02:41:30.103: E/AndroidRuntime(4130): at android.view.View$PerformClick.run(View.java:17721)
03-21 02:41:30.103: E/AndroidRuntime(4130): at android.os.Handler.handleCallback(Handler.java:730)
03-21 02:41:30.103: E/AndroidRuntime(4130): at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 02:41:30.103: E/AndroidRuntime(4130): at android.os.Looper.loop(Looper.java:137)
03-21 02:41:30.103: E/AndroidRuntime(4130): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-21 02:41:30.103: E/AndroidRuntime(4130): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 02:41:30.103: E/AndroidRuntime(4130): at java.lang.reflect.Method.invoke(Method.java:525)
03-21 02:41:30.103: E/AndroidRuntime(4130): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-21 02:41:30.103: E/AndroidRuntime(4130): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-21 02:41:30.103: E/AndroidRuntime(4130): at dalvik.system.NativeStart.main(Native Method)
注意片段:
package com.example.mycalc;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.widget.ListView;
public class NoteFragment extends ListFragment {
private DB mDbHelper;
private Cursor mCursor;
private SimpleCursorAdapter adapter;
//String[] from_column = new String[]{DB.KEY_ITEM, DB.KEY_CREATED};
//int[] to_layout = new int[]{android.R.id.text1, android.R.id.text2};
//SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2,
//mCursor, from_column, to_layout);
private Cursor newCursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//int layout = android.R.layout.simple_list_item_1;
//ArrayAdapter<String> adapter = new ArrayAdapter(getActivity(), layout, CalcFragment.InputResult);
//setListAdapter(adapter);
setAdapter();
if(Debug.log){ Log.d("NoteFragment", "onCreated");}
}
@Override
public void onStart() {
super.onStart();
FragmentManager fm = getFragmentManager();
if( fm.findFragmentById(R.id.note_fragment)!= null){
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
public void setAdapter(){
mDbHelper= new DB(getActivity());
mDbHelper.open();
mCursor = mDbHelper.getAll();
getActivity().startManagingCursor(mCursor);
String[] from_column = new String[]{DB.KEY_ITEM, DB.KEY_CREATED};
int[] to_layout = new int[]{android.R.id.text1, android.R.id.text2};
adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2,
mCursor, from_column, to_layout);
setListAdapter(adapter);
if(Debug.log){ Log.d("NoteFragment", "setAdapter");}
}
public void updateView(){
//mDbHelper= new DB(getActivity());
//mDbHelper.open();
//mCursor = mDbHelper.getAll();
//getActivity().startManagingCursor(mCursor);*/
//String[] from_column = new String[]{DB.KEY_ITEM, DB.KEY_CREATED};
//int[] to_layout = new int[]{android.R.id.text1, android.R.id.text2};
adapter.changeCursor(mCursor);
adapter.notifyDataSetChanged();
//if(Debug.log){ Log.d("NoteFragment", "updateView");}
}
}
主要活动:
public class MainActivity extends FragmentActivity implements CalcFragment.OnEqualSelectedListener {
...
@Override
public void updateResult() {
NoteFragment fragmentResult = new NoteFragment();
fragmentResult.updateView();
if(Debug.log){ Log.d("MainActivity", "updateResult");}
}
新的错误信息:
03-22 13:49:07.321: W/dalvikvm(1566): threadid=1: thread exiting with uncaught exception (group=0xa4c21648)
03-22 13:49:07.325: E/AndroidRuntime(1566): FATAL EXCEPTION: main
03-22 13:49:07.325: E/AndroidRuntime(1566): java.lang.NullPointerException
03-22 13:49:07.325: E/AndroidRuntime(1566): at com.example.mycalc.NoteFragment.updateView(NoteFragment.java:80)
03-22 13:49:07.325: E/AndroidRuntime(1566): at com.example.mycalc.MainActivity.updateResult(MainActivity.java:34)
03-22 13:49:07.325: E/AndroidRuntime(1566): at com.example.mycalc.CalcFragment$1.onClick(CalcFragment.java:155)
03-22 13:49:07.325: E/AndroidRuntime(1566): at android.view.View.performClick(View.java:4240)
03-22 13:49:07.325: E/AndroidRuntime(1566): at android.view.View$PerformClick.run(View.java:17721)
03-22 13:49:07.325: E/AndroidRuntime(1566): at android.os.Handler.handleCallback(Handler.java:730)
03-22 13:49:07.325: E/AndroidRuntime(1566): at android.os.Handler.dispatchMessage(Handler.java:92)
03-22 13:49:07.325: E/AndroidRuntime(1566): at android.os.Looper.loop(Looper.java:137)
03-22 13:49:07.325: E/AndroidRuntime(1566): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-22 13:49:07.325: E/AndroidRuntime(1566): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 13:49:07.325: E/AndroidRuntime(1566): at java.lang.reflect.Method.invoke(Method.java:525)
03-22 13:49:07.325: E/AndroidRuntime(1566): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-22 13:49:07.325: E/AndroidRuntime(1566): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-22 13:49:07.325: E/AndroidRuntime(1566): at dalvik.system.NativeStart.main(Native Method)