startActivity from BroadcastReceiver Button Views on Activity don't show up - android

I start my activity alarmAlert.class from onReceive().
The activity was started .
There three buttons and two TextViews on my activity.
when the activity started ,Textviews always show up,
but the buttons not always show up.
I start the activity from my MainActivity ,all the views work
perfect
Anything I can do .
thanks
I got something.
If my App is not running in background (click menu button,remove the app from list)
the activity started from onReceive() , buttons will not show up.
if the App still run in background ,everything is perfect .
my onReceive code:
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int id = intent.getIntExtra("id", -1);
try{
Intent alertIntent=new Intent();
alertIntent.setClass(context, AlarmAlert.class);
Bundle bundle=new Bundle();
bundle.putInt("id", id);
alertIntent.putExtras(bundle);
alertIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
context.startActivity(alertIntent);
}
catch (Exception e){
AlarmGloble.writeLog(context,"AlarmReceiver:"+e.toString());
}
part of my AlarmAlert code
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d("AA","onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_alert);
AlarmGloble.dbHelper=new DBHelper(this,AlarmGloble.DB_NAME,AlarmGloble.DB_VERSION);
AlarmGloble.dbLib=new DataBaseLib(AlarmGloble.dbHelper);
settingFromId();
findViews();
setListener();
updateView();
mVibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
setSystemVolume(0);
//play mp3 by Handler
mHandler.sendMessage(mHandler.obtainMessage(PLAY));
}
private void updateView(){
TextView textViewSnooze=(TextView)findViewById(R.id.alarm_alertTextViewSnooze);
if (mp3Test){
textViewSnooze.setVisibility(View.INVISIBLE);
spinnerSnooze.setVisibility(View.INVISIBLE);
buttonClose.setVisibility(View.INVISIBLE);
buttonEdit.setVisibility(View.INVISIBLE);
}
else {
textViewSnooze.setVisibility(View.VISIBLE);
spinnerSnooze.setVisibility(View.VISIBLE);
buttonClose.setVisibility(View.VISIBLE);
buttonEdit.setVisibility(View.VISIBLE);
if(alarmRept<=0){
textViewSnooze.setVisibility(View.VISIBLE);
spinnerSnooze.setVisibility(View.VISIBLE);
}
else{
textViewSnooze.setVisibility(View.INVISIBLE);
spinnerSnooze.setVisibility(View.INVISIBLE);
if (alarmReptCount<alarmReptTimes)
if (alarmKind==0)
mMessage=mMessage+"\n"+"next alarm :"+mClockSnooze[alarmRept]+" ";
else
mMessage=mMessage+"\n"+"next alarm:"+AlarmGloble.rept[alarmRept]+" ";
}
}
textViewMessage.setText(mMessage);
if(mMp3.equals(AlarmGloble.RANDOM)){
File file=new File(AlarmGloble.ROOT_MUSIC);
RandomMp3 rm=new RandomMp3(file);
if (rm.getCount()>0)
mMp3=rm.getFile();
else
mMp3="";
}
else {
if(mMp3.equals(AlarmGloble.DEFAULT)){
mMp3=getDefaultMP3();
}
else{
String[] s=mMp3.split("/");
textViewSongName.setText(s[s.length-1]);
}
}
if (mMp3.equals("")){
textViewSongName.setText(mMp3);
}
else {
String[] s=mMp3.split("/");
textViewSongName.setText(s[s.length-1]);
}
(LinearLayout)findViewById(R.id.alarm_alertLinearLayoutButtons);
linearLayoutButtons.requestFocus();
}
here is alarm_alert.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/linearLayoutAlarmAlert" >
<LinearLayout
android:id="#+id/alarm_alertLinearLayoutButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/alarm_alertButtonClose"
style="#style/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnClose" />
<Button
android:id="#+id/alarm_alertButtonEdit"
style="#style/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnEdit"
android:visibility="visible" />
<Button
android:id="#+id/alarm_alertButtonDismiss"
style="#style/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnExit"
android:visibility="visible" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/alarm_alertTextViewSnooze"
style="#style/RedLeft17sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/snooze" />
<Spinner
android:id="#+id/alarm_alertSpinnerSnooze"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="#+id/alarm_alertTextViewMessage"
style="#style/YellowLeft17sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/btnClose" />
<TextView
android:id="#+id/alarm_alertTextViewSongName"
style="#style/YellowLeft17sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:text="#string/btnClose" />
</LinearLayout>

I can only see 2 buttons buttonClose and buttonEdit in the AlarmAlert Activity, so i'm assuming the 3 buttons that aren't showing are in alarm_alertLinearLayoutButtons.xml which should be reused in alarm_alert.xml as such
<include layout="#layout/alarm_alertLinearLayoutButtons"/>
Ref: http://developer.android.com/training/improving-layouts/reusing-layouts.html
I can't see anything wrong with this except for the last couple of lines.
linearLayoutButtons isn't set.
linearLayoutButtons = (LinearLayout)findViewById(R.id.alarm_alertLinearLayoutButtons);
linearLayoutButtons.requestFocus();

Related

start Activity B(called by onCreate of an Application Class's sub Class) on Top of Activity A(called by onResume of an Activity Class's sub Class)

I have 2 activities that involved in this question:
LoginActivity: A Login in Screen (called by a subclass of Activity Class)
Updater Activity: A Update Screen that show updating progress when new app version is availble (called by a subclass of Application Class)
This is what I want:
When the app FIRST LAUNCH, and if new version of the app is available, the UPDATER ACTIVITY should be on top of every activity include the Login Activity,
so it will be the first screen user see during first launch of the app.
only after the updating progress is done, the UPDATER ACTIVITY will be dismissed then Login Screen be on top.
However this is what actually happened:
The LOGIN ACTIVITY always on top of UPDATER ACTIVITY...
My Guess is that since the UPDATER ACTIVITY is called By APPLICATION CLASS's subClass, and the LOGINACTIVITY is called by an ACTIVITY CLASS sub class,
the Application class's method such as OnResume always get called before Activity onCreate Method, thus the LOGINACTIVITY always appear on top of UPDATER ACTIVITY,
I just wondering if there is anyway I could make my UPDATER ACTIVITY be on top of any activities including the LOGIN ACTIVITY?
//LoginActivity involved
public abstract ClassA extends Activity {
.....
.....
protected void onResume() {
super.onResume();
.....
if(isAuthenticated){
Intent intent = new Intent(this, LoginActivity.class)
.putExtra(...)
startActivityForResult(intent, XXX);
}
}
}
====
//Updater Activity involved
public abstract class ClassB extends Application {
....
....
....
public void onCreate() {
super.onCreate();
Intent updaterIntent = new Intent(this, UpdaterActivity.class);
updaterIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(updaterIntent);
}
}
i have diffrent solution instent of activity create dialog box activity.
public class ViewDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.updatedialog);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogUpdate = (Button) dialog.findViewById(R.id.update);
Button dialogcancel = (Button) dialog.findViewById(R.id.cancel);
Button dialogremindme = (Button) dialog.findViewById(R.id.remindeme);
TextView textView= (TextView) dialog.findViewById(R.id.machhint1);
TextView textView2= (TextView) dialog.findViewById(R.id.machhint2);
textView2.setText("Your old version is : "+versionName);
textView.setText("Current version in playstore : "+version);
dialogcancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialogUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
SharedPreferences.Editor editoradddetail1 = getSharedPreferences("later", MODE_PRIVATE).edit();
editoradddetail1.putString("days", "");
editoradddetail1.commit();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
later="";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
dialog.dismiss();
}
});
dialogremindme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editoradddetail1 = getSharedPreferences("later", MODE_PRIVATE).edit();
Date someDate = new Date(); // Or whatever
Date dayAfter = new Date(someDate.getTime() + TimeUnit.DAYS.toMillis( 2 ));
DateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");
String formattedDate=dateFormat.format(dayAfter);
editoradddetail1.putString("days", formattedDate);
editoradddetail1.commit();
dialog.dismiss();
}
});
dialog.show();
}
}
//and xml is.
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="250dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="250dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/main"
android:background="#FFed6904"
android:layout_height="120dp">
<TextView
android:id="#+id/machhint"
android:text="Note:- Your current Version Did Not Match"
android:layout_width="match_parent"
android:textColor="#color/white"
android:textSize="13dp"
android:layout_height="60dp" />
<TextView
android:id="#+id/machhint1"
android:text="preschool version is"
android:layout_width="wrap_content"
android:textSize="13dp"
android:textColor="#color/white"
android:layout_below="#+id/machhint"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/machhint2"
android:text="Your Vertion is :"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:textSize="13dp"
android:layout_below="#+id/machhint1"
android:layout_height="30dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/text1"
android:layout_below="#+id/main"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXTO"
android:id="#+id/text_dialog"
android:layout_marginTop="3dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="3dp"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_below="#+id/text1"
android:gravity="center_vertical|center_horizontal"
android:layout_height="55dp">
<Button
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="Update"
android:id="#+id/update"
android:gravity="center"
android:layout_toLeftOf="#+id/remindeme"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="Later"
android:id="#+id/remindeme"
android:gravity="center"
android:layout_centerHorizontal="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="Cancel"
android:id="#+id/cancel"
android:gravity="center"
android:layout_toRightOf="#+id/remindeme"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>

android - Holo button looks different between versions

I have an android app with min sdk version 14.
in my values-v14 folder, in styles.xml I have this:
<style name="MyButtonStyle" parent="android:Widget.Holo.Button"></style>
I apply this style to two buttons in one of my activities (the activity looks like a big dialog).
I run the app on:
OnePlus phone (version 4.4.4 - api 19)
GT-I8190 (version 4.1.2 - api 16)
For some reason the buttons look different in color, which is weird because the Holo Theme should be available in API 14 and above (and both phones are above 14).
Small difference but its there (in the OnePlus the buttons are darker and in the GT they are lighter). Why is it like that? shouldn't it be the same?
OnePlus Image:
GT-I8190 Image:
----------------- EDIT - Adding Code --------------------
Here is the xml file with the buttons (not_allowed_dialog.xml).
As you can see, there isn't any drawables in the button or anywhere else.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
android:orientation="vertical" >
<TextView
android:id="#+id/not_logged_in_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Please log in to add a ride"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="25sp" />
<LinearLayout
android:id="#+id/not"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/not_logged_in_text_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:orientation="horizontal"
android:paddingBottom="1.0dip"
android:paddingLeft="4.0dip"
android:paddingRight="4.0dip"
android:paddingTop="5.0dip" >
<Button
android:id="#+id/not_allowed_login"
style="#style/MyButtonStyle"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="Login Now!" />
<Button
android:id="#+id/not_allowed_register"
style="#style/MyButtonStyle"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="Register Now!" />
</LinearLayout>
</RelativeLayout>
Also - Activity code:
public class DialogActivity extends Activity {
private Button toRegisterBtn, toLoginBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.not_allowed_dialog);
setFinishOnTouchOutside(false);
toRegisterBtn = (Button) findViewById(R.id.not_allowed_register);
toLoginBtn = (Button) findViewById(R.id.not_allowed_login);
toRegisterBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
setResult(RESULT_OK, null);
finish();
}
});
toLoginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(i);
setResult(RESULT_OK, null);
finish();
}
});
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
}
}

Creating object using a form, adding it to a list on another activity

I want to retrieve a value from a text of a radiobutton inside a radiogroup. This sounds really simple but I am not being able to put it to work as it always gives me a null value.
Here is my code:
public class NewMovie extends FragmentActivity {
RadioGroup rating;
RadioButton selectedRating;
int selectedRatingIndex;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_movie_layout);
rating = (RadioGroup) findViewById(R.id.rating);
onClickListenerButton();
}
public void onClickListenerButton()
{
selectedRatingIndex = rating.getCheckedRadioButtonId();
final RadioButton selectedRating = (RadioButton) findViewById(selectedRatingIndex);
intent.putExtra("rating", selectedRating.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
}
XML:
<RadioButton
android:id="#+id/U"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/U" />
<RadioButton
android:id="#+id/PG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/PG" />
<RadioButton
android:id="#+id/r12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/r12" />
<RadioButton
android:id="#+id/r15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/r15" />
<RadioButton
android:id="#+id/r18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/r18" />
</RadioGroup>
Always nullpointerexception for some reason.
Don't know if it helps the context, but I am trying to go from activity 1 to activity 2, in activity 2 (the one I show above) display the radiogroup and the user then submits one of the answers and the answer is then used in activity 1.
You have to put a default select in radio buttons, Try as follow,
Place all your radio buttons with in radio group in xml
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/U"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/U"
android:checked="true" /> // deafult select
//other radio buttons
</RadioGroup>
Initiate all radio buttons in activity
radioU=(RadioButton) findViewById(R.id.U);
Check the selection in button click event
public void onClickListenerButton(View v) {
if(radioU.isChecked())
{
intent.putExtra("rating", getResources().getString(R.string.U));
}
else if(radioPG.isChecked())
{
intent.putExtra("rating", getResources().getString(R.string.PG));
}
setResult(RESULT_OK, intent);
finish();
}

Android toggle button display wrong value after orientation change

Here is my problem.
I've got a simple activity which set a layout, and add rows in a table-layout(itself in a scroll view).
Those table-rows have a custom layout with a text-field and a toggle button.
Each toggle button has a value taken from a database, and when I first create the activity, the values are OK. But when I turn the device and then change the orientation, all the toggles-button take "false" value. I printed the values that I set in the Logcat, and the values are the good ones (those in the database).
I thought something like the layout I want is hidden behind another layout, but I made some tests and the text-fields change with new values, so I really don't understand why the toggle buttons don't work.
Here is the code :
TableRow layout :
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="#+id/relativelayout_row_parametres"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="1.0">
<TextView
android:id="#+id/textview_row_parametres"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginLeft="2dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
/>
<ToggleButton
android:id="#+id/togglebutton_row_parametres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="2dp"
android:textOn="#string/togglebutton_on"
android:textOff="#string/togglebutton_off" />
</RelativeLayout>
</TableRow>
Activity Layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView style="#style/header" />
<LinearLayout
android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="0.8"
android:background="#drawable/gradient_bg"
android:padding="10dip"
android:orientation="vertical">
<ScrollView
android:layout_width="fill_parent" android:layout_height="0dp"
android:layout_weight="0.85"
android:fillViewport="true"
android:padding="10dip"
android:layout_gravity="center">
<TableLayout
android:id="#+id/tablelayout_parametres"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/cornered_bg"
android:paddingTop="5dip"
android:paddingBottom="5dip">
</TableLayout>
</ScrollView>
<Button
android:id="#+id/button_parametres_accept"
android:gravity="center"
android:text="#string/accept_changes"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.15"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
And the activity code:
public class Parameters extends Activity {
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Map<String, Boolean> changes = new HashMap<String, Boolean>();
final Context ctx = getApplicationContext();
LanguageManager.updateConfig(this);
setContentView(R.layout.parametres);
CountryDB[] countries = Database.instance(getApplicationContext()).getCountries();
TableLayout tabLayout = (TableLayout) findViewById(R.id.tablelayout_parametres);
for(int i =0; i<countries.length; i++){
TableRow newRow = (TableRow) getLayoutInflater().inflate(R.layout.row_parametres, null);
TextView textView = (TextView) newRow.findViewById(R.id.textview_row_parametres);
ToggleButton toggleButton = (ToggleButton) newRow.findViewById(R.id.togglebutton_row_parametres);
toggleButton.setChecked(countries[i].isToSynchronize());
toggleButton.setTag(countries[i]);
Log.e("setChecked",""+toggleButton.getId()+"/"+countries[i].isToSynchronize());
textView.setText(countries[i].getLabel());
toggleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CountryDB countryTemp = (CountryDB) v.getTag();
changes.put(countryTemp.getLabel(), ((ToggleButton)v).isChecked());
}
});
tabLayout.addView(newRow);
TableRow rowDivider = (TableRow) getLayoutInflater().inflate(R.layout.row_divider, null);
tabLayout.addView(rowDivider);
}
Button buttonValidation = (Button) findViewById(R.id.button_parameters_accept);
buttonValidation.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Iterator<String> iterator = changes.keySet().iterator();
while(iterator.hasNext()){
String stringTemp = iterator.next();
Database.instance(ctx).updateCountry(stringTemp, changes.get(stringTemp));
}
Intent intent = new Intent(ctx, Splash.class);
String result = "restart";
String from = "parameters";
Intent returnIntent = new Intent();
returnIntent.putExtra("result", result);
returnIntent.putExtra("from", from);
setResult(RESULT_OK, returnIntent);
startActivity(intent);
}
});
}
}
In the Log.e, I print the values, and they are good, the display on togglebuttons is wrong, they are just all "false".
Thanks for your help.
Between onCreate() and onResume() , Android tries to restore the old state of the toggle Buttons. Since they don't have unique ID's , Android wont succeed and everything is false again. Try to move your setChecked() calls into onResume() ( maybe onStart() works too).
Here is a pretty good answer to the same Question:
ToggleButton change state on orientation changed
you have to save data before Orientation.
Android have method onSaveImstamceState(Bundle outState) and onRestoreInstanceState(BundleInstaceState)
override these method in Activity.
There's a simpler solution: you only need to add configChanges property to your activity declaration, like stated here. This way you can prevent Activity restart when orientation changes. So in your manifest you should have something like
<activity android:name=".Parameters"
android:configChanges="orientation|keyboardHidden">
or if your buildTarget>=13
<activity android:name=".Parameters"
android:configChanges="orientation|keyboardHidden|screenSize">
Edit: Like reported on comments below, this is not an optimal solution. The main drawback is reported in a note of the document linked above:
Note: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.

button onclicklistener error

i'm getting this error when i try to run my app in the emulator (any android version): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.test/com.android.test.HomeScreen}: java.lang.NullPointerException
my question is: is this because in 2.2 there is no "golf button" in the layout, and in 3.0 there is?
this is my activity:
public class HomeScreen extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
Button basketball = (Button)findViewById(R.id.basketball);
basketball.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent basketball = new Intent(HomeScreen.this, basketball.class);
startActivity(basketball);
}
});
Button golf = (Button)findViewById(R.id.golf);
golf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent golf = new Intent(HomeScreen.this, golf.class);
startActivity(golf);
}
});
}
}
i got 2 layouts
layout 1: layout-normal-mdpi <= running in 2.2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/homescreen">
<Button
android:id="#+id/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/basketbal"
android:layout_marginTop="46dp"
android:layout_marginRight="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true">
</Button>
</RelativeLayout>
layout 2: xlarge-port <= running in 3.0
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/homescreen">
<Button
android:id="#+id/basketball"
android:background="#drawable/basketbal"
android:layout_height="225dp"
android:layout_width="225dp"
android:layout_marginRight="70dp"
android:layout_marginTop="90dp"
android:layout_alignParentRight="true">
</Button>
<Button
android:id="#+id/golf"
android:background="#drawable/golf"
android:layout_height="550dp"
android:layout_width="211dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="390dp"
android:layout_alignParentLeft="true">
</Button>
</RelativeLayout>
if you are getting error in the below line,
Button golf = (Button)findViewById(R.id.golf);
which is not actually present in the first layout you have provided here, it is no wonder. This definitely doesn't exist and which obviously returns null to you and there you go, getting null pointer exception.
To avoid this either you have to catch the exception else you will have change the layout accordingly such that it contains both the buttons in both the layouts.
You're lacking golf button in your first layout, as you stated.
So, when you run
Button golf = (Button)findViewById(R.id.golf);
golf.setOnClickListener(new View.OnClickListener() {
golf is set to null (first line) and then you're trying to assign it something...
Try this:
Button golf = (Button)findViewById(R.id.golf);
if (golf != null) {
golf.setOnClickListener(new View.OnClickListener() {
EDITED:
Put these lines in your 2.2 layout:
<Button
android:id="#+id/golf"
android:visibility="invisible"
android:layout_height="1dp"
android:layout_width="1dp"
android:layout_alignParentLeft="true">
</Button>
With attribute visibility="invisible" your app should work and your layout shouldn't be wasted

Categories

Resources