Skip to content Skip to sidebar Skip to footer

How Do I Use Settext From Within A Callback?

In my code below I am able to edit a text from my first setText() call but not from within the callback. The error I receive is a NullPointerException for title in the callback. Ho

Solution 1:

Your setContentView() method must be called before giving references using findViewById() method, so your code will be -

publicclassListingActivityextendsAppCompatActivity {
    finalStringTAG="ListingActivity";
    TextView title;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listing_activity);

        title = (TextView)findViewById(R.id.tvTitle);

        Intentintent= getIntent();
        finalStringlistingId= intent.getStringExtra("ObjectId");

        Log.e(TAG, "listingId: " + listingId);

        title.setText("listingId");//fine

        ListingManager.getListing(listingId, newListingCB() {
            @Overridepublicvoiddone(String error, Listing listing) {
                title.setText("listingId");//null pointer error
            }
        });
    }
}

Solution 2:

title.setText("listingId");//fine

That shouldn't be fine...

You must put setContentViewbefore any findViewById. That is number one reason why your TextView is null.

Your callback is fine.

Post a Comment for "How Do I Use Settext From Within A Callback?"