Android AlertDialog TextView SetText提供NullPointerException

时间:2014-04-17 00:34:00

标签: android nullpointerexception textview alertdialog settext

我正在尝试打开一个AlertDialog,它使用与打开它的活动相同的信息。当我使用setText作为Dialog时,抛出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.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;
}

}

由于

1 个答案:

答案 0 :(得分:1)

final TextView popupTypeOfTransactionTV = (TextView)view
        .findViewById(R.id.log_popup_type);

应该是

final TextView popupTypeOfTransactionTV = (TextView) promptsView
        .findViewById(R.id.log_popup_type);

如果它在那个布局中。现在它正在查看传递给View

onClick()