1

In the code below, I am reacting to an activity return. When I get the correct code and result, I want to put the result of "data" into an uneditable text view. I have tried to set the text view directly to the intent data using the following code:

encodedText.setText(data.getData().toString());

However I get an exception error when I try to do this. I then tried to do it the following way(See arrow):

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == request_Code) {
        if (resultCode == RESULT_OK) {
            String encoded = data.getData().toString();
            TextView encodedText = (TextView) findViewById(R.id.result); 
        encodedText.setText(encoded); <------
        }            
    }
}

I put a break point at the line the arrow points to in the code example, and it is this line the program has a problem with. I have looked at the setText() function in the textView docs and I still am not sure what I am doing wrong. Anyone see what I don't?

5
  • I think your encodedText is null it isn't found in the layout. Are you sure you have this TextView in your activity layout? Commented Feb 23, 2012 at 16:32
  • encodedText is pointing to "result' which is the name of the textview in the layout. Commented Feb 23, 2012 at 16:41
  • You need to post the stacktrace, otherwise it's impossible to answer your question. Commented Feb 23, 2012 at 16:49
  • Slukian, You were right. I had the TextView in the activity layout, but it currently was not active as the activity this problem was referencing was not active. I changed the view to the current layout that the user sees and it works now. Thank you. Commented Feb 23, 2012 at 16:57
  • Are you setting the Intent in the setResult(resultCode, intent) method ? Maybe you should try putting extras into the intent before setting result and trying to read that instead. Commented Feb 23, 2012 at 16:58

2 Answers 2

1

You are getting a NullPointerException when trying to set the text(encodedText.setText(encoded);) because your TextView that you lookup with findViewById() is null. Check your layout so you do have this TextView in the layout and avoid the NullPointerException.

Sign up to request clarification or add additional context in comments.

Comments

1

Since I assume it's a String object in the first place you can just cast it.

String encoded = (String) data.getData();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.