How do I use setText from within a callback? - android

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.
How do I set this up so I can edit the text from within the callback?
public class ListingActivity extends AppCompatActivity {
final String TAG = "ListingActivity";
TextView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
title = (TextView)findViewById(R.id.tvTitle);
Intent intent = getIntent();
final String listingId = intent.getStringExtra("ObjectId");
setContentView(R.layout.listing_activity);
Log.e(TAG, "listingId: " + listingId);
title.setText("listingId");//fine
ListingManager.getListing(listingId, new ListingCB() {
#Override
public void done(String error, Listing listing) {
title.setText("listingId");//null pointer error
}
});
}
}

Your setContentView() method must be called before giving references using findViewById() method, so your code will be -
public class ListingActivity extends AppCompatActivity {
final String TAG = "ListingActivity";
TextView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listing_activity);
title = (TextView)findViewById(R.id.tvTitle);
Intent intent = getIntent();
final String listingId = intent.getStringExtra("ObjectId");
Log.e(TAG, "listingId: " + listingId);
title.setText("listingId");//fine
ListingManager.getListing(listingId, new ListingCB() {
#Override
public void done(String error, Listing listing) {
title.setText("listingId");//null pointer error
}
});
}
}

title.setText("listingId");//fine
That shouldn't be fine...
You must put setContentView before any findViewById. That is number one reason why your TextView is null.
Your callback is fine.

Related

I want to Pass the result of the QR Code from ScannerCodeActivity to the TextView in ResultWebService

I want to passe the result of the QR code from the ScanCodeActivity to the TextView in other layout called ResultWebService.
#Override
public void handleResult(Result result) {
ResultWebService.resultTExtViewCode.setText(result.getText());
onBackPressed();
}
this is my ResultWebService :
public class ResultWebService extends AppCompatActivity {
public static TextView resultTExtViewCode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_web_service);
resultTExtViewCode = (TextView) findViewById(R.id.textViw_QRcode);
}
}
How can i do this please ?
You can do that, but the better way is to send it in the intent as extras, or have a global variable and assign the text value of the result there.
Intent i=new Intent(context);
i.putExtra("QrText", result.getText());
context.startActivity(i);
and to get it in the other activity
Bundle extras = getIntent().getExtras();
String qrText;
if (extras != null) {
qrText = extras.getString("QrText");
}
// after you do the findViewById
resultTExtViewCode.setText(qrText)

PutExtra getExtra in RecyclerView not working

I need help.
I create android recyclerview cardview project, when data list from cardview clicked (use putExtra dan getExtra) will call new activity , but not working (in new activity blank no content).
this my code (CardViewAndroidAdapter.java) my full code error when i paste in here - ask in stackoverflow
holder.btnDetail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("PUT_PHOTO", getListAndroid().get(position).getPhoto());
intent.putExtra("PUT_DESK", getListAndroid().get(position).getDesk());
context.startActivity(intent);
}
});
this my code (DetailActivity.java) my full code error when i paste in here
public class DetailActivity extends AppCompatActivity {
private static final String GET_PHOTO = "get_photo";
private static final String GET_DESK = "get_desk";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getSupportActionBar().setTitle("Detail Version");
getIncomingIntent();
}
private void getIncomingIntent() {
if(getIntent().hasExtra(GET_PHOTO)&&getIntent().hasExtra(GET_DESK)){
String getPhoto=getIntent().getStringExtra(GET_PHOTO);
String getDesk = getIntent().getStringExtra(GET_DESK);
TextView detail=findViewById(R.id.tv_detail);
detail.setText(getDesk);
ImageView img_item_photo=findViewById(R.id.img_item_photo);
Glide.with(this)
.load(getPhoto)
.into(img_item_photo);
}
}
Replace this:
intent.putExtra("PUT_PHOTO", getListAndroid().get(position).getPhoto());
intent.putExtra("PUT_DESK", getListAndroid().get(position).getDesk());
with this:
intent.putExtra(DetailActivity.GET_PHOTO, getListAndroid().get(position).getPhoto());
intent.putExtra(DetailActivity.GET_DESK, getListAndroid().get(position).getDesk());
It should work now.

getStringExtra() value is not getting set in URL

I have passed a value through putExtra from first screen and fetched by getStringExtra method to second screen. To confirm that I have received the value, I passed it as TextView on second screen and I was able to get it.
Now I am passing this value to url, but android studio is showing redline under it. During mouse rollover it shows following error.
Cannot resolve symbol 'cropcategoryname'
The code is as follows.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
TextView txtName = (TextView) findViewById(R.id.cropCategoryName);
Intent i = getIntent();
String cropcategoryname = i.getStringExtra("cropCategoryName");
txtName.setText(cropcategoryname);
} private String url = "http://dsy.impras.in/mup/cropname.php?cropcategory=" + cropcategoryname;
I believe it is a simple problem but I've been stuck on it for a while.
String cropcategoryname = i.getStringExtra("cropCategoryName");
This is a local variable in the onCreate() method.
private String url = "http://dsy.impras.in/mup/cropname.php?cropcategory=" + cropcategoryname;
This is an instance field in the activity class. The local variable in onCreate() does not exist when this code is executed.
I suggest that you learn about variable scope and the activity lifecycle. Since the value for url relies on a value that is only available in onCreate(), you must initialize url in that method, not inline with its field declaration:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
TextView txtName = (TextView) findViewById(R.id.cropCategoryName);
Intent i = getIntent();
String cropcategoryname = i.getStringExtra("cropCategoryName");
txtName.setText(cropcategoryname);
url = "http://dsy.impras.in/mup/cropname.php?cropcategory=" + cropcategoryname;
}
private String url;
Try this:
Use this in your CropnameActivity before onCreate:
private static final String EXTRA_NAME = "EXTRA_NAME";
private String cropcategoryname;
private String url = "http://dsy.impras.in/mup/cropname.php?cropcategory=";
private static Intent createIntent(Context context, String cropCategoryName) {
Intent intent = new Intent(context, CropnameActivity .class);
intent.putExtra(EXTRA_NAME, cropCategoryName);
return intent;
}
public static void startActivity(Activity activity, String cropCategoryName) {
activity.startActivity(createIntent(activity, cropCategoryName));
}
from your MainActivity do this:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String cropCategoryName =((TextView)view.findViewById(R.id.cropCategoryName)).getTex‌​t().toString();
Toast.makeText(getApplicationContext(),cropCategoryName,Toas‌​t.LENGTH_SHORT).show‌​();
CropnameActivity.startActivity(MainActivity.this, cropCategoryName);
});
then retrieve your String as such:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
TextView txtName = (TextView) findViewById(R.id.cropCategoryName);
cropcategoryname = getIntent().getStringExtra(EXTRA_NAME);
if(cropcategoryname != null) txtName.setText(cropcategoryname);
}
After that you can use the url String as such:
if(cropcategoryname != null) {
String finalUrl = url + cropcategoryname;
}
Hope it helps!!!
Simply initialize the cropcategoryname string variable above the onCreate method.
String cropcategoryname="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
TextView txtName = (TextView) findViewById(R.id.cropCategoryName);
Intent i = getIntent();
cropcategoryname = i.getStringExtra("cropCategoryName");
txtName.setText(cropcategoryname);
} private String url = "http://dsy.impras.in/mup/cropname.php?cropcategory=" + cropcategoryname;

Can setContentView be replaced by android-annotations when espresso is used?

I have espresso test which verifies that text is displayed:
public class InformationActivityTests {
#Rule
public ActivityTestRule<InformationActivity_> mInformationActivityTestRule =
new ActivityTestRule<InformationActivity_>(InformationActivity_.class) {
#Override
protected Intent getActivityIntent() {
Intent i = new Intent(getTargetContext(), InformationActivity_.class);
i.putExtra("INFORMATION", "Espresso");
return i;
}
};
#Test
public void textIsDisplayed() {
onView(withText("Espresso")).check(matches(isDisplayed()));
}
}
This test passes when Activity has following code:
#EActivity
public class InformationActivity extends AppCompatActivity {
#ViewById(R.id.information_view)
TextView informationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
Intent intent = getIntent();
String information = intent.getStringExtra("INFORMATION");
informationView.setText(information);
}
}
but fails when I "move" setContentView to #EActivity annotation:
#EActivity(R.layout.activity_information)
public class InformationActivity extends AppCompatActivity {
#ViewById(R.id.information_view)
TextView informationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String information = intent.getStringExtra("INFORMATION");
informationView.setText(information);
}
}
Error is:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edu/com.edu.InformationActivity_}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
Am I doing something wrong or is it an issue with espresso / android-annotations?
I've checked code generated by android-annotations when using #EActivity(R.layout.activity_information) and this is how onCreate looks like in generated class (InformationActivity_):
public void onCreate(Bundle savedInstanceState) {
OnViewChangedNotifier previousNotifier = OnViewChangedNotifier.replaceNotifier(onViewChangedNotifier_);
init_(savedInstanceState);
super.onCreate(savedInstanceState);
OnViewChangedNotifier.replaceNotifier(previousNotifier);
setContentView(R.layout.activity_information);
}
the problem is that it first calls super.onCreate (so onCreate from InformationActivity) where I handle intent and TextView and then it calls setContentView and this can't work like that.
The solution for this is what Be_Negative suggested, so using #AfterViews annotation. It also simplifies code a bit (I could remove onCreate method):
#EActivity(R.layout.activity_information)
public class InformationActivity extends AppCompatActivity {
#ViewById(R.id.information_view)
TextView informationView;
#AfterViews
void handleIntent() {
Intent intent = getIntent();
String information = intent.getStringExtra("INFORMATION");
informationView.setText(information);
}
}
Your problem is that you are never initializing informationview
public class InformationActivity extends AppCompatActivity {
#ViewById(R.id.information_view)
TextView informationView; //<-- Null
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String information = intent.getStringExtra("INFORMATION");
informationView.setText(information); //<--STILL NULL
}
}
So you first will have to initialize that view.

android if-statement doesn't work

public class MainActivity extends Activity
{
public static String EXTRA_MESSAGE;
private Intent ceec;
private EditText cc;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);}
public void sendMessage(View view)
{
ceec = new Intent(this, ToActivity.class);
cc = (EditText) findViewById(R.id.edit_message);
String message = cc.getText().toString();
ceec.putExtra(EXTRA_MESSAGE, message);
startActivity(ceec);
}}
And
public class ToActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setText(message);
textView.setTextColor(Color.rgb(5,8,100));
if( message == "hi"){
textView.setTextSize(80);
}
// Set the text view as the activity layout
setContentView(textView);
}}
[ if( message == "hi"){
textView.setTextSize(80);
} ]
It didn't work why? And how to fix it and thank you
Instead of
message == "hi"
You should do:
message.equal("hi")
Never compare Strings with ==, check out this question to understand why.
Use .equals() method as,
if(message.equals("hi")){
textView.setTextSize(80);
}
else
{
// do your else stuff
}
this should work.

Categories

Resources