I am trying to open an AlertDialog that uses the same information that the activity it is opened from. A NullPointerExcaption is thrown when I use setText for the Dialog. LogCat says:

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)

Line 118 where the exception happens is:

popupTypeOfTransactionTV.setText(outputtext);

And the whole code:

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;
}

}

Thanks

有帮助吗?

解决方案

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

should be

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

if it's in that layout. Right now it is looking inside the View passed to the onClick()

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top