Android AlertDialog TextView SetText提供NullPointerException
我试图打开一个AlertDialog,该对话框使用的信息与从中打开它的活动相同。当我为对话框使用setText时,会引发NullPointerExcaption。LogCat说:Android AlertDialog TextView SetText提供NullPointerException,android,nullpointerexception,textview,android-alertdialog,settext,Android,Nullpointerexception,Textview,Android Alertdialog,Settext,我试图打开一个AlertDialog,该对话框使用的信息与从中打开它的活动相同。当我为对话框使用setText时,会引发NullPointerExcaption。LogCat说: 04-17 01:14:03.878: E/AndroidRuntime(4860): java.lang.NullPointerException 04-17 01:14:03.878: E/AndroidRuntime(4860): at com.giantwell.moneycounter.data.Adapt
04-17 01:14:03.878: E/AndroidRuntime(4860): java.lang.NullPointerException
04-17 01:14:03.878: E/AndroidRuntime(4860): at
com.giantwell.moneycounter.data.Adapters.TransactionLogActivityCursorAdapter$1.onClick(TransactionLogActivityCursorAdapter.java:118)
发生异常的第118行是:
popupTypeOfTransactionTV.setText(outputtext);
而整个代码:
public class TransactionLogActivityCursorAdapter extends CursorAdapter {
LayoutInflater inflater;
int valueOfTransaction = 0;
public TransactionLogActivityCursorAdapter(Context context, Cursor c, int flags) {
super(context,c,flags);
inflater = LayoutInflater.from(context);
}
@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
final int typeOfTransaction = cursor.getInt(cursor.getColumnIndex(Storage.TYPE_OF_TRANSACTION));
final int valueOfTransaction = cursor.getInt(cursor.getColumnIndex(Storage.VALUE_OF_TRANSACTION));
final long timeOfTransaction = cursor.getLong(cursor.getColumnIndex(Storage.TIME_OF_TRANSACTION));
final String notesOnTransaction = cursor.getString(cursor.getColumnIndex(Storage.NOTES_ON_TRANSACTION));
final TextView typeOfTransactionTV = (TextView)view.findViewById(R.id.type_of_transaction);
final TextView valueOfTransactionTV = (TextView)view.findViewById(R.id.value_of_transaction);
TextView timeOfTransactionTV = (TextView)view.findViewById(R.id.time_of_transaction);
final TextView notesOnTransactionTV = (TextView)view.findViewById(R.id.notes_on_transaction);
switch(typeOfTransaction) {
case 0: typeOfTransactionTV.setText("In"); break;
case 1: typeOfTransactionTV.setText("Out"); break;
case 2: typeOfTransactionTV.setText("Change"); break;
default : typeOfTransactionTV.setText("error"); break;
}
if(valueOfTransaction >= 100 || valueOfTransaction <= -100){
valueOfTransactionTV.setText("£" + Double.toString((double)valueOfTransaction/100));
}
else{
valueOfTransactionTV.setText(Integer.toString(valueOfTransaction) + "p");
}
timeOfTransactionTV.setText(dateFromUnixTime(timeOfTransaction));
notesOnTransactionTV.setText(notesOnTransaction);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Storage store = new Storage(v.getContext());
v.setSelected(true);
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.log_popup, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
//Set up EditText, put current notes in it and put cursor at the end of string
final EditText userInput = (EditText) promptsView.findViewById(R.id.notes_on_transaction);
userInput.setText(store.getNotesOnTransaction(timeOfTransaction).toString(), TextView.BufferType.EDITABLE);
userInput.setSelection(userInput.getText().length());
//alertDialogBuilder.setView(popupTypeOfTransactionTV);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//Update database and the TextView in the activity
store.setNotesOnTransaction(timeOfTransaction, userInput.getText().toString());
notesOnTransactionTV.setText(store.getNotesOnTransaction(timeOfTransaction).toString());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
final TextView popupTypeOfTransactionTV = (TextView)view.findViewById(R.id.log_popup_type);
String outputtext = typeOfTransactionTV.getText().toString();
Log.d("ouputtext", outputtext);
popupTypeOfTransactionTV.setText(outputtext);
}
});
}
@SuppressLint("SimpleDateFormat")
public String dateFromUnixTime(long unixTime) {
Date normalDate = new Date(unixTime);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM d, hh:mm");
String dateAsString = simpleDateFormat.format(normalDate);
return dateAsString;
}
@SuppressLint("SimpleDateFormat")
public String expandedDateFromUnixTime(long unixTime) {
Date normalDate = new Date(unixTime);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY MMM dd, hh:mm aa");
String dateAsString = simpleDateFormat.format(normalDate);
return dateAsString;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.log_row, parent, false);
bindView(v,context,cursor);
return v;
}
}
公共类TransactionLogActivityCursorAdapter扩展了CursorAdapter{
充气机;
int valueOfTransaction=0;
PublicTransactionLogActivityCursorAdapter(上下文上下文、游标c、int标志){
super(上下文、c、标志);
充气器=充气器。从(上下文);
}
@凌驾
公共void bindView(最终视图、最终上下文上下文、最终光标){
final int typeOfTransaction=cursor.getInt(cursor.getColumnIndex(Storage.TYPE OF_事务));
TRANSACTION的最终int值=cursor.getInt(cursor.getColumnIndex(存储.VALUE_OF_事务));
final long timeOfTransaction=cursor.getLong(cursor.getColumnIndex(Storage.TIME_OF_事务));
最终字符串notesOnTransaction=cursor.getString(cursor.getColumnIndex(Storage.NOTES_ON_事务));
最终文本视图typeOfTransactionTV=(文本视图)视图.findViewById(R.id.typeOfTransaction);
TransactionTV的最终文本视图值=(文本视图)view.findViewById(R.id.value\u of\u事务);
TextView timeOfTransactionTV=(TextView)view.findViewById(R.id.time of the_transaction);
final TextView notesOnTransactionTV=(TextView)view.findViewById(R.id.notes on_transaction);
开关(变速器类型){
案例0:typeOfTransactionTV.setText(“In”);break;
案例1:typeOfTransactionTV.setText(“Out”);break;
案例2:typeOfTransactionTV.setText(“更改”);break;
默认值:typeOfTransactionTV.setText(“错误”);break;
}
如果(变速箱值>=100 | |变速箱值
应该是
final TextView popupTypeOfTransactionTV = (TextView) promptsView
.findViewById(R.id.log_popup_type);
现在它正在查看传递给onClick()
的视图中TransactionLogActivityCursorAdapter.java
中的第118行是log\u popup.xml
中的log\u popup\u type
?
final TextView popupTypeOfTransactionTV = (TextView) promptsView
.findViewById(R.id.log_popup_type);