ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) - android

I'm getting this error ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2585 when i start an intent
public class SingleMenuItemActivity extends Activity {
TextView lblName;
TextView lblyest;
TextView lbltoday;
TextView lblHigh;
TextView lblLow;
TextView lblChange;
TextView lblPcchange;
String name,yesterday,today,high,low,change,pcchange;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
lblName = (TextView) findViewById(R.id.name);
lblyest = (TextView) findViewById(R.id.yesterday);
lbltoday = (TextView) findViewById(R.id.today);
lblHigh = (TextView) findViewById(R.id.high);
lblLow = (TextView) findViewById(R.id.low);
lblChange = (TextView) findViewById(R.id.change);
lblPcchange = (TextView) findViewById(R.id.pcchange);
Intent in = getIntent();
name = in.getStringExtra("name");;
yesterday = in.getStringExtra("yesterday");
today =in.getStringExtra("today");
high =in.getStringExtra("high");
low = in.getStringExtra("low");
change = in.getStringExtra("change");
pcchange =in.getStringExtra("pcchange");
//Error doesnt happen when the lines below are commented
lblName.setText((CharSequence)name.toString());
lblyest.setText(yesterday.toString());
lbltoday.setText(today.toString());
lblHigh.setText(high.toString());
lblLow.setText(low.toString());
lblChange.setText(change.toString());
lblPcchange.setText(pcchange.toString());
}
}
I know the error is triggered when i do the settext() function but i don't know how to fix it

You're setting variables, lblName and all those, to contain all of the child View's ... but you're doing it at the class level. Those Views are not created until the layout is inflated, in the line that reads
setContentView(R.layout.single_list_item);
, so all those variables are getting set to null.
Set the variables after your setContentView() call and see how it goes.

Try this code.
public class SingleMenuItemActivity extends Activity {
TextView lblName;
TextView lblyest;
TextView lbltoday;
TextView lblHigh;
TextView lblLow;
TextView lblChange;
TextView lblPcchange;
String name, yesterday, today, high, low, change, pcchange;
#Override
protected void onCreate (Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate( savedInstanceState );
setContentView( R.layout.single_list_item );
Intent in = getIntent();
name = in.getStringExtra( "name" );
;
yesterday = in.getStringExtra( "yesterday" );
today = in.getStringExtra( "today" );
high = in.getStringExtra( "high" );
low = in.getStringExtra( "low" );
change = in.getStringExtra( "change" );
pcchange = in.getStringExtra( "pcchange" );
lblName = (TextView) findViewById( R.id.name );
lblyest = (TextView) findViewById( R.id.yesterday );
lbltoday = (TextView) findViewById( R.id.today );
lblHigh = (TextView) findViewById( R.id.high );
lblLow = (TextView) findViewById( R.id.low );
lblChange = (TextView) findViewById( R.id.change );
lblPcchange = (TextView) findViewById( R.id.pcchange );
lblName.setText( (CharSequence) name.toString() );
/* lblyest.setText(yesterday.toString());
lbltoday.setText(today.toString());
lblHigh.setText(high.toString());
lblLow.setText(low.toString());
lblChange.setText(change.toString());
lblPcchange.setText(pcchange.toString()); */
}
}
Actually, You can not initialize views before setContentView() call. That's why, you are getting this issue.

move these lines after setContentView(R.layout.single_list_item);
TextView lblName = (TextView) findViewById(R.id.name);
TextView lblyest = (TextView) findViewById(R.id.yesterday);
TextView lbltoday = (TextView) findViewById(R.id.today);
TextView lblHigh = (TextView) findViewById(R.id.high);
TextView lblLow = (TextView) findViewById(R.id.low);
TextView lblChange = (TextView) findViewById(R.id.change);
TextView lblPcchange = (TextView) findViewById(R.id.pcchange);

Related

App crashed when using setText method

Problem: My app crashed whenever the comment part is removed. But after I had tried to debug the problem seemed to be at line priceTextViews[i].setText(price[i]). Can anyone help to point out the problem? Thank you
TextView[] quantityTextViews;
TextView[] priceTextViews;
int[] unitPrice = {5, 15, 40, 20, 25, 15, 20, 25, 10, 20};
int[] price = new int[10];
int[] quantity = new int[10];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.balance_page);
textView();
Intent intent = getIntent();
String[] quantityOfItemString = intent.getStringArrayExtra (ShoppingPageActivity.EXTRA_MESSAGE);
for (int i = 0; i < quantityTextViews.length; i++) {
quantityTextViews[i].setText(quantityOfItemString[i]);
quantity[i] = Integer.parseInt(quantityOfItemString[i]);
price[i]= unitPrice[i]* quantity[i];
}
/*for (int i = 0; i < priceTextViews.length; i++) {
price[i]= unitPrice[i]* quantity[i];
priceTextViews[i].setText(price[i]);
}*/
}
public void textView() {
quantityTextViews = new TextView[]{
(TextView) findViewById(R.id.textView49),
(TextView) findViewById(R.id.textView48),
(TextView) findViewById(R.id.textView47),
(TextView) findViewById(R.id.textView46),
(TextView) findViewById(R.id.textView45),
(TextView) findViewById(R.id.textView44),
(TextView) findViewById(R.id.textView43),
(TextView) findViewById(R.id.textView42),
(TextView) findViewById(R.id.textView41),
(TextView) findViewById(R.id.textView40),
};
priceTextViews = new TextView[]{
(TextView) findViewById(R.id.textView51),
(TextView) findViewById(R.id.textView52),
(TextView) findViewById(R.id.textView53),
(TextView) findViewById(R.id.textView54),
(TextView) findViewById(R.id.textView55),
(TextView) findViewById(R.id.textView56),
(TextView) findViewById(R.id.textView57),
(TextView) findViewById(R.id.textView58),
(TextView) findViewById(R.id.textView59),
(TextView) findViewById(R.id.textView50),
};
}
setText requires a string. The setText method that does accept an int needs to correspond to a text resource within your application.
priceTextViews[i].setText(Integer.toString(price[i]));
When you're using setText with int argument it automatically use setText(#StringRes int resid) instead of void setText(CharSequence text), so cast your argument to String or similar CharSequence type.
Easiest solution is
priceTextViews[i].setText("" + price[i]);

Android Studio TextView and sharedpreferences

in one activity I save data into the sharedpreferences via:
public void createIt() {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
Context context = getApplicationContext();
EditText editTextAge = (EditText) findViewById(R.id.editTextAge);
String s = editTextAge.getText().toString();
age = Integer.valueOf(s);
if (age < 16) {
setWeightValues();
setRepValues();
switch (workoutPref) {
case "fitness":
//mat exercises
editor.putString("sprints", "15" );
editor.putString("jogs", "10");
editor.putString("walks", "10" );
break;
}
}
editor.commit();
launchNextactivity();
}
--
and in this new activity, I display 3 specific items from the memory and display them in a textview with this code inside onCreate()
public class WorkoutActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout);
//pagetabview 3 days 3 workouts
TextView txt1 = (TextView) findViewById(R.id.txt1);
String t1 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("Star Jumps", "nothing");
txt1.setText(t1);
TextView txt2 = (TextView) findViewById(R.id.txt1);
String t2 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("Push Ups", "nothing");
txt1.setText(t2);
TextView txt3 = (TextView) findViewById(R.id.txt1);
String t3 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("Sit Ups", "nothing");
txt1.setText(t3);
TextView txt4 = (TextView) findViewById(R.id.txt1);
String t4 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("Plank", "nothing");
txt1.setText(t4);
TextView txt5 = (TextView) findViewById(R.id.txt1);
String t5 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("Rowing", "nothing");
txt1.setText(t5);
TextView txt6 = (TextView) findViewById(R.id.txt1);
String t6 = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("Treadmill", "nothing");
txt1.setText(t6);
}
for some reason, it only puts "nothing" in the first textview, and then doesn't even change the other text views they stay as default. please can someone help, im not sure if its my sharedpreference or my way of setting textview
The key you have used for set and get values from shared preference should be the same.
You have not change textview value when finding textviews.
TextView txt1 = (TextView) findViewById(R.id.txt1);
TextView txt2 = (TextView) findViewById(R.id.txt2);
TextView txt3 = (TextView) findViewById(R.id.txt3);
TextView txt4 = (TextView) findViewById(R.id.txt4);
TextView txt5 = (TextView) findViewById(R.id.txt5);
TextView txt6 = (TextView) findViewById(R.id.txt6);
base from your code on putting data, this is how you get the data
getDefaultSharedPreferences(getApplicationContext()).getString("sprints", "nothing");

Null Pointer Exception in Receiving serializable object

I am trying to populate values into list view. It is working fine. And on clicking list row its description activity is going to open. I am getting null pointer exception on populating values.
ListView mylistview = null;
mylistview = (ListView) findViewById(R.id.list);
CustomAdapter listAdapter = new CustomAdapter(SearchActivityResult.this, 0, temp);
mylistview.setAdapter(listAdapter);
list=temp;
mylistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Intent i = new Intent(SearchActivity.this, DescriptionActivity.class);
Bundle b = new Bundle();
b.putSerializable("plant", list.get(pos));
i.putExtras(b);
startActivity(i);
Then this serializable object is received as following:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
plant = (Plant) b.getSerializable("plant");
setContentView(R.layout.description_activity);
setTitle("Flora Enroute");
View titleView = getWindow().findViewById(android.R.id.title);
if (titleView != null) {
ViewParent parent = titleView.getParent();
if (parent != null && (parent instanceof View)) {
View parentView = (View)parent;
parentView.setBackgroundColor(Color.parseColor("#99CC00"));
}
}
initViews();
populateValues();
}
private void populateValues() {
Picasso.with(this).load(plant.getimagepath()).error(R.drawable.plant_icon).resize(240,320).into(ivImage);
commonname.setText(plant.getcommonname());
scientificname.setText(plant.getscientificname());
planttype.setText(plant.getplanttype());
season.setText(plant.getseason());
growthhabit.setText(plant.getgrowthhabit());
duration.setText(plant.getduration());
leafshape.setText(plant.getleafshape());
flowershape.setText(plant.getflowershape());
flowercolor.setText(plant.getflowercolor());
lightexposure.setText(plant.getlightexposure());
growthrate.setText(plant.getgrowthrate());
waterreq.setText(plant.getwaterrequirement());
sid.setText(plant.getsid());
features.setText(plant.getfeatures());
}
private void initViews() {
commonname = (TextView) findViewById(R.id.commonname);
duration = (TextView) findViewById(R.id.duration);
features = (TextView) findViewById(R.id.features);
flowercolor = (TextView) findViewById(R.id.flowercolor);
flowershape = (TextView) findViewById(R.id.flowershape);
growthhabit = (TextView) findViewById(R.id.growthhabit);
growthrate = (TextView) findViewById(R.id.growthrate);
leafshape = (TextView) findViewById(R.id.leafshape);
lightexposure = (TextView) findViewById(R.id.lightexposure);
planttype = (TextView) findViewById(R.id.planttype);
scientificname = (TextView) findViewById(R.id.scientificname);
season = (TextView) findViewById(R.id.season);
sid = (TextView) findViewById(R.id.sid);
showMap = (TextView) findViewById(R.id.show_map);
ivImage = (ImageView) findViewById(R.id.iv_);
}
I am getting null pointer exception on poulate values function. When I click any list row to start its description activity I got null pointer exception.
In populateValues(), you are setting texts for various textviews.
All other textviews are initialized in initViews(). But you have missed initializing waterreq
So you may be getting NPE on line:
waterreq.setText(plant.getwaterrequirement());
Please initialize it in initViews() function.
Hope this helps.

gettag() is getting only last index value of an array

I am getting values from web services and filling it textviews and I am also setting tags on some values because I have to send those values on other activity on a button click. but I am only getting the last value of an array.
txt_TransStatus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView cr = (TextView) child.findViewById(R.id.txt_StatusValue);
TextView pid = (TextView) child.findViewById(R.id.txt_DateValue);
TextView amount = (TextView) child.findViewById(R.id.txt_RechargeAmountValue);
TextView phone = (TextView) child.findViewById(R.id.txt_PhoneValue);
TextView pin = (TextView) child.findViewById(R.id.txt_PinNumberValue);
Intent intent = new Intent(con, GetTransData.class);
intent.putExtra("CRID", cr.getTag().toString());
intent.putExtra("PID", pid.getTag().toString());
intent.putExtra("Amount", amount.getTag().toString());
intent.putExtra("Phone", phone.getTag().toString());
intent.putExtra("PINNum", pin.getTag().toString());
con.startActivity(intent);
}
});
I fixed my code with this:
OnClick method, changed my code to
ImageView iv = (ImageView)v;
ProductURL = (String) iv.getTag();

Custom Title bar in Android

I have create a custom title bar, It contains a TextView and label. i have added this to an activity as follows
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.homepage);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.windowtitle);
But now i want to display current time in the textview inside the titlebar.
How can this be done?
you can get text view like this.create a mytitle.xml layout with textview and then add this code it will work..
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText(" TITLE ");
}
}
Please try below code
LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.windowtitle, null, true);
TextView textView = (TextView) view.findViewById(R.id.txtdate);
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
txtdate.setText(c.getTime());

Categories

Resources