Trouble with EditText inside a (Sherlock)Fragment - android

Hey Everyone,
I am having some trouble with my edit texts in my Fragment. I get a NUllPointerException on the EditText.getText().toString(). I know there are a few similar threads on this but I have had not luck fixing my problem. I have tried putting the (EditText) getView().findViewById(...) both the in the onCreateView() and in the onActivityCreated() and still I get the NullPointerException.
I have attached (what I think is) the relevant code. Let me know if you need to see any more of the code.
The NullPointerException is in the Intent createShareIntent() method. I have commented the line where I get the exception.
public class EditNoteFragment extends SherlockFragment {
Long mCurrentPosition;
EditText title;
EditText body;
// in a fragment class this does not do anything.
// it is mostly here to register for the menu and context menu. that is all
// seems silly :/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getLong("RowId");
}
View view = inflater.inflate(R.layout.edit_note, container, false);
title = (EditText) view.findViewById(R.id.title);
//title.setText("");
body = (EditText) view.findViewById(R.id.body);
// body.setText("");
return view;
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuShare = menu.findItem(R.id.menu_share);
ShareActionProvider shareAction = (ShareActionProvider)
menuShare.getActionProvider();
shareAction.setShareIntent(createShareIntent());
}
protected Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title.getText().toString()); // From logcat I can see the NullPointerException here
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, body.getText().toString());
return shareIntent;
}
As requested the relevant xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="#string/titlehint"
android:inputType="textCapSentences"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<EditText
android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_margin="5dp"
android:layout_weight="1"
android:fadeScrollbars="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="top"
android:hint="#string/noteshint"
android:inputType="textMultiLine"
android:scrollbars="vertical" />
Any help would be appreciated.
Cheers.

Simply do this.
if(title!=null){
final String text = title.getText();
if (!TextUtils.isEmpty(text)) {
// creates the intent and fires it...
} else {
// shows an error message that the text is required.
}
}
Is is bound to work

The method of EditText.getText() retrieves an object of kind Editable only if the edit text contains any text field inside (Editable is the product of the text filled by the user in the box with graphical properties).
You should do the following:
final String text = title.getText() != null ? title.getText().toString() : null;
if (!TextUtils.isEmpty(text)) {
// creates the intent and fires it...
} else {
// shows an error message that the text is required.
}
Hope it helps.. :)

Related

Inflating same view through another xml file. Do I need unique id?

This is my initial layout of which the following EditText with id: assign_edit and weight_edit are part of. The name of this layout is new_class_container.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/assign_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="#string/example_assign"
android:singleLine="true"
android:inputType="textCapWords"
android:layout_weight="1">
</EditText>
<EditText
android:id="#+id/weight_edit"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="#string/example_weight"
android:layout_marginLeft="3dp"
android:gravity="center_horizontal"
android:inputType="number"
android:singleLine="true">
</EditText>
</LinearLayout>
The layout that I want to inflate is named new_class. In this layout, I copy pasted the exact same EditText's mentioned above. The following is my java file
public class DefiningClass extends Activity {
private ViewGroup mContainerView;
private EditText assignName;
private EditText assignWeight;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.new_class_container);
mContainerView = (ViewGroup) findViewById(R.id.container);
}
#Override
public boolean onCreateOptionsMenu(Menu add){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, add);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.btn_save:
//INSERT INSTRUCTIONS TO SAVE
String str = assignName.getText().toString();
Toast msg = Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG);
msg.show();
break;
case R.id.btn_add:
//findViewById(android.R.id.empty).setVisibility(View.GONE);
addItem();
break;
default:
return true;
}
return super.onOptionsItemSelected(item);
}
private void addItem() {
final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
R.layout.new_class, mContainerView, false);
assignName = (EditText) newView.findViewById(R.id.assign_edit);
assignWeight = (EditText) newView.findViewById(R.id.weight_edit);
mContainerView.addView(newView, 0);
}
}
I put a Toast in my btn_save to see the work it does. When I run it, it only shows the most recent EditText. How can I make it so the toast recognizes all the edittexts that I have "inflated" and placed in it. For instance, if I had three edittexts, first said Hello, 2nd, Welcome, 3rd, Bye. The toast only shows Bye and not the other two.
What can I do for it to recognize the other two? When I save it in the future I want all the edittexts to be saved.
Use
<EditText
android:id="#+id/assign_edit"/> // missing #+
Your findViewById looks for a view with the id in the current infalted layout.
So having the same ids in different layout xml files is not a problem.
Check the topic id in the below link
http://developer.android.com/guide/topics/ui/declaring-layout.html
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).

How do I open an image by clicking on a text

i am making my very first app and i need some help please.
How do i open an image which is already preset in my app by clicking on a hyperlinked text?
I am trying to
For example,
"Refer to Image001"
when the user taps on the word "Image001", a window opens up the preset picture and it closes when i press the back button.
This is what i have so far
In strings.xml
<string name="refer">Refer to Image001</string>
In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="left"
android:text="#string/refer" />
</ScrollView>
</LinearLayout>
thanks
You will have to have 2 TextViews. The first one will have the text: "Refer to " and the second one will have the text "Image001". Then you set a click listener on the second textview with code like this:
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
you must first set Id to your textView in xml file like below:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="left"
android:text="#string/refer" />
then set setOnClickListener for your textview like following code:
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//DO you work here
}
});
after doing that in onClick method you can call you image like following code
String url = "http://www.xxx.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
The best way to do this is to use DialogFragments. For example, this class extends DialogFragment and displays a picture inside ImageView.
public class MyDialogFragment extends DialogFragment {
public MyDialogFragment newInstance (Bitmap targetPicture) {
MyDialogFragment frag = new MyDialogFragment();
Bundle args = new Bundle();
args.putExtra("picture", targetPicture);
frag.setArguments(args);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.dialogfrag, container, false);
Bitmap b = (Bitmap) getArguments().getExtra("picture");
ImageView iv = (ImageView) v.findViewById(R.id.targetImageView);
iv.setImageBitmap(b);
return v;
}
However, you need to define dialogfrag.xml inside your layout folder, like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/targetImageView" />
</LinearLayout>
And finally inside your activity:
OnClickListener myClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
MyDialogFragment d = MyDialogFragment.newInstance(targetPicture) //replace targetPicture with the picture you want to display
d.show();
}
};
TextView tv = (TextView) findViewById(R.id.txt1);
tv.setOnClickListener(myClickListener);

java.lang.IllegalStateException: The specified child already has a parent. (After editing my post)

building android,location based application. I have my class that initiate google maps api work (MygoogleMapsActivator). this is constractor of this class
public MyGoogleMapsActivator(Context mContext)
{
this.mContext = mContext;
}
.
Context mContext is a activity where i need to display map and address,a pass it here for getting services that i need to activate.Activity displaying the results - my location and address(location and address i am getting from MyGoogleMapsActivator)
when i want to display it on my activity i get this exception in LogCat: Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
i am getting ti from this function:
public void SetUpTextViewWithAddress()
{
text = (TextView) findViewById(R.id.address);
text.append(addressString);
setContentView(text);
}
How can i solved it,thanks.
This is full code of my activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_place);
mGoogleMapsActivator = new MyGoogleMapsActivator(this);
Button buttonAddPlace = (Button) findViewById(R.id.buttonAddPlace);
Button buttonAddByAddress = (Button) findViewById(R.id.buttonAddByAddress);
fm = (SupportMapFragment ) getSupportFragmentManager().findFragmentById(R.id.map);
buttonAddPlace.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(AddNewPlaceActivity.this, AddByAddress.class);
mMyPlace = new Places("", city, street, number, "", LoggedWithFacebookMainActivity.GetLoggedInUserName(), myPosition.latitude, myPosition.longitude);
intent.putExtra("Place", mMyPlace);
//intent.putExtra("TheStreet", street);
//intent.putExtra("TheNumber", number);
intent.putExtra("Clarification");
startActivity(intent);
}
});
buttonAddByAddress.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(AddNewPlaceActivity.this, AddByAddress.class);
mMyPlace = new Places("", city, street, number, "", LoggedWithFacebookMainActivity.GetLoggedInUserName(), myPosition.latitude, myPosition.longitude);
intent.putExtra("Place", mMyPlace);
// intent.putExtra("TheStreet", "Street");
// intent.putExtra("TheNumber", "Number");
intent.putExtra("Clarification");
startActivity(intent);
}
});
mGoogleMapsActivator.SetUpMapIfNeed(fm);
mGoogleMapsActivator.SetMyGoggleActivatorLocationEnabled();
mGoogleMapsActivator.SetUpService();
mGoogleMapsActivator.setUpListener();
mGoogleMapsActivator.SetUpAndGetProvider();
mGoogleMapsActivator.SetMyLocationWithListener();
mGoogleMapsActivator.SetMyPosition();
mGoogleMapsActivator.SetUpMyMarker();
mGoogleMapsActivator.SetUpMapView();
mGoogleMapsActivator.SetUpAddressFromGeocoder();
city = mGoogleMapsActivator.getCity();
street = mGoogleMapsActivator.getStreet();
number = mGoogleMapsActivator.getNumber();
addressString = mGoogleMapsActivator.getAddressString();
SetUpTextViewWithAddress();
mGoogleMapsActivator.AnimateCamera();
public void SetUpTextViewWithAddress()
{
text = (TextView) findViewById(R.id.address);
text.setText(addressString);
// setContentView(text);
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="end"
tools:context=".AddNewPlaceActivity" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="370dp" />
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/buttonAddByAddress"
android:layout_alignParentLeft="true"
android:layout_below="#+id/map"
android:layout_toLeftOf="#+id/buttonAddPlace"
android:text="#string/place_address_he" />
<Button
android:id="#+id/buttonAddPlace"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonAddByAddress"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/address"
android:background="#drawable/custom_button_main"
android:paddingBottom="10dp"
android:text="#string/add_place_he" />
<Button
android:id="#+id/buttonAddByAddress"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/custom_button_main"
android:paddingTop="10dp"
android:text="#string/add_place_manually_he" />
</RelativeLayout>
Thanks
The issue is the TextView is already attached to your view hierarchy. I'm guessing the view with address id is already inside the main layout file you are using for your activity, without the full activity code it's hard to tell. If this is true you should remove the setContentView call as it isn't needed. Make sure you want to append the text to what may already be in the TextView, if you want to replace the text you should use setText instead of append.

Support ListFragment issue

I'm currently using a ListFragment together with an ExpandableListView to show some data backed by a SimpleCursorTreeAdapter. Everything works fine, but I recently switched to the support.v4 package, to make use of the ViewPager class to swipe between tabs. Swiping and all the other classes that now use the support.v4.Fragment work fine, but my ListFragment has stopped working.
There are no exceptions thrown, but the ListFragment simply doesn't show any items.
This is the code for the ListFragment:
public class VisuTextFragment extends ListFragment {
private Storage mStorage;
private int mFilterSensortype;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mStorage = Storage.newSQLiteDatabase(getActivity());
mFilterSensortype = -1;
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.l_visu_text, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
fillData();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.textvis, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_filter:
final Dialog dialog = new Dialog(getActivity());
dialog.setTitle("Filter by sensor type");
dialog.setContentView(R.layout.l_dialog_filter);
Button ok = (Button) dialog.findViewById(R.id.filter_ok);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int filter = Integer.parseInt(((EditText) dialog.findViewById(R.id.et_filter)).getText().toString());
mFilterSensortype = filter;
fillData();
dialog.dismiss();
}
});
Button cancel = (Button) dialog.findViewById(R.id.filter_cancel);
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.cancel();
}
});
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
break;
}
return true;
}
#Override
public void onResume() {
super.onResume();
fillData();
}
public void fillData() {
Log.d("VisuTextFragment", "fillData()");
Cursor cursor;
if (mFilterSensortype == -1)
cursor = mStorage.queryAllAsCursor();
else
cursor = mStorage.query(mFilterSensortype);
TextVisCursorAdapter adapter = new TextVisCursorAdapter(
getActivity(),
cursor,
R.layout.l_visu_text_group,
new String[] { Storage.ELEMENT_ID, Storage.ELEMENT_ENTRIES_DATE, Storage.ELEMENT_ENTRIES_LATITUDE, Storage.ELEMENT_ENTRIES_LONGITUDE, Storage.ELEMENT_ENTRIES_SENSORTYPE },
new int[] { R.id.id, R.id.date, R.id.latitude, R.id.longitude, R.id.sensortype },
R.layout.l_visu_text_child,
new String[] { Storage.ELEMENT_MEASUREMENTS_VALUE },
new int[] { R.id.value });
ListView lv = (ListView) getListView();
ExpandableListView elv = (ExpandableListView) lv;
elv.setGroupIndicator(null);
elv.setAdapter(adapter);
}
}
And this is the layout that I'm using (don't know if that helps):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/id"/>
<TextView
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="#string/date"/>
<TextView
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="#string/latitude"/>
<TextView
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="#string/longitude"/>
<TextView
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/sensortype"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ExpandableListView android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transcriptMode="normal"/>
<TextView android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/no_entries"/>
</LinearLayout>
</LinearLayout>
Just FYI: The TextView's in the layout file are shown, but the list itself is just missing. Not even the TextView for an empty list is shown.
Hope you can help.
EDIT: I have checked for the ExpandableListView's width and height via their corresponding methods and both return a value of 0. Its getCount() method returns 347. So the View definitely exists and is filled properly, but it is for some weird reason just not drawn to the screen.
EDIT2: Ok I fixed the problem. The problem was that the LinearLayout that hosted the TextViews on top of the actual list had its layout_height attribute set to fill_parent, which strangely was no issue for the non-support version as well as the composer in eclipse, since they both worked that way and I didn't even notice that it was set to fill_parent.
getListView() in a ListFragment is specifically looking for a listview id of #id/android:list. I'm not sure that adding the "+" in there like you did would have an effect or not, but it's the first thing I would try.
You also note you switched to the support library... did you switch all the appropriate method calls? For example, instead of getFragmentManager you would need to use getSupportFragmentManager and instead of using an Activity to control the fragments, you would need to use FragmentActivity, etc.
I think that that its butter to use android:id="#android:id/list" instead of android:id="#+id/android:list", also, in your case its really useless to extend ListFragment, just use Fragment and use findViewById for your expandableList.
Can you change:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
to:
public void onActivityCreated(Bundle aSavedInstanceState) {
super.onActivityCreated(aSavedInstanceState);
fillData();
}
make sure that your fillData method is called.
Ok I fixed the problem. The problem was that the LinearLayout that hosted the TextViews on top of the actual list had its layout_height attribute set to fill_parent, which strangely was no issue for the non-support version as well as the composer in eclipse, since they both worked that way and I didn't even notice that it was set to fill_parent.

DialogFragment not floating, acts embeded or as another fragment

I have this app, that I created a custom dialog for. I must of goofed something up cause while the .show call on the dialog does indeed bring it up, it looks like a whole new fragment and it is not floating but instead replacing the ui with its contents. I did see in their help for DialogFragment:
http://hi-android.info/docs/reference/android/app/DialogFragment.html#Lifecycle
that one can embed a dialog as a regular fragment or not. Though I am not doing anything to do this so I cannot figure out why its acting like an embedded fragment and not floating. After thinking on it, is it the way I defined my XML definition? The dialogfragment example above didn't really give a definition for the xml layout, so maybe that is where my issue is? (Even added the gravity to the xml file, still no dice)
My xml definition for this Dialog is here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:text = "Location:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"/>
<Spinner
android:id="#+id/location_spinner"
android:layout_width = "450sp"
android:layout_height="wrap_content"/>
<!-- fill out the data on the package total cost etc -->
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="#+id/location_dlg_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Okay"/>
<Button android:id="#+id/location_dlg_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
<Button android:id="#+id/location_dlg_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create new..."/>
</LinearLayout>
</LinearLayout>
Like I said displays just fine, the code for the fragment:
package com.viciousbytes.studiotab.subactivities.dialogfragments;
import ... ...
public class LocationPicker extends DialogFragment {
ArrayList<Location> mLocations;
public static LocationPicker newInstance()
{
LocationPicker loc = new LocationPicker();
return loc;
}
private void setLocations(ArrayList<Location> loc)
{
mLocations=loc;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Pick a style based on the num.
int style = DialogFragment.STYLE_NORMAL, theme = android.R.style.Theme;
setStyle(style, theme);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.location_dialog, container, false);
Spinner spinner = (Spinner)v.findViewById(R.id.location_spinner);
ArrayAdapter<Location> adapter = new ArrayAdapter<Location>(v.getContext(), android.R.layout.simple_spinner_item, mLocations);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if(mLocations==null)
spinner.setPrompt("No Locations");
else
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new LocationSelectedListener());
// Watch for button clicks.
Button newBtn = (Button)v.findViewById(R.id.location_dlg_new);
newBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
// Cancel do nothing dismissthis
Button cancelBtn = (Button)v.findViewById(R.id.location_dlg_cancel);
cancelBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
// okay button means set listener with the selected location.
Button okBtn = (Button)v.findViewById(R.id.location_dlg_ok);
okBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
//create new start that activity...
}
});
return v;
}
}
It is called from a fragment itself? though does that matter? because I am calling a TimePIckerDialog and a DatePickerDialog and those work fine, but my calling code from my other fragment is:
void showLocationDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("locpicker");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = LocationPicker.newInstance();
newFragment.show(ft, "locpicker");
}
Your constructors are wrong. Try to have just one static method newInstance to instantiate the fragment for all cases and use a Bundle to store the arguments that you want to use in the fragment. Refer to Basic Dialog section here and extend it to your case.

Categories

Resources