App Crashing when using onCreate Eclipse [Beginner] - java

This is so simple but I have no idea why it's crashing when I click a button.... It's meant to just change the TextView on the activity.
Code is below:::::
MainActivity:
package com.example.databasetut;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void displayText(String message) {
TextView textView = (TextView) findViewById(R.id.textDisplay);
textView.setText(message);
}
public void onClick_AddRecord(View v) {
displayText("Clicked add record");
}
public void onClick_ClearAll(View v) {
displayText("Clicked clear all");
}
public void onClick_DisplayRecords(View v) {
displayText("Clicked display record");
}
}
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/textDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.databasetut.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:onClick="onClick_DisplayRecords"
android:text="Display" />
<Button
android:id="#+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="onClick_ClearAll"
android:text="Clear" />
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="29dp"
android:onClick="onClick_AddRecord"
android:text="Add" />
<TextView
android:id="#+id/textDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="23dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
What's the problem here?!

You have given android:id="#+id/textDisplay" to both RelativeLayout and to TextView so error occurs.
Change the id of RelativeLayout or TextView your problem will be solved.

I dont think that you're alowed to use uppercase letters in view Id's in your xml layout. So the fix will be to change android:id="#+id/textDisplay" to ex. android:id="#+id/text_display". Plus remove android:id="#+id/textDisplay" from your RelativeLayout keep this id only on your textview

Related

Can't figure out how to show an android dialog

Im trying to get a dialog to display on my app, I followed the exact dialog tutorial on the android website https://developer.android.com/guide/topics/ui/dialogs.html
(with some slight name changes) however it is rather ambiguous about where to put certain portions of code leading to my code not working. Im not sure what to do at this point.
Dialog Fragment
public class CreatePlayerDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.player_name_dialog)
.setPositiveButton(R.string.player_name_dialog_yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.player_name_dialog_no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
XML File The button in question is id: button 6
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/content_start_new_game"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.hunte.assassin.StartNewGameActivity"
tools:showIn="#layout/activity_start_new_game">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
android:textAlignment="center"
android:hint="Name Of Game"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColorLink="?attr/colorPrimaryDark"
android:selectAllOnFocus="true" />
<ListView
android:layout_marginTop="15dp"
android:layout_height="300dp"
android:layout_width="250dp"
android:layout_below="#+id/editText"
android:layout_alignLeft="#+id/editText"
android:layout_alignStart="#+id/editText"
android:id="#+id/Players" />
<TextView
android:text=" Players:"
android:id="#+id/textView3"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="62dp"
android:layout_alignTop="#+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:text="Button"
android:id="#+id/button6"
android:foreground="#android:drawable/ic_input_add"
android:layout_marginTop="6dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_below="#+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="confirmFireMissles"/>
<ImageButton
android:text="Button"
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/button7"
android:layout_below="#+id/button6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:foreground="#android:drawable/ic_delete" />
<Button
android:text="Begin!"
android:layout_width="100dp"
android:layout_height="50dp"
android:id="#+id/button3"
android:layout_marginBottom="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Activity W/ show dialog method
package com.example.hunte.assassin;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.app.AlertDialog;
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class StartNewGameActivity extends AppCompatActivity {
private ListView lv;
public void confirmFireMissiles() {
DialogFragment newFragment = new CreatePlayerDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles");
}
ArrayList<Player> players = new ArrayList<Player>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_new_game);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
lv = (ListView) findViewById(R.id.Players);
// Instanciating an array list (you don't need to do this,
// you already have yours).
List<String> game_players = new ArrayList<String>();
for(int i = 0; i < players.size(); i++){
game_players.add(players.get(i).name);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
game_players );
lv.setAdapter(arrayAdapter);
}
}
This is what I thought would be the most logical implementation however my XML file does not seem to recognize my "confirmFireMissles" method and my activity file says that my method isnt used. Sorry for including so much code I just cannot identify where the problem may lie. Im only trying to get this to work so I can understand how to display a dialog
You need to pass View as parameter to confirmFireMissiles() method.
public void confirmFireMissiles(View view) {
DialogFragment newFragment = new CreatePlayerDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles");
}

Android Studio add EditText input to a ListView with button onClick

I am trying to make a simple Planner app so I have an XML file with a ListView on it, and another XML file with an EditText and a sumbit button on it, and of course my Java files. I have done a ton of research but I can't find a working method to make it when I press the button, it adds the EditText stuff to the ListView as an item. Here is my code:
activity_main.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"
tools:context=".MainActivity" >
<ListView
android:layout_width="300dp"
android:layout_height="fill_parent"
android:id="#+id/event_list"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/datePicker" />
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/datePicker"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calendarView"
android:layout_alignBottom="#+id/datePicker"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Event"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignEnd="#+id/datePicker"
android:layout_toEndOf="#+id/event_list"
android:background="#android:color/holo_blue_light"
android:onClick="newEvent"
android:textSize="20dp"
android:textColor="#android:color/white" />
</RelativeLayout>
new_plan.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_name"
android:hint="Event Name"
android:inputType="textCapWords"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/event_date"
android:hint="Event Date" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="#+id/button2"
android:layout_gravity="center_horizontal"
android:background="#android:color/holo_blue_light"
android:textSize="20dp"
android:textColor="#android:color/white" />
</LinearLayout>
MakePlan.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MakePlan extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_plan);
}
}
MainActivity.java
package com.kass.planner;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void newEvent(View v){
Intent i = new Intent(this, MakePlan.class);
startActivity(i);}}
Please help, thank you.
Take a look on this google developers project. It's for animations but i think it also have the solution to your question.
https://developer.android.com/training/animation/layout.html
You should append data to the collection of items linked to your ListView via your adapter and notify your adapter that your data has changed. This illustration assumes that you have a List of String objects.
EditText editText = ...
List<String> list = ...
YourAdapter adapter = ...
ListView listView = ...
You must have done something like this to setup your listView.
listView.setAdapter(new YourAdapter(list, this));
Now on button click you should get the String in EditText, add it to your List and finally notify the adapter that your data has changed in order to update your ListView for e.g.
void OnButtonClick(View view){
String strToAdd = editText.getText().toString();
list.add(strToAdd);
adapter.notifyDataSetChanged();
}

R.Id. cannot be resolved or is not a field - Can't find the mistake

From looking online, I know there is some problem with my XML file but not sure what it is. I've tried remaking the entire button from scratch but the error persists. The following is the code in the activity.
package com.example.linked1n;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.linked1n.R;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
Button login;
int counter;
EditText username, password;
String success;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 3;
username = (EditText)findViewById(R.id.getEmail);
password = (EditText)findViewById(R.id.getPassword);
login = (Button)findViewbyId(R.Id.login);
login.setOnClickListener(this);
}
private void loginClick() {
Intent intent = new Intent(this, ScreenAftLog.class );
startActivity(intent);
}
public void onClick (View v) {
switch (v.getId()) {
case R.Id.login:
loginClick();
break;
}
}
This is the XML code:
<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="com.example.linked1n.MainActivity" >
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="loginClick"
android:text="#string/yayaya" />
<EditText
android:id="#+id/getEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:ems="10"
android:inputType="textWebEmailAddress" />
<EditText
android:id="#+id/getPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/getEmail"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
</RelativeLayout>
Any help will be appreciated!
Edit: adding doesn't fix it
It is not R.Id.login, it should be R.id.login
You wrote ID in caps in your switch statement. id is a reserved word and needs to be in small letter, change the R.Id.login to R.id.login, then run :)
End tag is missing for the parent layout ...
Add </RelativeLayout> tag at the end. If the problem still persists, let us know.
Remove the last line - </EditText>
Below is your XML file which i build successfully. Try this.
<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="com.example.linked1n.MainActivity" >
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="loginClick"
android:text="Sample" />
<EditText
android:id="#+id/getEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:ems="10"
android:inputType="textWebEmailAddress" />
<EditText
android:id="#+id/getPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/getEmail"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="textPassword" />
<requestFocus />
</RelativeLayout>
java file
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
Button login;
int counter;
EditText username, password;
String success;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 3;
username = (EditText)findViewById(R.id.getEmail);
password = (EditText)findViewById(R.id.getPassword);
login = (Button)findViewById(R.id.login);
login.setOnClickListener(this);
}
private void loginClick() {
Intent intent = new Intent(this, ScreenAftLog.class );
startActivity(intent);
}
public void onClick (View v) {
switch (v.getId()) {
case R.id.login:
loginClick();
break;
}
}
}

How to pass values between fragments while using a viewpager in android

I am creating a sign up form which has been divided into three fragments.The three fragments are connected by a viewpager.I can traverse throught the fragments with next and previous buttons.I want the data from the first and second fragment to be available in the third fragment.One option is to use sharedpreferences.Is there any other approach.
The main class:
package pl.looksok.viewpagerdemo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends FragmentActivity {
ViewPager pager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager = (ViewPager)findViewById(R.id.myViewPager);
pager.setAdapter(pageAdapter);
}
public void selectFragment(int position){
pager.setCurrentItem(position, true);
// true is to animate the transaction
}
}
The main 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"
tools:context=".MainActivity" >
<pl.looksok.viewpagerdemo.CustomViewPager
android:id="#+id/myViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
The first fragment:
package pl.looksok.viewpagerdemo;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class FragmentBlue extends Fragment {
Button btnnext1;
RelativeLayout frag1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blue, container, false);
btnnext1=(Button) view.findViewById(R.id.btnnext1);
frag1=(RelativeLayout) view.findViewById(R.id.frag1);
btnnext1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
((MainActivity)getActivity()).selectFragment(1);
}
});
return view;
}
}
The first 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="#4ECDC4"
android:id="#+id/frag1"
>
<Button
android:id="#+id/btnnext1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="NEXT" />
<EditText
android:id="#+id/txtfname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtmname"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="FIRST NAME" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtmname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtfname"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="MIDDLE NAME" />
<EditText
android:id="#+id/txtlname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtmname"
android:layout_below="#+id/txtmname"
android:layout_marginTop="23dp"
android:ems="10"
android:hint="LAST NAME" />
<EditText
android:id="#+id/txtdob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtlname"
android:layout_centerVertical="true"
android:ems="10"
android:hint="DOB" />
</RelativeLayout>
The second fragment:
package pl.looksok.viewpagerdemo;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class FragmentGreen extends Fragment {
Button btnnext2,btnprev2;
RelativeLayout frag2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_green, container, false);
btnnext2=(Button) view.findViewById(R.id.btnnext2);
btnprev2=(Button) view.findViewById(R.id.btnprev2);
frag2=(RelativeLayout) view.findViewById(R.id.frag2);
btnnext2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
((MainActivity)getActivity()).selectFragment(2);
}
});
btnprev2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity)getActivity()).selectFragment(0);
}
});
return view;
}
}
The second fragment's 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="#C7F464"
android:id="#+id/frag2"
>
<Button
android:id="#+id/btnnext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="60dp"
android:text="NEXT" />
<Button
android:id="#+id/btnprev2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="68dp"
android:layout_toLeftOf="#+id/btnnext2"
android:text="PREV" />
<EditText
android:id="#+id/txtgender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/btnnext2"
android:ems="10"
android:hint="GENDER" />
<EditText
android:id="#+id/txtmaritalstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtgender"
android:layout_below="#+id/txtgender"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="MARITAL STATUS" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtoccupation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtmaritalstatus"
android:layout_below="#+id/txtmaritalstatus"
android:layout_marginTop="23dp"
android:ems="10"
android:hint="OCCUPATION" />
<EditText
android:id="#+id/txtusername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtoccupation"
android:layout_centerVertical="true"
android:ems="10"
android:hint="USERNAME" />
<EditText
android:id="#+id/txtpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtusername"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="PASSWORD"
android:inputType="textPassword" />
</RelativeLayout>
The third fragment:
package pl.looksok.viewpagerdemo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;
public class FragmentPink extends Fragment {
Button btnnext3,btnprev3;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pink, container, false);
btnnext3=(Button) view.findViewById(R.id.btnnext3);
btnprev3=(Button) view.findViewById(R.id.btnprev3);
btnnext3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
btnprev3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity)getActivity()).selectFragment(1);
}
});
return view;
}
}
The third fragment's 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="#FF6B6B">
<Button
android:id="#+id/btnnext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="NEXT" />
<Button
android:id="#+id/btnprev3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="PREV" />
<EditText
android:id="#+id/txtaddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="ADDRESS" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtdistrict"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtaddress"
android:layout_below="#+id/txtaddress"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="DISTRICT" />
<EditText
android:id="#+id/txtstate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtdistrict"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="STATE" />
<EditText
android:id="#+id/txtpincode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txtstate"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:ems="10"
android:hint="PINCODE" />
</RelativeLayout>
The adapter:
package pl.looksok.viewpagerdemo;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
this.fragments = new ArrayList<Fragment>();
fragments.add(new FragmentBlue());
fragments.add(new FragmentGreen());
fragments.add(new FragmentPink());
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
}
I also created a class which has string values.I was wondering if i could create an object and set the fields as properties for that object
The class with the fields:
package pl.looksok.viewpagerdemo;
import java.io.Serializable;
public class RegistrationValues implements Serializable {
String fname;
String mname;
String lname;
String dob;
String gender;
String marital_status;
String occupation;
String username;
String password;
String address;
String district;
String state;
String pincode;
String nationality;
String email;
String phno;
String bank_acno;
String bank_branch;
String bank_ifsc;
String bank_swift;
}
So the question is how to pass the values in the first and second fragment to the last fragment without using Sharedpreferences.
The easy approach would be to let the RegistrationValues instance reside in your MainActivity instance:
public class MainActivity extends FragmentActivity {
ViewPager pager;
RegistrationValues values; //add this
public RegistrationValues getValues() { //add this
return values; //add this
}; //add this
In your fragments, use ((MainActivity) getActivity()).getValues() to update the values and to actually get the values in the third fragment or so.
Note: You should probably save the values instance in your MainActivity's onSaveInstance and restore it in your MainActivity's onCreate.
i think that for values passing between fragments you can use listener and dispatcher patterns and singleton pattern, which will manage listeners and dispatchers, because fragments are not Activities and you can't pass values via Intents.
A very common pattern is for the fragments to provide an interface by which values may be passed to them, and to use the containing activity as a hub. To see an example of this, use Eclipse or Android Studio to instantiate a new Master/Detail Flow activity and look at the code for the ListActivity.
But I would argue that in this case, you probably don't want to do that. Your data record looks like something you want to maintain in your persistence layer (Model), maybe mutate through a service (Controller) and pull up as needed into the fragment (View). Separate UI from business logic, and use an Adapter to keep the data in sync.

Android - Controls resolve to null

I'm writing a decimal time converter for someone, and I am trying to use findViewById to access some of my controls. So, I assign each of them, and then I assign an onclick listener to a button(btnConvert). When I assign the listener, the app crashes due to the fact that btnConvert is null. I have read into this, but I haven't found a working solution yet. Please refer to my code below
MainActivity.java:
package com.alex_justi.dec2time;
import android.app.Activity;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity {
static EditText inDec;
static Button btnConvert;
static TextView outHours;
static TextView outMinutes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
inDec = (EditText)rootView.findViewById(R.id.dec);
btnConvert = (Button)rootView.findViewById(R.id.convert);
outHours = (TextView)rootView.findViewById(R.id.hours);
outMinutes = (TextView)findViewById(R.id.minutes);
return rootView;
}
}
}
fragment_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
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.alex_justi.dec2time.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Decimal Time" />
<EditText
android:id="#+id/dec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView1"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="#+id/convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/dec"
android:text="Convert" />
<TextView
android:id="#+id/minutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/hours"
android:layout_marginTop="15dp"
android:text="Minutes: "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/convert"
android:text="Hours: "
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Add the listeners in your fragment onCreateView() after you've obtained the references with rootView.findViewById().

Categories

Resources