Null pointer in putExtras - android

I have some trouble with putExtras to an intent. Could you please review my code?
public void onSelectCategory(View v) {
int category = Integer.parseInt((String) v.getTag());
Intent intent = new Intent(HomeActivity.this, ListActivity.class);
intent.putExtra("EXT_CATEGORY", category);
startActivity(intent);
}
And in the ListActivity, I'm doing the following..
public static final String EXT_CATEGORY = "category";
int category = getIntent().getExtras().getInt(EXT_CATEGORY);

From this line
intent.putExtra("EXT_CATEGORY", category);
in your another activity String name should be same means:--
public static final String EXT_CATEGORY = "EXT_CATEGORY";

You did several mistakes, here is a overworker version of your code.
public void onSelectCategory(View v) {
int category = Integer.parseInt((String) v.getTag());
Intent intent = new Intent(HomeActivity.this, ListActivity.class);
intent.putExtra(EXT_CATEGORY, category);
startActivity(intent);
}
int defaultCat = -1;
public static final String EXT_CATEGORY = "category";
int category = getIntent().getIntExtra(EXT_CATEGORY,defaultCat); // Use default int if there is no extra

You should use this to get you info, not getExtras() :
int category = getIntent().getIntExtra("EXT_CATEGORY");
getExtras() returns additional Bundle of data. You need only one integer.

Related

Android serializable putextra

I'm new on Android and I want to know how I can use Serializable in my code
First Activity:
#Override
public void onItemClick(Neighbour item) {
Intent i = new Intent(getActivity(), ProfilNeighbourActivity.class);
i.putExtra("avatar", item.getAvatarUrl());
i.putExtra("name", item.getName());
i.putExtra("city", item.getAddress());
i.putExtra("phone", item.getPhoneNumber());
i.putExtra("about", item.getAboutMe());
i.putExtra("fbUrl", item.getFbUrl());
startActivity(i);
}
Second Activity:
mAvatar = findViewById(R.id.profil_neighbour_image);
mName1 = findViewById(R.id.profil_neighbour_name1);
mName2 = findViewById(R.id.profil_neighbour_name2);
mCity = findViewById(R.id.profil_neighbour_city);
mPhone = findViewById(R.id.profil_neighbour_phone);
mAbout = findViewById(R.id.profil_neighbour_about);
mFbUrl = findViewById(R.id.profil_neighbour_fbUrl);
Intent i = getIntent();
String image = i.getStringExtra("avatar");
String name = i.getStringExtra("name");
String city = i.getStringExtra("city");
String phone = i.getStringExtra("phone");
String about = i.getStringExtra("about");
String fbUrl = i.getStringExtra("fbUrl");
Glide.with(this).asBitmap().load(image).fitCenter().into(mAvatar);
mName1.setText(name);
mName2.setText(name);
mCity.setText(city);
mPhone.setText(phone);
mFbUrl.setText(fbUrl + name);
mAbout.setText(about);
How can I shorten my code with Serializable please?
you can use passing objects around then Parcelable
more details : Parcelable

Intent not sending or receiving integer value

public void Show(int n){
Intent in= new Intent(this, Results.class);
in.putExtra("Percent", n); // n is inetger
startActivity(in);`enter code here`
}
Intent intent= getIntent();
// int num = Integer.parseInt(String.valueOf(intent.getStringExtra("Percent")));
String num = intent.getStringExtra("Percent");
int num2= Integer.parseInt(num);
//ch.setText(num);
//String num= in.getStringExtra("Percent");
//int n=
//b.setProgress(num);
t.setText(num2);
You can recieve integer value like this:
Intent intent= getIntent();
int num = intent.getIntExtra("Percent");
You need to use int num intent.getInt("Percent");

this is giving error.plz suggest some answer

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DataModel dataModel = (DataModel) parent.getItemAtPosition(position);
// Log.d("imageId--- ", String.valueOf(dataModel.getImageid()));
//
// Intent intent = new Intent(MainActivity.this, DetailActivity.class);
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_DRAWABLE_ID, dataModel.getImageid());
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_NAME, dataModel.getName());
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_COST, dataModel.getPrice());
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_NAME_DETAIL, dataModel.getDetailName());
// intent.putExtra(IntentKeyConstant.INTENT_KEY_ITEM_DETAIL, dataModel.getDetail());
Intent intent1 = new Intent(MainActivity.this, DetailActivity.class);
intent1.putExtra("keyclassobject",dataModel);
startActivity(intent1);
}
});
another activity is
final DataModel dataModel = (DataModel) intent.getSerializableExtra("keyclass");
// drawableId = intent.getIntExtra(IntentKeyConstant.INTENT_KEY_ITEM_DRAWABLE_ID, 0);
// String itemName = intent.getStringExtra(IntentKeyConstant.INTENT_KEY_ITEM_NAME);
// String itemCost = intent.getStringExtra(IntentKeyConstant.INTENT_KEY_ITEM_COST);
// String itemNameDetail = intent.getStringExtra(IntentKeyConstant.INTENT_KEY_ITEM_NAME_DETAIL);
// String itemDetail = intent.getStringExtra(IntentKeyConstant.INTENT_KEY_ITEM_DETAIL);
//Log.d("name", itemName);
// Log.d("itemcost",itemCost);
//Log.d("imageId ",String.valueOf(drawableId)+ "-"+Integer.parseInt(drawableId));
Typeface typeface=Typeface.createFromAsset(getAssets(),"fonts/fontawesome-webfont.ttf");
tv_item_rupee_symbol=(TextView)findViewById(R.id.tv_item_rupee_symbol);
tv_item_rupee_symbol.setTypeface(typeface);
toolbar_title.setText(dataModel.getName());
iv_item.setImageResource(dataModel.getImageid());
tv_item_name_detail.setText(dataModel.getDetailName());
tv_item_rupee_symbol.setText("\uf156");
tv_item_cost.setText(dataModel.getPrice());
tv_item_detail.setText(dataModel.getDetail());
Intent intent = getIntent();
If your extra data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:
String id = intent.getStringExtra("keyclassobject");

how sent integer parameter in intent

I want to send int parameter in intent like this:
String textname = (String) dataItem.get("name");
Intent m = new Intent(list.this,main.class);
m.putExtra("name",textname);
m.putExtra("page",1);
startActivity(m);
and in main class I get that parameter
Intent intent = getIntent();
name = intent.getStringExtra("name");
page1 = Integer.parseInt(intent.getStringExtra("page"));
but when I run my code it force closes!!!
You should use getIntent().getIntExtra(name, defaultValue) instead of Integer.parseInt(intent.getStringExtra("page"));
Update:
int defaultValue = -1;// take any default value of your choice
String name = intent.getStringExtra("name");
int page1 = intent.getIntExtra("page", defaultValue);
I in other class use the main class and send parameter to it and it work without any problem but just in list class i have problem
in other class i like this send parameter
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(list_sub.this,main.class);
i.putExtra("name", Name[position]);
i.putExtra("page", Ted[position]);
startActivity(i);
}
and in refresh class i get ted parameter
private void refresh(){
db.open();
int s = db.List_count("text", ID);
Name= new String[s];
Ted=new String[s];
for(int i=0;i<s;i++){
Name[i]=db.List_display_sub("text", ID, i);
Ted[i]=db.page_count("text", Name[i].toString())+"";
}
db.close();
}
Activity A
String textname = (String) dataItem.get("name");
Intent m = new Intent(list.this,main.class);
m.putExtra("name",textname);
m.putExtra("page",1);
startActivity(m);
Activity B
Intent intent = getIntent();
name = intent.getStringExtra("name");
int page = intent.getIntExtra("page", 0);
where 0 is the default value.

Bundle.putInt() , how does it work?

I am still learning android and I in a tutorial
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),com.example.addressbook.DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
My problem is here I didn't understand how this works
dataBundle.putInt("id", id_To_Search);
if id_To_Search = 0 then id = 0 ? and id refer to the column name ?
android site explains
putInt(String key, int value)
Inserts an int value into the mapping of this Bundle, replacing any existing value for the given key.
as far I understand, if int = 5 then key will be 5 ?
then 'Id' it will be = 5 ?
1) You can share int with other activity like this :
String YOUR_KEY = "id";
Intent intent = new Intent(getApplicationContext(), DisplayContact.class);
intent.putExtra(YOUR_KEY, id);
startActivity(intent);
or
Bundle dataBundle = new Bundle();
dataBundle.putInt(YOUR_KEY, id);
intent.putExtras(dataBundle);
2) And get int in your activity like that :
int defaultValue = 0;
int id = getIntent().getIntExtra(YOUR_KEY, defaultValue);
or
Bundle extras = getIntent().getExtras();
int id = extras.getInt(YOUR_KEY);
That's exaclty what you said
as far I understand, if int = 5 then key will be 5 ? then 'Id' it will be = 5 ?
So ,when you enter in another activity, here
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),com.example.addressbook.DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
You can do int id = getIntent().getIntExtra("id"); , and will be 5

Categories

Resources