Reputation: 7415
I don't get my fragment to work... :-(
My Activity:
public class Test extends SherlockActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setTitle(R.string.title);
// actionBar.setDisplayShowTitleEnabled(false);
//
actionBar.setDisplayShowHomeEnabled(false);
Tab tab = actionBar.newTab().setText(dayName(Calendar.MONDAY))
.setTabListener(new WeekDayTabListener(getApplicationContext()));
actionBar.addTab(tab);
...
tab = actionBar.newTab().setText(dayName(Calendar.SUNDAY))
.setTabListener(new WeekDayTabListener(getApplicationContext()));
actionBar.addTab(tab);
}
public static class WeekDayTabListener implements TabListener {
private Context context;
public WeekDayTabListener(Context context) {
this.context = context;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Tools.longToast(tab.getText().toString(), this.context);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
}
my 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" >
<fragment
android:id="@+id/weekDayFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.test.fragment.DayFragment" >
</fragment>
</LinearLayout>
and finally my Fragment:
import com.actionbarsherlock.app.SherlockFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class DayFragment extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Context c = getActivity().getApplicationContext();
LinearLayout l = new LinearLayout(c);
TextView tv = new TextView(c);
tv.setText("foo");
l.addView(tv);
return l;
}
}
If I execute, I get the following exception:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.fragment/com.test.fragment.Test}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
So if i comment out the parte there is no error - but the path is correct, so what am i doing wrong?
Upvotes: 1
Views: 9048
Reputation: 121
Your class Test needs to extend SherlockFragmentActivity
not SherlockActivity
:
class Test extends SherlockFragmentActivity{
...
}
Upvotes: 7
Reputation: 7415
Aaah...
After wasting three hours trying to change my (package)names to whatever, inflating different views reading beginner tutorials over and over the solution was soooo simple:
Removing android:layout_marginTop="?android:attr/actionBarSize" did the job...
Upvotes: -1
Reputation: 28152
Try android:name="com.test.fragment.DayFragment"
instead of class="com.test.fragment.DayFragment"
in your xml.
Upvotes: 0
Reputation: 2061
In your fragments onCreateView()
method you need to inflate a layout.
For example
.....
myInflater = inflater;
TextView tv;
tv = inflater.inflate(R.layout.your_fragment_layout_here);
.....
From here you can then add your linear layout to your view.
Please not that this is only pseudo code and you will have to play around with the syntax to get it working.
Upvotes: 1