我没有让我的片段工作...... :-(
我的活动:
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
}
}
}
我的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>
最后是我的片段:
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;
}
}
如果我执行,我会得到以下异常:
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
因此,如果我注释掉部分没有错误 - 但路径是正确的,那我做错了什么?