getCheckedRadioButtonId just gives me nullpoint exception - android

Im having some issues, i have two Groups of Radiobuttons in my XML which has handling in an activity. It just crashes with nullpoint exception on line 48:
int languangeId = languageGroup.getCheckedRadioButtonId();
I have set a "default" checked button in the XML to one of the buttons in the buttongroup,
So why doesnt it get an valid value?:(
public class FirstTimeSelectMenu extends Activity{
private RadioGroup languageGroup;
private RadioGroup storageGroup;
private Button okButton;
private String getStorage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_time_run);
languageGroup = (RadioGroup)this.findViewById(R.id.languageGroup);
storageGroup = (RadioGroup)this.findViewById(R.id.storageGroup);
okButton = (Button)findViewById(R.id.okBtn);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int languangeId = languageGroup.getCheckedRadioButtonId();
int storageId = storageGroup.getCheckedRadioButtonId();
RadioButton languageb = (RadioButton)findViewById(languangeId);
RadioButton storageb = (RadioButton)findViewById(storageId);
if(languageb.equals("Norwegian")){
//Need to fix this!
}
if(languageb.equals("English")){
//Need to fix this!
}
if(storageb.equals("SD Card")){
String sdStoragePath = Environment.getExternalStorageDirectory().toString() + "/PictureTalk/";
FileInteraction fi = new FileInteraction();
fi.firstTimeFillPath(getResources(), "PictureTalk/Food", sdStoragePath +"PictureTalk/Food");
fi.firstTimeFillPath(getResources(), "PrivatePictures", Environment.getExternalStorageDirectory().toString() +"PrivatePictures");
Intent intent = new Intent(getApplicationContext(),MainMenuActivity.class);
intent.putExtra("dataStorePath",sdStoragePath);
startActivity(intent);
}
}
});
}}
My XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/languagetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="236dp"
android:text="Choose language" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/languagetext"
android:layout_alignRight="#+id/languagetext"
android:layout_below="#+id/languageGroup">
<RadioButton
android:id="#+id/norwegianRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Norwegian" />
<RadioButton
android:id="#+id/englishRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English" />
</RadioGroup>
<TextView
android:id="#+id/internal_sd"
android:layout_width="464dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="236dp"
android:text="Do you want to store the data to:" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="#+id/storageGroup">
<RadioButton
android:id="#+id/sdcardRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:checked="true"
android:text="SD card" />
<RadioButton
android:id="#+id/internalRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Internal storage" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="#+id/okBtn"
android:layout_gravity="bottom" />

To start off with, what element is assigned the id "#+id/languageGroup" since it doesn't appear to be declared in your XML

Related

Error:NullPointerException when put button.setOnclickListener in onResponse method [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
When i click my button in my activity, it will stop and show an error like this :
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
In my activity, i put button.setOnclickListener in onResponse() method from Response.Listener. I do this because i want to get the data from JSONObject and use that data when i click my button.
Sorry, I'm not too fluent in English :)
My activity code:
public class MenuDaftarNilai2 extends AppCompatActivity {
TextView tvMatkulDN, tvKlpDN, tvMhsDN;
Button bUasDN;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_daftar_nilai2);
tvMatkulDN = (TextView) findViewById(R.id.tvMatkulDN) ;
tvKlpDN = (TextView) findViewById(R.id.tvKlpDN) ;
tvMhsDN = (TextView) findViewById(R.id.tvMhsDN) ;
bUasDN = (Button) findViewById(R.id.bUasDN) ;
Intent intent = getIntent();
final String nama_matkul = intent.getStringExtra("nama_matkul");
final String nama_klp = intent.getStringExtra("nama_klp");
final String nama_mhs = intent.getStringExtra("nama_mhs");
tvMatkulDN.setText(nama_matkul);
tvKlpDN.setText(nama_klp);
tvMhsDN.setText(nama_mhs);
Response.Listener responseListener1 = new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
final String nilai_uas = jsonResponse.getString("nilai_uas");
final String id_uas = jsonResponse.getString("id_uas");
bUasDN.setText("Nilai Uas : " + nilai_uas);
bUasDN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MenuDaftarNilai2.this, EditUas.class);
intent.putExtra("nilai_uas", nilai_uas);
intent.putExtra("nama_matkul", nama_matkul);
intent.putExtra("nama_klp", nama_klp);
intent.putExtra("nama_mhs", nama_mhs);
intent.putExtra("id_uas", id_uas);
MenuDaftarNilai2.this.startActivity(intent);
finish();
}
});
}catch (JSONException e) {
e.printStackTrace();
}
}
};
ResponUas resUas = new ResponUas(nama_klp,nama_mhs, responseListener1);
RequestQueue queue1 = Volley.newRequestQueue(MenuDaftarNilai2.this);
queue1.add(resUas);
My XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".LoginAsis.MenuAsisten.MenuDaftarNilai.MenuDaftarNilai2"
android:background="#ad4f4f">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama Mata Kuliah"
android:id="#+id/textView54"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama Kelompok"
android:id="#+id/textView58"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView54"
android:layout_alignParentLeft="true"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama Mahasiswa"
android:id="#+id/textView59"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView58"
android:layout_alignParentLeft="true"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:id="#+id/textView60"
android:textColor="#ffffff"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/textView61"
android:layout_alignStart="#+id/textView61" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:id="#+id/textView61"
android:textColor="#ffffff"
android:layout_below="#+id/textView60"
android:layout_alignLeft="#+id/textView62"
android:layout_alignStart="#+id/textView62" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:id="#+id/textView62"
android:textColor="#ffffff"
android:layout_marginLeft="68dp"
android:layout_marginStart="40dp"
android:layout_below="#+id/textView61"
android:layout_toRightOf="#+id/textView54"
android:layout_toEndOf="#+id/textView54" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:id="#+id/tvMatkulDN"
android:layout_alignTop="#+id/textView60"
android:layout_toRightOf="#+id/textView60"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:id="#+id/tvKlpDN"
android:layout_below="#+id/tvMatkulDN"
android:layout_toRightOf="#+id/textView60"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:id="#+id/tvMhsDN"
android:layout_below="#+id/tvKlpDN"
android:layout_toRightOf="#+id/textView61"
android:textColor="#ffffff"
android:layout_alignRight="#+id/tvKlpDN"
android:layout_alignEnd="#+id/tvKlpDN"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Belum ada nilai uas"
android:id="#+id/bUasDN"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#id/textView59"
android:gravity="left|center_vertical"
android:drawableRight="#drawable/ic_create_black_24dp"/>
</RelativeLayout>
just put this outside the response parenthesis ...............
bUasDN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MenuDaftarNilai2.this, EditUas.class);
intent.putExtra("nilai_uas", nilai_uas);
intent.putExtra("nama_matkul", nama_matkul);
intent.putExtra("nama_klp", nama_klp);
intent.putExtra("nama_mhs", nama_mhs);
intent.putExtra("id_uas", id_uas);
MenuDaftarNilai2.this.startActivity(intent);
finish();
}
});
enjoy coding..........

How to switch between 2 TextViews places on the layout?

I have an OnClickListener that listens to Button A clicks. I also have 2 TextViews below the Button.
I want that on every click on Button A, the 2 TextViews will switch their places between them, like this:
Before clicking:
TextView A
TextView B
After clicking:
TextView B
TextView A
How can I do it? Is there a special function that meant to do that? or some kind of a trick? Thanks!
actvity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
</RelativeLayout>
MainActivity.java:
txtA = (TextView) findViewById(R.id.txtA);
txtB = (TextView) findViewById(R.id.txtB);
float mAX = txtA.getX();
float mAY = txtA.getY();
float mBX = txtB.getX();
float mBY= txtB.getY();
txtA.setX(mBX);
txtA.setY(mBY);
txtB.setX(mAX);
txtB.setY(mAY);
Trick is changing the x and y axis of both views :
Your xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:text="A"
android:tag="A"
android:clickable="true"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA"
android:text="B"
android:tag="B"
android:clickable="true"
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch"
android:id="#+id/btn1"
android:onClick="onClickButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Java Code :
public class MainActivity extends Activity {
TextView txtA,txtB;
boolean _isOnTxtA;
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtA = (TextView)findViewById(R.id.txtA);
txtB = (TextView)findViewById(R.id.txtB);
btn1 = (Button)findViewById(R.id.btn1);
}
public void onClickButton(View v)
{
float mPos1x,mPos1y,mPos2x,mPos2y;
mPos1x = txtB.getX();
mPos1y = txtB.getY();
mPos2x = txtA.getX();
mPos2y= txtA.getY();
if(_isOnTxtA)
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA = false;
}
else
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA= true;
}
}
}
View.bringToFront method may be useful.
Where your layout is like:
<LinearLayout>
<TextView A>
<TextView B>
</LinearLayout>
To call bringToFront on TextView A will bring it front (the last position in the parent LinearLayout).
<LinearLayout>
<TextView B>
<TextView A>
</LinearLayout>
For more detailed example, below is layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/text_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_a" />
<TextView
android:id="#+id/text_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_b" />
</LinearLayout>
In your OnClickListener (in your Activity) call:
View textViewA = findViewById(R.id.text_a);
textViewA.bringToFront();
This should work.
Toggling behavior can be achieved by this application. For example:
ViewGroup textHolder = (ViewGroup) findViewById(R.id.text_holder);
textHolder.getChildAt(0).bringToFront();
ViewGroup.getChildAt(0) always returns the first child of the ViewGroup. So everytime you call bringToFront on the first child will be bring to front.
All previous answers do not work because you use a relative layout where a linear layout will suffice.
If you have to go RelativeLayout, do the following in your click listener
TextView textA = (TextView) findViewById(R.id.txtA);
TextView textB = (TextView) findViewById(R.id.txtB);
RelativeLayout.LayoutParams textALayoutParams = (RelativeLayout.LayoutParams) textA.getLayoutParams();
textALayoutParams.addRule(RelativeLayout.BELOW, R.id.txtB);
textA.setLayoutParams(textALayoutParams);
RelativeLayout.LayoutParams textBLayoutParams = (RelativeLayout.LayoutParams) textB.getLayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
textBLayoutParams.removeRule(RelativeLayout.BELOW);
} else {
textBLayoutParams.addRule(RelativeLayout.BELOW,0);
}
textB.setLayoutParams(textBLayoutParams);
This will place A under B. You can do the same for reversing.

How to get selected radio button value from question1 activity page to next page

i am developing a quiz using mobile application. currently, i am having problems on how to retrieve selected radio button from previous page. i want the selected radio button value to be displayed on the next page. below is my code in xml and java.
question1. java
public class question1 extends Activity {
private Button Button2;
public RadioButton r1,r2,r3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question1);
r1 = (RadioButton)findViewById(R.id.rb1);
r2 = (RadioButton)findViewById(R.id.rb2);
r3 = (RadioButton)findViewById(R.id.rb3);
//Button2
Button2 = (Button) findViewById(R.id.button1);
Button2.setOnClickListener((new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),question2.class);
intent.putExtra("r1",r1.isSelected());
intent.putExtra("r2",r2.isSelected());
intent.putExtra("r3",r3.isSelected());
startActivityForResult(intent,0);
}
}));
}
}
this is my code in question1.xml.
<?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="wrap_content"
android:gravity="center"
android:padding="10dp" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="452dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:src="#drawable/clay" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="YANG MANA SATUKAH DI ANTARA AYAT-AYAT BERIKUT YANG BENAR ?"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/radioButton2"
android:text="A.Tanah liat yang leper lebih berat" />
<RadioButton
android:id="#+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/radioButton3"
android:text=" B. Kedua-dua tanah liat adalah sama berat" />
<RadioButton
android:id="#+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_alignLeft="#+id/radioButton2"
android:layout_marginBottom="14dp"
android:text="C.Bola tanah liat lebih berat" />
</RadioGroup>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:background="#drawable/nextnext" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
this is my code for the second page that i want the selected radio button value to be displayed.
question2. java
public class question2 extends Activity
{
private Button Button2;
public RadioButton r1,r2,r3,r4,r5;
public RadioGroup rg1,rg2;
public String a2,b2,total;
public TextView output ;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.question2);
output = (TextView)findViewById(R.id.textView5);
r1 = (RadioButton)findViewById(R.id.rb1);
r2 = (RadioButton)findViewById(R.id.rb2);
r3 = (RadioButton)findViewById(R.id.rb3);
r4 = (RadioButton)findViewById(R.id.rb4);
r5 = (RadioButton)findViewById(R.id.rb5);
rg1 = (RadioGroup)findViewById(R.id.radioGroup1);
rg2 = (RadioGroup)findViewById(R.id.radioGroup2);
Object RadioButton;
r1= (RadioButton).getValue();
r2 =(RadioButon).getValue();
r3 =(RadioButon).getValue();
//Button2
Button2 = (Button) findViewById(R.id.button1);
Button2.setOnClickListener((new OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent(view.getContext(),question3.class);
startActivityForResult(intent,0);
}
}
));
{
switch(rg2.getCheckedRadioButtonId()){
case R.id.rb4:
if (r4.isChecked()){
//a2 =0;
}
break;
case R.id.rb5:
if (r5.isChecked()){
//b2 = 1;
}
break;
//total = rg1.getCheckedRadioButtonId()+rg2.getCheckedRadioButtonId() ;
}}
//receive the arguments from the previous Activity
Bundle extras = getIntent().getExtras();
if (extras==null) {
return;
}
output = (TextView) findViewById(R.id.textView5);
Bundle data = this.getIntent().getExtras();
r1=data.getValue(r1);
r2=data.getValue(r2);
r3=data.getValue(r3);
double result = 0;
if (r1 == "")
{
result+ = 1;
}
else if (r2 == "true"){
result+ = 0;}
else if (r3=="true")
{
result+ = 0;
}
else {
result + =0;
}
}
}
}
question2.xml
<?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:padding="10dp" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="452dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_weight="0.13"
android:src="#drawable/soklan2" />
<RadioGroup
android:id="#+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/textView4" >
<RadioButton
android:id="#+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A. Ya" />
<RadioButton
android:id="#+id/rb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/radioGroup1"
android:layout_below="#+id/radioGroup1"
android:text="B. Tidak" />
</RadioGroup>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioGroup2"
android:layout_marginTop="20dp"
android:text="Result"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:background="#drawable/nextnext" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
I would like to know, how to retrieve the value from question 1 and displayed it on question2 page.
you could use:
r1.setChecked(getIntent().getExtras().getBoolean("r1"));
r2.setChecked(getIntent().getExtras().getBoolean("r2"));
r3.setChecked(getIntent().getExtras().getBoolean("r3"));
You may also need to change the code where you put the values:
intent.putExtra("r1",r1.isChecked());
intent.putExtra("r2",r2.isChecked());
intent.putExtra("r3",r3.isChecked());
You should also probably look at this code:
Object RadioButton;
r1 = (RadioButton).getValue();
r2 = (RadioButon).getValue();
r3 = (RadioButon).getValue();
I have no idea what you are trying to accomplish here. And it should probably be removed to keep the refferences to your RadioButton Objects
In you second activity , get the values from intent extras:
Intent intent = getIntent();
boolean r1Value = intent.getBooleanExtra("r1",false);
r1.setChecked(r1Value );

Null Pointer Exception - Getting EditText Value

This is my first question on here I will apologize in advance if I didn't ask this question very well. I have looked at my other questions similar to my problem, but I have not found a good solution to satisfy what is going on in my program.
So my problem is I am trying to assign a value to a variable called, num1 from my EditText field called, num1TextField, but I am not having any luck so far.
The segment inside my java file that throws the exception is:
EditText num1Field = (EditText)v.findViewById(R.id.num1TextField);
num1 = num1Field.getText().toString();
I appreciate every contribution to this problem in advance.
Here is my fragment_compute.xml file:
<?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:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="#string/compute_activity"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="#string/num_1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/num1TextField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="number"
android:gravity="center" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/num2TextField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/num1TextField"
android:layout_below="#+id/textView3"
android:ems="10"
android:inputType="number"
android:layout_centerHorizontal="true"
android:gravity="center" >
</EditText>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/num2TextField"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="#string/result"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/num1TextField"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="#string/num_2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp" />
<Button
android:id="#+id/compute_multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/num2TextField"
android:layout_below="#+id/result"
android:layout_marginTop="40dp"
android:text="#string/multiply" />
<Button
android:id="#+id/compute_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/compute_multiply"
android:layout_alignBottom="#+id/compute_multiply"
android:layout_alignLeft="#+id/num2TextField"
android:text="#string/add" />
</RelativeLayout>
Here is my ComputeFragment.java code:
public class ComputeFragment extends Fragment {
String num1;
String num2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_compute, parent, false);
// Add Button
Button addButton = (Button)v.findViewById(R.id.compute_add);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// ERROR STARTS HERE!
EditText num1Field = (EditText)v.findViewById(R.id.num1TextField);
num1 = num1Field.getText().toString(); // Null Pointer Exception is thrown here!
EditText num2Field = (EditText)v.findViewById(R.id.num2TextField);
num2 = num2Field.getText().toString();
// ERROR ENDS HERE!
Intent i = new Intent(getActivity(), AddActivity.class);
i.putExtra("x", num1);
i.putExtra("y", num2);
startActivityForResult(i,0);
}
});
return v;
}
}
The parameter v in EditText num1Field = (EditText)v.findViewById(R.id.num1TextField); is your button, not the whole view.
You should declare the variable in onCreateView:
final EditText num1Field = (EditText)v.findViewById(R.id.num1TextField);
And use it to get the value in onClick
num1 = num1Field.getText().toString();
The parameter view is the button. If the button is on the same parent as the editTexts you could also do:
View parentView = (View) view.getParent();
EditText num1TextField = (EditText)parentView.findViewById(R.id.num1TextField);
Try this code -
#Override
public void onClick(View view) {
EditText num1Field = (EditText)v.findViewById(R.id.num1TextField);
if(num1Field.getText().length() > 0) {
num1 = num1Field.getText().toString();
}
}

Android Activity as dialog black border

I have activity as a dialogue, all the shows I like. But I have a problem with a frame around them. Just like here.
My Activity:
public class AlarmAlert extends Activity implements Alarms.AlarmSettings {
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
AlarmAlertWakeLock.acquire(this);
/*
* FIXME Intentionally verbose: always log this until we've fully
* debugged the app failing to start up
*/
Log.v("AlarmAlert.onCreate()");
DigitalClock dc = (DigitalClock) findViewById(R.id.digitalClock1);
mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
Intent i = getIntent();
mAlarmId = i.getIntExtra(Alarms.ID, -1);
Alarms.getAlarm(getContentResolver(), this, mAlarmId);
mKlaxon = new AlarmKlaxon();
mKlaxon.postPlay(this, mAlarmId);
/* Set the title from the passed in label */
setTitleFromIntent(i);
/*
* allow next alarm to trigger while this activity is active
*/
Alarms.disableSnoozeAlert(AlarmAlert.this);
Alarms.disableAlert(AlarmAlert.this, mAlarmId);
Alarms.setNextAlert(this);
mKlaxon.setKillerCallback(new AlarmKlaxon.KillerCallback() {
public void onKilled() {
if (Log.LOGV)
Log.v("onKilled()");
updateSilencedText();
/* don't allow snooze */
mSnoozeButton.setEnabled(false);
dismiss();
mState = KILLED;
}
});
updateLayout();
SharedPreferences settings = getSharedPreferences(
AlarmClock.PREFERENCES, 0);
if (settings.getBoolean(AlarmClock.PREF_SHAKE_SNOOZE, true)) {
mShakeListener = new ShakeListener(this);
mShakeListener
.setOnShakeListener(new ShakeListener.OnShakeListener() {
public void onShake() {
snooze();
if (mCaptchaSnooze == 0)
finish();
}
});
}
}
private void setTitleFromIntent(Intent i) {
mLabel = i.getStringExtra(Alarms.LABEL);
if (mLabel == null || mLabel.length() == 0) {
mLabel = getString(R.string.default_label);
}
setTitle(mLabel);
}
private void updateSilencedText() {
TextView silenced = (TextView) findViewById(R.id.silencedText);
silenced.setText(getString(R.string.alarm_alert_alert_silenced,
mDuration / 1000 * 60));
silenced.setVisibility(View.VISIBLE);
}
private void updateLayout() {
setContentView(R.layout.alarm_alert);
My alert_alarm.xml (Dialog).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="327dp"
android:layout_height="295dp"
android:background="#drawable/alertbg"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/silencedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingTop="2dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/ltgrey"
android:visibility="gone" />
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="230dp"
android:gravity="center_horizontal" >
<Button
android:id="#+id/snooze"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="20dp"
android:background="#drawable/teesstt"
android:text="#string/snooze_button"
android:textColor="#fff"
android:textSize="25dp"
android:visibility="visible" />
<Button
android:id="#+id/dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="25dp"
android:layout_marginTop="20dp"
android:background="#drawable/teesstt"
android:text="#string/dismiss_button"
android:textColor="#fff"
android:textSize="25dp"
android:visibility="visible" />
</RelativeLayout>
<TextView
android:id="#+id/tvDD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tvDate"
android:layout_alignLeft="#+id/tvDate"
android:text="Medium Text"
android:textSize="20dp"
android:textColor="#a6a6a6" />
<TextView
android:id="#+id/tvDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/relativeLayout1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="55dp"
android:layout_marginLeft="30dp"
android:text="Medium Text"
android:textSize="25dp"
android:textColor="#a6a6a6" />
<com.boxclocks.android.alarmclock.dc
android:id="#+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="58dp"
android:text="DigitalClock"
android:textColor="#a6a6a6"
android:textSize="70dp" />
</RelativeLayout>
How do I remove the border.
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Dialog
You have two options. You can set the theme of your Activity to
Theme_Translucent_NoTitleBar
Or you have to create a new theme and load it with a custom nine patch as specified here
How to remove border from Dialog?

Categories

Resources