this sounds easy at the beginning but is driving me insane.
So i downloaded the latest android sdk and eclipse and now there is somthing new.... :
when iam creating a Activity and a Layout it generates me 2 Layout files somthing like: main_laout.xml and fragment_main.xml
however eclipse opend up only the fragment file and i made my GUI there. When iam starting my Application all my Buttons and TextViews are there. I Press a button and a Second Activity starts.
And here my Problem: The Second Activity is like the First one (the 2 layout xml files but here called status)
when iam trying to change a TextView there i get a nullPointer exception. Can anyone plz help me with this iam getting crazy.
My Code so far:
statusActivity:
public class StatusActivity extends ActionBarActivity{
private TextView version,dbstatus,dbrows;
private Button done,refresh;
NetworkTask task;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
version=(TextView) findViewById(R.id.versionsOutputTextSTATUS);
dbstatus=(TextView) this.findViewById(R.id.dbstatusOutputTextSTATUS);
dbrows=(TextView) this.findViewById(R.id.dbRowsOutputTextSTATUS);
done=(Button) this.findViewById(R.id.beendenButtonSTATUS);
refresh=(Button) this.findViewById(R.id.refreshButtonSTATUS);
version.setText("Test");
}
And my xml files:
activity_status.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.project.zigbeecontrol.StatusActivity"
tools:ignore="MergeRootFrame" />
fragment_status.xml:
<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:background="#drawable/eisblumen"
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="de.project.zigbeecontrol.StatusActivity$PlaceholderFragment" >
<TextView
android:id="#+id/welcomeTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:text="#string/status"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/versionsOutputTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/versionTextSTATUS"
android:layout_alignBottom="#+id/versionTextSTATUS"
android:layout_marginLeft="24dp"
android:layout_toRightOf="#+id/versionTextSTATUS"
android:text="#string/empty"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/dbstatusOutputTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/dbstatusTextSTATUS"
android:layout_alignLeft="#+id/versionsOutputTextSTATUS"
android:text="#string/empty"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/beendenButtonSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/welcomeTextSTATUS"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/welcomeTextSTATUS"
android:background="#drawable/button_trans"
android:text="#string/endeStatus"
android:onClick="onClickStatus" />
<TextView
android:id="#+id/dbRowsTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/beendenButtonSTATUS"
android:layout_alignRight="#+id/dbstatusTextSTATUS"
android:layout_marginBottom="26dp"
android:text="#string/dbrows"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/dbstatusTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/beendenButtonSTATUS"
android:text="#string/datenbankstatus"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/versionTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/dbstatusTextSTATUS"
android:layout_below="#+id/welcomeTextSTATUS"
android:layout_marginTop="56dp"
android:text="#string/version"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/dbRowsOutputTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/dbRowsTextSTATUS"
android:layout_alignBottom="#+id/dbRowsTextSTATUS"
android:layout_alignLeft="#+id/dbstatusOutputTextSTATUS"
android:text="#string/empty"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/refreshButtonSTATUS"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/welcomeTextSTATUS"
android:layout_marginBottom="17dp"
android:layout_marginLeft="31dp"
android:layout_toRightOf="#+id/welcomeTextSTATUS"
android:text="#string/refresh"
android:onClick="onClickStatus" />
</RelativeLayout>
even when its working i post the parts of the "main" programm too so you can see how i worked there :
MainActivity:
public class MainActivity extends ActionBarActivity{
//Buttons
private Button beendenButton;
private Button statusbutton;
private Button restartButton;
private Button auswertungButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
/*Zuweißung der Buttons */
beendenButton=(Button) this.findViewById(R.id.endButtonMAIN);
statusbutton=(Button) this.findViewById(R.id.statusButtonMAIN);
restartButton=(Button) this.findViewById(R.id.restartButtonMAIN);
auswertungButton=(Button)this.findViewById(R.id.auswertungButtonMAIN);
/*Fertig mit initzialisieren warten auf Eingabe*/
}
private void statusMethod() {
try{
Intent intent = new Intent(this, StatusActivity.class);
startActivity(intent);
this.finish();
}catch(Exception e){errorMessage();}
}
//.... Some uninterresting Stuff here
public void onClick(View v)
{
switch (v.getId())
{
/*Auswahl was gedrückt wurde und aufruf der Entsprechenden Methode */
case R.id.endButtonMAIN: endActivity(); break;
case R.id.statusButtonMAIN: statusMethod();break;
case R.id.restartButtonMAIN: restartMethod();break;
case R.id.auswertungButtonMAIN: sensorMethod(); break;
default: break;
}
}
}
And the two xmls from layout:
activity_main:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.project.zigbeecontrol.MainActivity"
tools:ignore="MergeRootFrame"
/>
and last but not least fragment_main:
<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:background="#drawable/eisblumen"
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="de.project.zigbeecontrol.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/endButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/welcomeTextMAIN"
android:background="#drawable/button_trans"
android:text="#string/beenden"
android:onClick="onClick" />
<TextView
android:id="#+id/welcomeTextMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/endButtonMAIN"
android:layout_centerHorizontal="true"
android:text="#string/welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/statusButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/welcomeTextMAIN"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:background="#drawable/button_trans"
android:text="#string/status"
android:onClick="onClick" />
<Button
android:id="#+id/restartButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/statusButtonMAIN"
android:layout_below="#+id/statusButtonMAIN"
android:layout_marginTop="19dp"
android:background="#drawable/button_trans"
android:text="#string/neustart"
android:onClick="onClick" />
<Button
android:id="#+id/auswertungButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/restartButtonMAIN"
android:layout_below="#+id/restartButtonMAIN"
android:layout_marginTop="33dp"
android:background="#drawable/button_trans"
android:text="#string/sensoren"
android:onClick="onClick" />
</RelativeLayout>
So PLZ! why do i get a NPE when trying:
version.setText("Test");
regards
Try this..
Change this..
setContentView(R.layout.activity_status);
to
setContentView(R.layout.fragment_status);
because TextViews and Buttons are in fragment_status.xml so setContentView should refer fragment_status.xml
and remove
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Ans same like that in MainActivity.java
Change this..
setContentView(R.layout.activity_main);
to
setContentView(R.layout.fragment_main);
and remove
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Here is your problem
setContentView(R.layout.activity_status);
but your text view is not present in activity_status.xml. Create your Views in activity_status.xml instead of
fragment_status.xml and remove
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Related
I have this screen.
And want to convert it to this.
I hope you understand what I am trying to do. Here is my xml code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/appBackground"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:weightSum="1"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="loginScreen.solution.example.com.loginScreen.WelcomeActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#CC5ec639"
app:popupTheme="#style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
android:layout_gravity="center"
android:id="#+id/toolbar_title"
android:textStyle="bold"
/>
</android.support.v7.widget.Toolbar>
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
style="#style/TextView"
android:ems="10"
android:text="Name: "
android:backgroundTint="#color/textboxTint"
/>
<TextView
android:id="#+id/tv_email"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
style="#style/TextView"
android:ems="10"
android:text="Email: "/>
<TextView
android:id="#+id/tv_phone"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
style="#style/TextView"
android:ems="10"
android:text="Phone: "/>
</LinearLayout>
and my activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button loginButton;
Button signUpButton;
ViewFlipper viewFlipper;
#Override
public void onCreate(Bundle instanceState) {
super.onCreate(instanceState);
setContentView(R.layout.content_main);
loginButton = (Button)findViewById(R.id.bt_login);
loginButton.setTag(true);
signUpButton = (Button)findViewById(R.id.bt_signup);
viewFlipper = (ViewFlipper)findViewById(R.id.view_flipper);
loginButton.setOnClickListener(this);
signUpButton.setOnClickListener(this);
updateTheViewBasedOnTag();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_login:
loginButton.setTag(true);
updateTheViewBasedOnTag();
viewFlipper.startFlipping();
break;
case R.id.bt_signup:
signUpButton.setTag(true);
updateTheViewBasedOnTag();
break;
}
}
private void updateTheViewBasedOnTag() {
if((boolean)loginButton.getTag())
{
loginButton.setBackground(getDrawable(R.drawable.selected_background));
signUpButton.setBackground(getDrawable(R.drawable.normal_background));
}else
{
loginButton.setBackground(getDrawable(R.drawable.selected_background));
loginButton.setBackground(getDrawable(R.drawable.normal_background));
}
}
}
The
viewFlipper.startFlipping();
doesn't seem to work correctly. I don't click the signup button but still the sign up for is shown. Also, the clicking of the buttons don't respond
What's wrong?
Thanks,
Theo.
I am just trying to guess what is wrong with your code. You are setting the tag to true for one button and forgot to set the false for other button.
switch (v.getId()) {
case R.id.bt_login:
loginButton.setTag(true);
signUpButton.setTag(false);
updateTheViewBasedOnTag();
viewFlipper.startFlipping();
break;
case R.id.bt_signup:
signUpButton.setTag(true);
loginButton.setTag(false);
updateTheViewBasedOnTag();
break;
}
I am very new at android development, and I am trying to make an app which has 4 buttons in its main activity and when I click on one of its button it takes me to another activity and displays its xml file, what should I write in the 2nd activity? Here is my code so far.
main xml file
<?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">
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#color/colorAccent"
android:text="Overview"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Information" />
<TableRow
android:id="#+id/hr1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Work Experience" />
<TableRow
android:id="#+id/hr3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
</LinearLayout>
</LinearLayout>
........................
main activity
package com.lakshay.display.piechart;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
Button btn1 , btn2 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
String nextAct = null ;
String shield = "com.lakshay.display.piechart";
Integer flag= -1;
switch (v.getId())
{
case (R.id.one ):
nextAct = shield + "ContactActicity";
break;
default:
Toast.makeText(MainActivity.this , "Item Currently Unavailable"
, Toast.LENGTH_SHORT).show();
}
try {
if (nextAct!=null)
{
intent = new Intent(MainActivity.this , Class.forName(nextAct));
flag = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
if (flag != -1 ){
intent.setFlags(flag);
} startActivity(intent);
}
} catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
...................
xml file for 2nd activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorAccent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
tools:context="com.lakshay.display.piechart.ContactActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Name"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:inputType="text"
android:paddingBottom="20dp"
android:paddingLeft="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Address"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:id="#+id/address"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:id="#+id/number"
android:paddingBottom="20dp"
android:background="#android:color/background_light"
android:paddingLeft="20dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
</LinearLayout>
...................
My answer as a checklist:
1.- If you are using android studio you should create 2nd activity with the assistant so you dont get into more complications.
The 2nd activity must have an xml file and a class file.
2.- You should add android:onClick propertie for your button in the xml file on the activity.
3.- Your 2nd activity must have an onCreate method in the class file to fill the contents of the 2nd activity.
3b.- You can leave onCreate with default content but your xml file must then have al the info of your textviews.
I can elaborate further if needed.
Let's say you have two activities : Activity1.java and Activity2.java.
to start Activity2 from Activity1 just do :
final Intent intent = new Intent(this, Activity2.class);>startActivity(intent)
If you want to start activity on button click you have to write this codein an onClickListener. To do that, write the following attribute in your button definition in Activity1 xml file.
android:onCLick="onButtonClick"
Then in your activity write the following listener :
public void onButtonClick(final View v) {
// put your intent here
}
This code help you ....
public class MainActivity extends AppCompatActivity {
Button btn1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn1);
final String name = editText.getText().toString();
button.setOnClickListener(new View.OnClickListener() {//when your btn1 button press
public void onClick(View v) {
Intent intent = new Intent(this, Activity2.class);//this is call your second activity
startActivity(intent);//start activity
}
});
}
}
First of all, you must get your Button id from your xml. Or you will get NullPointerException so change your onCreate like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
And if you want to call intent with Class, you can see this solution
OR
You can simply call another activity like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(firstActivity.this, secondActivity.class));
}
});
}
This is my java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
counter = 0;
add = (Button) findViewById(R.id.add_butt);
sub = (Button) findViewById(R.id.sub_butt);
display = (TextView) findViewById(R.id.tv_text);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
display.setText("Your Total is " + counter);
}
});
and this is my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
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="com.example.myfirstapp.StartingPoint$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Total is 0"
android:textSize="40sp"
android:layout_gravity="center"
android:id="#+id/tv_text" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add 1"
android:layout_gravity="center"
android:id="#+id/add_butt" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Sub 1"
android:layout_gravity="center"
android:id="#+id/sub_butt" />
</LinearLayout>
I get a NullPointerExeption error when I try to add the listeners, because the findViewById method returns null on all the views... I think that everything is in place. All the views are in the XML and they have the proper ID's, so what did i do wrong?
You are trying to findViewById of views that are located in the Fragment.
Try moving this code to your PlaceholderFragment class (OnCreateView)
New android developer here. I am trying to create a dynamic UI that loads based on the users selection of a RadioGroup. Based on their selection, one of 3 possible fragments will be loaded into a LinearLayout section. This is my first attempt at my own sample problem that is not just a walk-through tutorial. Here is the main activity:
public class BaseConverter extends Activity {
RadioGroup convert;
Fragment toFragment;
RadioGroup toRadioGroup = null;
TextView inputDisplay = null;
TextView outputDisplay = null;
TextView resultTitle = null;
#Override
public void onCreate(Bundle sIS) {
super.onCreate(sIS);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.base_converter);
convert = (RadioGroup) this.findViewById(R.id.bc_convert_group);
convert.setOnCheckedChangeListener(new ConvertListener());
FragmentManager fm = getFragmentManager();
FragmentTransaction converterFragment = fm.beginTransaction();
ConvertEmptyFragment emptyTo = new ConvertEmptyFragment();
converterFragment.replace(R.id.bc_converter_fragment, emptyTo);
converterFragment.commit();
FragmentTransaction toFragment = fm.beginTransaction();
ConvertEmptyFragment emptyConverter = new ConvertEmptyFragment();
toFragment.replace(R.id.bc_to_fragment, emptyConverter);
toFragment.commit();
}
#Override
public void onResume() {
convert.clearCheck();
super.onResume();
}
#Override
public void onPause() {
convert.clearCheck();
super.onPause();
}
// I put a little null check so you can see how I'm trying to access the TextViews and what results
public void updateUIComponents(){
View converterView = this.findViewById(R.id.bc_converter_fragment);
inputDisplay = (TextView)converterView.findViewById(R.id.bc_display_input);
outputDisplay = (TextView)converterView.findViewById(R.id.bc_display_output);
if (inputDisplay == null){
Log.d("BaseConverter", "inputDisplay == null");
} else {
Log.d("BaseConverter", "inputDisplay != null");
}
}
class ConvertListener implements OnCheckedChangeListener {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Fragment toFragment;
Fragment converterFragment;
switch (checkedId) {
case R.id.bc_convert_binary:
toFragment = new ConvertRBFragmentBinary();
converterFragment = new ConverterFragmentBinary();
break;
case R.id.bc_convert_decimal:
toFragment = new ConvertRBFragmentDecimal();
converterFragment = new ConverterFragmentDecimal();
break;
case R.id.bc_convert_hex:
toFragment = new ConvertRBFragmentHex();
converterFragment = new ConverterFragmentHex();
break;
default:
toFragment = new ConvertEmptyFragment();
converterFragment = new ConvertEmptyFragment();
break;
}
FragmentManager fm = getFragmentManager();
FragmentTransaction converterTransaction = fm.beginTransaction();
converterTransaction.replace(R.id.bc_converter_fragment, converterFragment);
converterTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
converterTransaction.commit();
FragmentTransaction toTransaction = fm.beginTransaction();
toTransaction.replace(R.id.bc_to_fragment, toFragment);
toTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
toTransaction.commit();
updateUIComponents();
}
}
So, based on what a user chooses, the proper fragments will be loaded into the respective LinearLayout sections. However, now I want to implement the business logic of the fragments (which is just integer base conversion; i.e. binary number to decimal...) but when I try to access the TextViews, as seen in the updateUIComponents method, I get null pointers. What am I missing?
Here's the ConverterFragmentBinary class for reference:
public class ConverterFragmentBinary extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sIS){
View v = inflater.inflate(R.layout.converter_fragment_binary, container, false);
return v;
}
}
and its respective xml layout for reference:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF000000"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/bc_binary_converter_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dip"
android:maxHeight="30dip"
android:src="#drawable/binary_converter" />
<TextView
android:id="#+id/bc_display_input"
style="#style/input_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:layout_marginTop="5dip"
android:gravity="center_vertical|right"
android:lines="1"
android:minHeight="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF000000"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/button_num_0"
style="#style/op_button_land"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:gravity="center"
android:onClick="num0"
android:text="#string/num_0" />
<Button
android:id="#+id/button_num_1"
style="#style/op_button_land"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:gravity="center"
android:onClick="num1"
android:text="#string/num_1" />
</LinearLayout>
<TextView
android:id="#+id/bc_result_title"
style="#style/radio_button_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="10dip"
android:gravity="left"
android:text="#string/choose_convert" />
<TextView
android:id="#+id/bc_display_output"
style="#style/display_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:layout_marginTop="5dip"
android:gravity="center_vertical|right"
android:lines="1"
android:minHeight="30sp" />
</LinearLayout>
and then heres the main activity it gets loaded into:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/base_conversion_layout"
style="#style/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="5"
android:baselineAligned="false"
android:gravity="center_vertical|left"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical" >
<TextView
style="#style/radio_button_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/convert" />
<RadioGroup
android:id="#+id/bc_convert_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dip" >
<RadioButton
android:id="#+id/bc_convert_binary"
style="#style/radio_button"
android:text="#string/binary" />
<RadioButton
android:id="#+id/bc_convert_decimal"
style="#style/radio_button"
android:text="#string/decimal" />
<RadioButton
android:id="#+id/bc_convert_hex"
style="#style/radio_button"
android:text="#string/hex" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="#+id/bc_to_fragment"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/bc_converter_fragment"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="13"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Thanks in advance and sorry for the long code blocks but I figured it was better to include more than less.
Also, you should inflate your Fragments layout to bring it from your XML to your Java code instead of simply referring it using findViewById() method.
So instead of doing this,
View converterView = this.findViewById(R.id.bc_converter_fragment);
Do this inside your onCreateView method of the fragment,
View converterView = infalter.inflate(R.id.bc_converter_fragment,null);
updateUIComponents(converterView);//call this methid and pass your view
new method looks like this,
public void updateUIComponents(View converterView){
inputDisplay = (TextView)converterView.findViewById(R.id.bc_display_input);
outputDisplay = (TextView)converterView.findViewById(R.id.bc_display_output);
if (inputDisplay == null){
Log.d("BaseConverter", "inputDisplay == null");
} else {
Log.d("BaseConverter", "inputDisplay != null");
}
}
here is the code that is giving the force close error running on avd
public class MainActivity extends Activity {
Button magic;
TextView display;
int random;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
magic=(Button)findViewById(R.id.magic);
display=(TextView)findViewById(R.id.textView1);
magic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
random = (int)Math.ceil(Math.random()*100);
display.setText("your number is"+random);
}
});
}
}
here is the code i am using even if random function or settext is not used application force closes please help
activity_main.xml not giving any error or warning..........
xml file
<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:background="#drawable/magic8ball"
android:gravity="center"
tools:context=".MainActivity" >
<android.widget.Space
android:id="#+id/space1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="144dp"
android:layout_marginTop="192dp" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="131dp"
android:layout_marginTop="171dp"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/magic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/linearLayout1"
android:layout_toRightOf="#+id/linearLayout1"
android:text="#string/Magic" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Magic"
android:layout_alignParentRight="true"
android:layout_marginBottom="64dp"
android:layout_marginRight="74dp"
android:text="#string/Ask"
android:textAppearance="? android:attr/textAppearanceLarge"
android:textColor="#EE5533" />
</RelativeLayout>
the xml file is given where the single waring is related to the textview1 when hard coded
since the text is about to change whether it is good to set it on string.xml or hard code it ?????
For what purpose you are using following code.
**<android.widget.Space
android:id="#+id/space1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="144dp"
android:layout_marginTop="192dp" />**
This seems not available.. its working fine in the ICS devices.. but not working in Gingerbread devices.. I think that's the unusable code...