2

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.

2
  • what errors? Context errors?? Commented Oct 21, 2012 at 10:22
  • The method getContentResolver() is undefined for the type AlarmClock The method findViewById(int) is undefined for the type AlarmClock The method finish() is undefined for the type new DialogInterface.OnClickListener(){} etc.. Commented Oct 21, 2012 at 10:28

2 Answers 2

3

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); 
Sign up to request clarification or add additional context in comments.

1 Comment

inflatedView cannot be resolved. I must use Fragments because I need use this in Tabs. This is my main Activity ( Launcher ) public class ABSTabsViewPagerActivity extends SherlockFragmentActivity
1

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.

1 Comment

how to use onCreateOptionMenu for ActionBarSherlock when extending SherlockFragment? @MarvinLabs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.