我有一个dialogfragment,当用户单击我创建的xml表中的行时创建该对话框。我希望dialogfragment显示他们选择的行中显示的信息。尝试以这种方式引用数据时,我得到一个空指针异常。
这是我的dialogfragment代码:
public class EditDataDialog extends DialogFragment implements OnEditorActionListener {
public interface EditNameDialogListener {
void onFinishEditDialog(String inputText);
}
private static final int MY_CODE = 12345;
private EditText mEditTextData;
private EditText mEditTextAlias;
private TextView mTextViewId;
private TextView mTextViewAlias;
private TextView mTextViewTime;
private TextView mTextViewData;
public static DatabaseFragment dbFrag = new DatabaseFragment();
public EditDataDialog() {
// Empty constructor required for DialogFragment
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_data, container);
System.out.println("time: " + dbFrag.temp_time.getText());
//DatabaseHandler cattleDb = new DatabaseHandler(getActivity());
//Cattle cattle = cattleDb.getCattle(dbFrag.temp);
mEditTextAlias = (EditText) view.findViewById(R.id.txt_your_alias);
mEditTextData = (EditText) view.findViewById(R.id.txt_your_data);
mTextViewId = (TextView) view.findViewById(R.id.tag_id);
mTextViewAlias = (TextView) view.findViewById(R.id.tag_alias);
mTextViewTime = (TextView) view.findViewById(R.id.tag_time);
mTextViewData = (TextView) view.findViewById(R.id.tag_data);
//System.out.println(dbFrag.temp_time.getText());
mTextViewId.setText("Tag ID: test");
mTextViewAlias.setText("Tag Alias: test");
//mTextViewTime.setText("Tag Time Stamp: " + dbFrag.temp_time.getText());//dbFrag.time is NULL
mTextViewData .setText("Tag Data: test\n");
getDialog().setTitle("Enter your data");
// Show soft keyboard automatically
mEditTextData.requestFocus();
getDialog().getWindow().setSoftInputMode(
LayoutParams.WRAP_CONTENT);
mEditTextData.setOnEditorActionListener(this);
mEditTextAlias.setOnEditorActionListener(this);
return view;
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (EditorInfo.IME_ACTION_DONE == actionId) {
// Return input text to activity
EditDataDialogListener fragment = (EditDataDialogListener) getTargetFragment();//.onActivityResult(getTargetRequestCode(), MY_CODE, data);
//System.out.println(fragment);
fragment.onFinishEditDialog(mEditTextAlias.getText().toString() +","+ mEditTextData.getText().toString());
this.dismiss();
return true;
}
return false;
}
这是我的DatabaseFragment,其中在Clicks上创建了dialogfragment:
public class DatabaseFragment extends Fragment implements EditDataDialogListener {
protected static final int MY_CODE = 12345;
public DatabaseFragment() {
// Empty constructor required for fragment subclasses
}
/*The number of tags that have been
read and waiting to be placed into the database*/
int tags_found=4;
Date dateNow = new Date();
//Temporary values used when user clicks a row in the database
int temp;
int row_i = 1;
TextView temp_row;
TextView temp_id;
TextView temp_alias;
TextView temp_time;
TextView temp_data;
//Buttons in the database
Button transferDb, restoreDb;
//Class for exporting database to .csv files
ExportDatabase exportDb;
String exportFileName = "CattleManagerDb.csv";
String id = "e9-23-f3-e2-01-11";
String alias = "Doug";
String time;
String data = "data";
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_database, container, false);
transferDb = (Button) rootView.findViewById(R.id.bSQLTransfer);
restoreDb = (Button) rootView.findViewById(R.id.bSQLRestore);
final DatabaseHandler cattleDb = new DatabaseHandler(getActivity());//does this need final?
TableLayout tl1 = (TableLayout) rootView.findViewById(R.id.cattleDb);
while(tags_found>0){
View newRow = inflater.inflate(R.layout.table_row_layout, null,false);
tl1.addView(newRow);
TextView row = (TextView) newRow.findViewById(R.id.row);
TextView tag_id = (TextView) newRow.findViewById(R.id.tag_id);
TextView tag_alias = (TextView) newRow.findViewById(R.id.tag_alias);
TextView tag_time = (TextView) newRow.findViewById(R.id.tag_time);
TextView tag_data = (TextView) newRow.findViewById(R.id.tag_data);
Cattle newCattle = new Cattle();
SimpleDateFormat ft = new SimpleDateFormat ("hh:mm:ss a zzz");
time = ft.format(dateNow);
newCattle.setTagID(id);
newCattle.setAlias(alias);
newCattle.setTimeStamp(time);
newCattle.setTagData(data);
row.setText(Integer.toString(row_i));
tag_id.setText(id);
tag_alias.setText(alias);
tag_time.setText(time);
tag_data.setText(data);
cattleDb.addCattle(newCattle);
//When a user clicks a row in the database
newRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Saving temporary tag TextViews for the clicked row
temp_row = (TextView) v.findViewById(R.id.row);
temp_id = (TextView) v.findViewById(R.id.tag_id);
temp_alias = (TextView) v.findViewById(R.id.tag_alias);
temp_time = (TextView) v.findViewById(R.id.tag_time);
temp_data = (TextView) v.findViewById(R.id.tag_data);
temp = Integer.parseInt(temp_row.getText().toString());
showEditDialog();
}
});
tags_found--;
row_i++;
}
cattleDb.close();
/*
Log.d("Reading: ", "Reading all cattle...");
List<Cattle> cattle = cattleDb.getAllCattle();
for(Cattle cn : cattle) {
String log = "ID: " + cn.getID()+ " , Tag ID: " + cn.getTagID() + " , Alias: " + cn.getAlias() + " Time: " + cn.getTimeStamp() + " Data: " + cn.getTagData();
Log.d("Name: ", log);
}
*/
transferDb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {//Do we want to keep adding to the database infinitely?
//---------------------------------
//Create and transfer the .csv file
//---------------------------------
exportDb = new ExportDatabase(getActivity());
exportDb.exportDataToCSV(exportFileName);
//new AsyncEmail().addAttachment(Environment.getExternalStorageDirectory().getAbsolutePath() + exportFileName);
new AsyncEmail().execute();
//------------------
//Clear the database
//------------------
int cattle_i=1;
TableLayout tl1 = (TableLayout) getActivity().findViewById(R.id.cattleDb);
View rowHeader = inflater.inflate(R.layout.table_row_header, null, false);
tl1.removeAllViews();
tl1.addView(rowHeader);//change newRow to headerRow
int numCattle = cattleDb.getCattleCount();
while(cattle_i<=numCattle){
Cattle cattle = cattleDb.getCattle(cattle_i);
cattleDb.deleteCattle(cattle);
cattle_i++;
}
/*
Log.d("Reading: ", "Reading all cattle...");
List<Cattle> cattle = cattleDb.getAllCattle();
for(Cattle cn : cattle) {
String log = "ID: " + cn.getID()+ " , Tag ID: " + cn.getTagID() + " , Alias: " + cn.getAlias() + " Time: " + cn.getTimeStamp() + " Data: " + cn.getTagData();
Log.d("Name: ", log);
}
*/
}
});
return rootView;
}
public void onCreate(SQLiteDatabase db) {
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void showEditDialog() {
FragmentManager fm = getActivity().getFragmentManager();
EditDataDialog editDataDialog = new EditDataDialog();
editDataDialog.show(fm.beginTransaction(), "fragment_edit_data");
editDataDialog.setTargetFragment(this, MY_CODE);
}
@Override
public void onFinishEditDialog(String inputText) {
DatabaseHandler cattleDb = new DatabaseHandler(getActivity());
Cattle cattle = cattleDb.getCattle(temp);
int commaOffset = inputText.indexOf(",");
String inputAlias = inputText.substring(0,commaOffset).trim();
String inputData = inputText.substring(commaOffset+1).trim();
System.out.println(inputAlias);
System.out.println(inputData);
if(inputAlias.length()!=0) {//Only update if field was modified
cattle.setAlias(inputAlias);
temp_alias.setText(inputAlias);
cattleDb.updateCattle(cattle);
}
if(inputData.length()!=0) {//Only update if field was modified
cattle.setTagData(inputData);
temp_data.setText(inputData);
cattleDb.updateCattle(cattle);
}
Log.d("Reading: ", "Reading all cattle...");
List<Cattle> cattleList = cattleDb.getAllCattle();
for(Cattle cn : cattleList) {
String log = "ID: " + cn.getID()+ " , Tag ID: " + cn.getTagID() + " , Alias: " + cn.getAlias() + " Time: " + cn.getTimeStamp() + " Data: " + cn.getTagData();
Log.d("Name: ", log);
}
}
}
这是LogCat:
12-02 22:08:00.932: I/Process(31818): Sending signal. PID: 31818 SIG: 9
12-02 22:10:52.880: D/AbsListView(605): Get MotionRecognitionManager
12-02 22:10:52.890: D/AbsListView(605): onVisibilityChanged() is called, visibility : 4
12-02 22:10:52.890: D/AbsListView(605): unregisterIRListener() is called
12-02 22:10:52.900: D/AbsListView(605): onVisibilityChanged() is called, visibility : 0
12-02 22:10:52.900: D/AbsListView(605): unregisterIRListener() is called
12-02 22:10:52.900: D/AbsListView(605): unregisterIRListener() is called
12-02 22:10:52.910: I/endeffect(605): AbsListView.onMeasure(), getWidth()=0, getHeight()=0, this=android.widget.ListView{42ab9898 VFED.VC. ......I. 0,0-0,0 #7f0b0002 app:id/left_drawer}
12-02 22:10:52.920: D/libEGL(605): loaded /system/lib/egl/libEGL_adreno200.so
12-02 22:10:52.920: D/libEGL(605): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
12-02 22:10:52.930: D/libEGL(605): loaded /system/lib/egl/libGLESv2_adreno200.so
12-02 22:10:52.930: I/Adreno200-EGL(605): <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: (CL3579225)
12-02 22:10:52.930: I/Adreno200-EGL(605): Build Date: 04/22/13 Mon
12-02 22:10:52.930: I/Adreno200-EGL(605): Local Branch: adreno_sba_20130419
12-02 22:10:52.930: I/Adreno200-EGL(605): Remote Branch:
12-02 22:10:52.930: I/Adreno200-EGL(605): Local Patches:
12-02 22:10:52.930: I/Adreno200-EGL(605): Reconstruct Branch:
12-02 22:10:52.990: D/OpenGLRenderer(605): Enabling debug mode 0
12-02 22:10:53.020: D/AbsListView(605): unregisterIRListener() is called
12-02 22:10:53.020: I/endeffect(605): AbsListView.onLayout(), getWidth()=720, getHeight()=1701, this=android.widget.ListView{42ab9898 VFED.VC. ......ID -720,0-0,1701 #7f0b0002 app:id/left_drawer}
12-02 22:10:53.020: D/AbsListView(605): onVisibilityChanged() is called, visibility : 4
12-02 22:10:53.020: D/AbsListView(605): unregisterIRListener() is called
12-02 22:10:53.170: D/AbsListView(605): unregisterIRListener() is called
12-02 22:10:53.180: I/endeffect(605): AbsListView.onMeasure(), getWidth()=720, getHeight()=1701, this=android.widget.ListView{42ab9898 IFED.VC. ......ID -720,0-0,1701 #7f0b0002 app:id/left_drawer}
12-02 22:10:53.190: D/AbsListView(605): unregisterIRListener() is called
12-02 22:10:53.190: I/endeffect(605): AbsListView.onLayout(), getWidth()=720, getHeight()=1701, this=android.widget.ListView{42ab9898 IFED.VC. ......ID -720,0-0,1701 #7f0b0002 app:id/left_drawer}
12-02 22:11:04.932: D/GestureDetector(605): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0
12-02 22:11:04.942: D/AbsListView(605): onVisibilityChanged() is called, visibility : 0
12-02 22:11:04.942: D/AbsListView(605): unregisterIRListener() is called
12-02 22:11:05.582: D/GestureDetector(605): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0
12-02 22:11:05.673: I/endeffect(605): AbsListView.onMeasure(), getWidth()=720, getHeight()=1701, this=android.widget.ListView{42ab9898 VFED.VC. ......ID 0,0-720,1701 #7f0b0002 app:id/left_drawer}
12-02 22:11:05.673: D/AbsListView(605): unregisterIRListener() is called
12-02 22:11:05.673: I/endeffect(605): AbsListView.onLayout(), getWidth()=720, getHeight()=1701, this=android.widget.ListView{42ab9898 VFED.VC. ......ID 0,0-720,1701 #7f0b0002 app:id/left_drawer}
12-02 22:11:06.053: D/AbsListView(605): onVisibilityChanged() is called, visibility : 4
12-02 22:11:06.053: D/AbsListView(605): unregisterIRListener() is called
12-02 22:11:06.613: D/GestureDetector(605): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 3 mFalseSizeCnt:0
12-02 22:11:06.634: D/AndroidRuntime(605): Shutting down VM
12-02 22:11:06.634: W/dalvikvm(605): threadid=1: thread exiting with uncaught exception (group=0x41787ac8)
12-02 22:11:06.634: E/AndroidRuntime(605): FATAL EXCEPTION: main
12-02 22:11:06.634: E/AndroidRuntime(605): java.lang.NullPointerException
12-02 22:11:06.634: E/AndroidRuntime(605): at com.rfidsensorsystems.cattletracker.EditDataDialog.onCreateView(EditDataDialog.java:41)
12-02 22:11:06.634: E/AndroidRuntime(605): at android.app.Fragment.performCreateView(Fragment.java:1695)
12-02 22:11:06.634: E/AndroidRuntime(605): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
12-02 22:11:06.634: E/AndroidRuntime(605): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
12-02 22:11:06.634: E/AndroidRuntime(605): at android.app.BackStackRecord.run(BackStackRecord.java:682)
12-02 22:11:06.634: E/AndroidRuntime(605): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
12-02 22:11:06.634: E/AndroidRuntime(605): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
12-02 22:11:06.634: E/AndroidRuntime(605): at android.os.Handler.handleCallback(Handler.java:725)
12-02 22:11:06.634: E/AndroidRuntime(605): at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 22:11:06.634: E/AndroidRuntime(605): at android.os.Looper.loop(Looper.java:137)
12-02 22:11:06.634: E/AndroidRuntime(605): at android.app.ActivityThread.main(ActivityThread.java:5328)
12-02 22:11:06.634: E/AndroidRuntime(605): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 22:11:06.634: E/AndroidRuntime(605): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 22:11:06.634: E/AndroidRuntime(605): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-02 22:11:06.634: E/AndroidRuntime(605): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-02 22:11:06.634: E/AndroidRuntime(605): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
仔细检查所有视图ID是否正确。错误消息显示NullPointerException是第41行。它看起来像:
mTextViewAlias.setText("Tag Alias: test");
确保mTextViewAlias实际上已设置为控件。你经常使用错误的id会感到惊讶。