Unable to initialize the Context - android

I have an activity which uses an adapter and a fragment that displays a recycler list view. When the user taps on any row, I display an AlertDialog (created as a DialogFragment) for them to enter the data.
The callbacks from the AlertDialog is listened by the Fragment and once all the fields are captured in the Fragment, the completed object is sent back to the activity to save it in the database.
Here is a screenshot ...
Right now after I enter a name and hit continue, I get a crash because the listener from the DisplayTextEntryAlert class (i.e. DialogFragment) isn't initialized.
java.lang.NullPointerException: Attempt to invoke interface method 'void alerts.DisplayTextEntryAlert$DisplayTextEntryAlertListener.onYesButtonClicked(android.support.v4.app.DialogFragment, java.lang.String)' on a null object reference
at alerts.DisplayTextEntryAlert$1.onClick(DisplayTextEntryAlert.java:97)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
In my DisplayTextEntryAlert class, it crashes when mListener.onYesButtonClicked is executed.
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
....
....
....
builder.setPositiveButton(R.string.stgContinue, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mTextEntered = editTextControl.getText().toString();
mListener.onYesButtonClicked(DisplayTextEntryAlert.this, mTextEntered);
}
});
The mListener object is initialized in the 'onAttach' method in the DisplayTextEntryAlert class
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof DisplayTextEntryAlertListener) {
mListener = (DisplayTextEntryAlertListener)context;
} else {
Log.d(this.toString(),"trackContext");
Log.d(this.toString(),context.toString());
//throw new RuntimeException(context.toString() + " must implement DisplayTextEntryAlertListener");
}
}
But when debugging, I notice that this line never gets executed.
mListener = (DisplayTextEntryAlertListener)context;
This AlertDialog is created from the Fragment class (AddFriendFragment) that is launched from the activity (AddFriendActivity)
DisplayTextEntryAlert displayTextEntryAlertFragment = DisplayTextEntryAlert.newInstance("","Enter the first name");
FragmentManager fragmentManager = ((FragmentActivity) mContext).getSupportFragmentManager();
displayTextEntryAlertFragment.show(fragmentManager, "newFriendFragment"); // give it a name for retrieving
The 'mContext' in here is created from the onCreateView method in this fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_newfriend_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
mContext = view.getContext();
....
....
....
mNewFriendAdapter = new NewFriendAdapter(mNewFriendFields, this);
recyclerView.setAdapter(mNewFriendAdapter);
}
return view;
}
Question: I'm clearly missing something here else the mListener would have been initialized in the onAttach method in the DisplayTextEntryAlert class.
Any clues ?
Here is the full source code for reference
https://gist.github.com/ArdenDev/229c69f803dce62a1e46acb0e05c7f1a

Make your AddFriendActivity implement DisplayTextEntryAlertListener
Then override that functionality in AddFriendActivity
#Override
public void onYesButtonClicked(String text)
{
// Do anything with your text
}
If you need Context for Listener, use getActivity() or getContext() instead of DisplayTextEntryAlert.this
mListener.onYesButtonClicked(getActivity());
One more thing, you don't have to check instance of when inflating your view. Because I saw there is not only RecyclerView in your layout, but also ToolBar, so the view is definately not RecyclerView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_newfriend_list, container, false);
// Remove the instance of condition
mContext = view.getContext();
....
....
....
mNewFriendAdapter = new NewFriendAdapter(mNewFriendFields, this);
recyclerView.setAdapter(mNewFriendAdapter);
return view;
}

So, your problem that you use on onAttach(Content context) which was added in 23 API version (Android 6) and it never calls at lower API versions. You should override both onAttach(Context context) and onAttach(Activity activity)

So the trick was to use setFragment. This now works
DisplayTextEntryAlert displayTextEntryAlertFragment = DisplayTextEntryAlert.newInstance("","Enter the first name");
displayTextEntryAlertFragment.setTargetFragment(this, 0);
displayTextEntryAlertFragment.show(getActivity().getSupportFragmentManager(), "newFriendFragment");

Related

How to check if the custom DialogFragment is displayed?

1) In my application, the user may receive a lot of notifications from FCM
2) If the user has an application open, he needs to display the custom DialogFragment
3) If the DialogFragment is already displayed, then the next time the notification arrives, it is necessary to prohibit the repeated display of this DialogFragment
4) My dialogue code:
public final class NotificationEventDialog extends DialogFragment implements DialogInterface.OnKeyListener, View.OnClickListener {
private Activity mCurrentActivity;
private NotificationEventDialogListener mNotificationEventDialogListener;
public interface NotificationEventDialogListener {
void showEvent();
}
public NotificationEventDialog() {
}
public static NotificationEventDialog newInstance() {
NotificationEventDialog notificationEventDialog = new NotificationEventDialog();
notificationEventDialog.setCancelable(false);
return notificationEventDialog;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
mCurrentActivity = (Activity)context;
try {
mNotificationEventDialogListener = (NotificationEventDialogListener) mCurrentActivity;
} catch (ClassCastException e) {
throw new ClassCastException(mCurrentActivity.toString() + " must implemented NotificationEventDialogListener");
}
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(mCurrentActivity);
#SuppressLint("InflateParams") View view = inflater.inflate(R.layout.dialog_notification_event, null);
Button btnNotificationEventYes = view.findViewById(R.id.notification_event_dialog_yes);
btnNotificationEventYes.setOnClickListener(this);
Button btnNotificationEventNo = view.findViewById(R.id.notification_event_dialog_no);
btnNotificationEventNo.setOnClickListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(mCurrentActivity);
builder.setView(view);
return builder.create();
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setOnKeyListener(this);
}
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onDetach() {
super.onDetach();
mCurrentActivity = null;
mNotificationEventDialogListener = null;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.notification_event_dialog_yes:
dismiss();
mNotificationEventDialogListener.showEvent();
break;
case R.id.notification_event_dialog_no:
dismiss();
break;
}
}
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
dismiss();
return true;
} else return false;
}
}
5) Each time I receive a notification from FCM, I create a dialog box:
DialogFragment notificationEventDialog = NotificationEventDialog.newInstance();
notificationEventDialog.show(getSupportFragmentManager(), "");
6) How to check if DialogFragment is already displayed? Every time I create a new object of this window and I cannot make it as Singleton, because This leads to a memory leak.
Found an answer in which a person suggests using Weak links to solve this problem:
Also you can store a weak link to the shown dialog in that singletone
class. Using such method, you can detect is your dialog currently
shown or not.
There was also such an answer:
I suggest to save link to the dialog in single instance class. In that
instance create method ensureShowDialog(Context context). That method
would check is current shown dialog or not. If yes, you can show the
dialog. In another casr you can pass new data you to the dialog.
But, honestly, I can’t quite understand how to use these tips in practice. Please can help with this realization or suggest another way? Thanks in advance.
You can use:
val ft: FragmentTransaction = fragmentManager!!.beginTransaction()
val prev: Fragment? = fragmentManager!!.findFragmentByTag("typeDialog")
if (prev == null) {
val fm = fragmentManager
val courseTypeListDialogFragment =
CourseTypeListDialogFragment()
courseTypeListDialogFragment.isCancelable = false
courseTypeListDialogFragment.setStyle(
DialogFragment.STYLE_NO_TITLE,
0
)
courseTypeListDialogFragment.setTargetFragment(this, 1)
if (fm != null) {
courseTypeListDialogFragment.show(ft, "typeDialog")
}
}
You can check if dialog fragment is showing by calling isAdded () inside DialogFragment or by
DialogFragment notificationEventDialog = NotificationEventDialog.newInstance();
notificationEventDialog.isAdded()
from activity
It will return true if fragment is added to an Activity, in case of dialog fragment - is shown.
You can store last shown dialog fragment date via putting System.currentTimeMillis() in SharedPreferences
I think you'v got the idea.

Fragment,DialogFragment Issue

I am calling dialog fragment from FragmentA and returning some values to fragmentA. Now issue is whenever i go to another fragmentB from same fragmentA and return to it my dialog fragment values get cleared.
when i click on consultant doctor textview, a dialog opens (Pic 2). On Selecting an item (Pic 2),returns a value back to FragmentA. Pic 3 is a Fragment B which opens on same activity. But when i click on cross button on pic 3 and popBackStack , my value for consult doctor clears shown in Pic 4.
Pic 4 is an ISSUE
DialogFragment
#Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
getDialog().getWindow().setGravity(Gravity.CENTER);
getDialog().setCancelable(false);
getDialog().setCanceledOnTouchOutside(false);
getDialog().closeOptionsMenu();
}
#Override public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Nullable #Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
View rootView = inflater.inflate(R.layout.consultant_doc_dialog, container, false);
recyclerView = (RecyclerView)rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setHasFixedSize(true);
adapter = new ConsultantDoctAdapter(getContext(),this);
adapter.getDocList().addAll(new ArrayList<DoctorList>());
recyclerView.setAdapter(adapter);
adapter.getDocList().clear();
adapter.getDocList().addAll(list);
adapter.notifyDataSetChanged();
close = (ImageButton)rootView.findViewById(R.id.bt_close);
close.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View view) {
getDialog().dismiss();
}
});
//cityEditText.setOnQueryTextListener(onQueryTextListener);
return rootView;
}
Fragment
#Nullable #Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_create_leads, container, false);
ButterKnife.bind(this, view);
setRetainInstance(true);
init();
setPicker();
setSpinnerListener();
btCheckCalendar.setOnClickListener(this);
etCityId.setOnClickListener(this);
etConsultingDocId.setOnClickListener(this);
btSubmit.setOnClickListener(this);
tvClientReferral.setOnClickListener(this);
etSalesPerson.setText(sharedPref.getString(AppConstants.PREFERENCE_USER_NAME, ""));
etZone.setText(sharedPref.getString(AppConstants.USER_ZONE, ""));
etAreaCode.setText(sharedPref.getString(AppConstants.USER_AREA_CODE, ""));
setSpinner();
getConsultantDoctorList();
return view;
}
Fragment B callBack:
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_main, new MyCalendarFragment())
.addToBackStack("calendarFragment")
.commit();
DialogCallack:
ConsultantDocDialogFragment consultantDocDialog = new ConsultantDocDialogFragment();
consultantDocDialog.setParameter(getContext(), this, doclist);
consultantDocDialog.show(getActivity().getSupportFragmentManager(),
ConsultantDocDialogFragment.class.getSimpleName());
break;
Please help me so that i can able to save state of values got from dialog fragment.
Please find the following code it may help you-
This is Fragment Code where you can get CallBack from Dialog Fragment-
HomeFragment.java
public class HomeFragment extends Fragment implements AlertDFragment.Callback {
private static final int DIALOG_FRAGMENT = 100;
Button alertdfragbutton;
private View rootView;
public HomeFragment() {
}
public static HomeFragment newInstance() {
return new HomeFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
initUI(rootView);
return rootView;
}
private void initUI(View rootView) {
alertdfragbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
AlertDFragment alertdFragment = new AlertDFragment();
alertdFragment.setTargetFragment(HomeFragment.this, DIALOG_FRAGMENT);
// Show Alert DialogFragment
alertdFragment.show(getChildFragmentManager(), "Alert Dialog Fragment");
}
});
}
#Override
public void accept() {
Log.e("Home ", "OK");
}
#Override
public void decline() {
}
#Override
public void cancel() {
Log.e("Home ", "CANCEL");
}
}
Here is Dialog Fragment where we declare CallBack with methods-
public class AlertDFragment extends DialogFragment {
Callback callback;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
callback = (Callback) getTargetFragment();
return new AlertDialog.Builder(getActivity())
// Set Dialog Icon
.setIcon(R.drawable.androidhappy)
// Set Dialog Title
.setTitle("Alert DialogFragment")
// Set Dialog Message
.setMessage("Alert DialogFragment Tutorial")
// Positive button
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.accept();
// Do something else
//getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, getActivity().getIntent());
}
})
// Negative Button
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
callback.cancel();
// Do something else
// getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, getActivity().getIntent());
}
}).create();
}
public static interface Callback {
public void accept();
public void decline();
public void cancel();
}
}
A simple way to return values from DialogFragment is using setTargetFragment for calling a fragmentB creation, then return data to getTargetFragment (if not null). In fragmentA you can receive data through onActivityResult.
Another way is using SharedPreferences. You can get a new value with onResume or onHiddenChanged.
Instead of using the "Fragment Transition" why don't you just POP-UP your custom view
Just Create a global reference of
Dialogue dialogue
View popupView
and on click of whatever textview button etc.
you can just call a method like
void popup(){
popupView = LayoutInflater.from(getActivity()).inflate(R.layout.your_calenderlayout, null);
//suppose you have TextView cal_textview in popUp view i.e, your_calenderlayout
cal_textview = (TextView ) popupView.findViewById(R.id.cal_textview);
dialog = new Dialog(getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(popupView); //and just add your popUpview
// For setting backgroung
/*dialog.getWindow().setBackgroundDrawableResource(android.R.color.Transparent);
*/
//For setting the width or height of popup
/*WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);*/
dialog.show();
}
and on dismiss of popUp or on click of the view inside popupView you set the value of variables or member inside the fragment directly
Hope this will help
You can use Shared prefs or sqlite to get your values and if you think its use less to save your temporary data in share prefs or sqlite Then Singleton model is a good option .. i believe we should follow KISS
design principle :P

Default constructors in fragments

I don´t have so many experiences with fragments at the moment so i hope anybody can help me with this ! Before the app release compiled fine some month ago and now i have this problem
Android Studio says:
"Problem avoid not default constructors in fragments. "
I hope anybody can help me here to solve this problem.
public static class TagClickDialog extends DialogFragment {
private final TagClickDialogListener mListener;
private final Context mContext;
private final Tag[] mTags;
public TagClickDialog(Context context, TagClickDialogListener listener, Tag[] tags) {
mListener = listener;
mContext = context;
mTags = tags;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String[] dialogItems = new String[mTags.length];
for (int i=0; i<mTags.length; i++) {
dialogItems[i] = mTags[i].getValue();
}
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(getResources().getString(R.string.tags))
.setItems(dialogItems, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
mListener.onTagClick(mTags[which]);
}
});
return builder.create();
}
}
From Activity you can send data to Fragment with intent as:
Bundle bundle = new Bundle();
bundle.putString("message", "message is hi");
//set Fragmentclass Arguments
Fragmentclass fragobj=new Fragmentclass();
fragobj.setArguments(bundle);
and to receive in fragment in Fragments onCreateView method:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// get message from activity
String strtext=getArguments().getString("message");
return inflater.inflate(R.layout.fragment, container, false);
}
This is working fine for me. This may help you.
Every fragment must have an empty constructor, so it can be
instantiated when restoring its activity's state. It is strongly
recommended that subclasses do not have other constructors with
parameters, since these constructors will not be called when the
fragment is re-instantiated; instead, arguments can be supplied by the
caller with setArguments(Bundle) and later retrieved by the Fragment
with getArguments().
Due to this reason we have static factory method when we want an instance of that fragment newInstance() in case you want it to have some information instantiated.
check this out
Update
you can use following code
public static class TagClickDialog extends DialogFragment {
private TagClickDialogListener mListener;
private Context mContext;
private Tag[] mTags;
public TagClickDialog() {
}
**update**
#Override
public void onAttach(Context context) {
super.onAttach(context);
mListener = (TagClickDialogListener )context;
}
public static TagClickDialog newInstance(Context context, Tag[] tags) {
mContext = context;
mTags = tags;
return this;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String[] dialogItems = new String[mTags.length];
for (int i=0; i<mTags.length; i++) {
dialogItems[i] = mTags[i].getValue();
}
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(getResources().getString(R.string.tags))
.setItems(dialogItems, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
mListener.onTagClick(mTags[which]);
}
});
return builder.create();
}
}
}
this is how you can use it
TagClickDialog fragment = TagClickDialog.newInstance("put all the parameters");
fragment.show(fragmentManager, "some tag");
If you are creating a non-default constructor for a Fragment, you will be getting this error
Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead.
Of course this means you have to remove non-default constructors.
WHAT I USUALLY DO IS
1. Use Bundle to pass data [Unless it's an Object, in which case you have to Serialize it] along with a key, so that in the Fragment, you can get the Bundle via the key
Bundle args = new Bundle();
args.putLong("key", value);
fragment.setArguments(args);
Inside the Fragment, you retrieve the argument as
getArguments().getType("key");
2. But sometimes I create a setter methods inside Fragment to set the data.
public void setData(int value) {
this.value = value;
}
And when I initialise the Fragment, I call this method right away.
MyFragment fragment = new MyFragment();
fragment.setData(5);

Exception when showing a dialog after orientation change

I have an activity and a fragment inside it.inside fragment, there is a button, and on click of button a dialog shows.
Everything works, until user do a orientation change and click button after it.
IllegalStateException(cannot perform this action after onsaveinstancestate) occurs when user clicks button after orientation change. I'm using android support framework.
Anybody have any idea regarfing this?
Activity Code
public void openMoreDialog(String shareData, String link) {
DialogFragment dialog = new MoreDialog(shareData, link);
dialog.show(getSupportFragmentManager(), "MoreDialog");
}
Fragment Code
public void onAttach(Activity activity) {
super.onAttach(activity);
mControl = (ActivityControl)activity;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ImageButton moreButton = (ImageButton)v.findViewById(R.id.moreButton);
moreButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mControl.openMoreDialog(shareData, link);
}
});
return rootView;
}
FragmentDialog code
public class MoreDialog extends DialogFragment {
private String mShareData;
private String mLink;
public MoreDialog(String shareData, String link){
mShareData = shareData;
mLink = link;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.more_dialog, null);
Button openBtn = (Button)dialogView.findViewById(R.id.openBtn);
openBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openLink(mLink);
}
});
Button shareBtn = (Button)dialogView.findViewById(R.id.shareBtn);
shareBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
shareNews(mShareData);
}
});
builder.setView(dialogView);
return builder.create();
}
private void openLink(String link){
}
private void shareNews(String data){
}
}
Helpful link & solution how to:
https://stackoverflow.com/a/17413324/619673 and btw, constructor in fragment must be empty! Documentation:
http://developer.android.com/reference/android/app/Fragment.html
public Fragment ()
Added in API level 11
Default constructor.
Every fragment must have an empty constructor, so
it can be instantiated when restoring its activity's state. It is
strongly recommended that subclasses do not have other constructors
with parameters, since these constructors will not be called when the
fragment is re-instantiated; instead, arguments can be supplied by the
caller with setArguments(Bundle) and later retrieved by the Fragment
with getArguments().
Applications should generally not implement a constructor. The first
place application code an run where the fragment is ready to be used
is in onAttach(Activity), the point where the fragment is actually
associated with its activity. Some applications may also want to
implement onInflate(Activity, AttributeSet, Bundle) to retrieve
attributes from a layout resource, though should take care here
because this happens for the fragment is attached to its activity.

Activity to SherlockFragment

I want to change my app ( extends Activity ) to Fragment ( extends SherlockFragment )
If I change it I have much errors;
public class AlarmClock extends SherlockFragment implements OnClickListener {
This is my onCreateView:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// sanity check -- no database, no clock
if (getContentResolver() == null) {
new AlertDialog.Builder(this)
.setTitle(getString(R.string.error))
.setMessage(getString(R.string.dberror))
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
finish();
}
})
.setOnCancelListener(
new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
finish();
}
}).setIcon(android.R.drawable.ic_dialog_alert)
.create().show();
return;
}
View view = inflater.inflate(R.layout.alarm_clock, container, false);
// menu buttons
add = (ImageButton) findViewById(R.id.ibAdd);
snooze = (ImageButton) findViewById(R.id.ibSnooze);
add.setOnClickListener(this);
snooze.setOnClickListener(this);
mFactory = LayoutInflater.from(this);
mPrefs = getSharedPreferences(PREFERENCES, 0);
mCursor = Alarms.getAlarmsCursor(getContentResolver());
mAlarmsList = (ListView) findViewById(R.id.alarms_list);
mAlarmsList.setAdapter(new AlarmTimeAdapter(this, mCursor));
mAlarmsList.setVerticalScrollBarEnabled(true);
mAlarmsList.setItemsCanFocus(true);
mClockLayout = (ViewGroup) findViewById(R.id.clock);
mQuickAlarm = findViewById(R.id.ibSnooze);
mQuickAlarm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showQuickAlarmDialog();
}
});
setVolumeControlStream(android.media.AudioManager.STREAM_ALARM);
setQuickAlarmVisibility(mPrefs.getBoolean(PREF_SHOW_QUICK_ALARM, true));
return view;
}
There are a lot of errors because there is no Activity.
If is Activity it works.
I use "extends SherlockFragment" because I want to add it to the table.
How fix this problem ? Please help me.
If am right, Fragments must definitely be used in an Activity.
So instead of using this use getActivity(); to get the Activity(which uses this fragment) Context.
something like:
getActivity.finish();
and in case of findViewById(//some Id);
use it like this:
inflatedView.findViewById(//Id);
A Fragment is not a Context (unlike Activity or Application). So quite a few methods are not available to it.
It however has access to the context it is attached to. Usually, you can call getActivity() within the fragment to get it. You should check if the Fragment is part of the activity by using the isAdded() method.
You should do some reading about Fragments, porting existing activities to use Fragments, ... tutorials are available using Google.

Categories

Resources