Android 2.1:由以下原因引起:java.lang.ClassNotFoundException:Fragment
我创建了一个需要两个片段的应用程序,所以我声明了一个片段类,并在该类中创建了另一个静态片段类。我将main.xml改为Android 2.1:由以下原因引起:java.lang.ClassNotFoundException:Fragment,java,android,android-fragments,Java,Android,Android Fragments,我创建了一个需要两个片段的应用程序,所以我声明了一个片段类,并在该类中创建了另一个静态片段类。我将main.xml改为 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <f
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="@+id/titles"
android:layout_width="25dp"
android:layout_height="wrap_content"
class="frag.demo.DActivity$TFragment" />
<fragment
android:id="@+id/details"
android:layout_width="25dp"
android:layout_height="wrap_content"
class="frag.demo.DActivity$DFragment" />
</FrameLayout>
05-25 14:39:45.264:E/AndroidRuntime(911):。。。还有11个
我的JFragmentActivity类扩展了FragmentActivity。
所以请告诉我代码哪里出错了
我的活动课
public class DActivity extends FragmentActivity {// from android.support.v4.app.*;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("DA","active");
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
// If the screen is now in landscape mode, we can show the
// dialog in-line with the list so we don't need this activity.
finish();
return;
}
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
DFragment details = new DFragment();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
}
public static class TFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
String nos[]={"1","2","3","4","5"};
public TFragment(){
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Log.i("TF","active");
setListAdapter(new ArrayAdapter<String>(getActivity(),R.layout.simple_list,nos));
View detailsFrame = getActivity().findViewById(R.id.nos);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null) {
// Restore last state for checked position.
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDualPane) {
// In dual-pane mode, the list view highlights the selected item.
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// Make sure our UI is in the correct state.
showDetails(mCurCheckPosition);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
void showDetails(int index) {
mCurCheckPosition = index;
if (mDualPane) {
// We can display everything in-place with fragments, so update
// the list to highlight the selected item and show the data.
getListView().setItemChecked(index, true);
// Check what fragment is currently shown, replace if needed.
DFragment details = (DFragment)
getFragmentManager().findFragmentById(R.id.nos);
if (details == null || details.getShownIndex() != index) {
// Make new fragment to show this selection.
details = DFragment.newInstance(index);
// Execute a transaction, replacing any existing fragment
// with this one inside the frame.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.nos, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
// Otherwise we need to launch a new activity to display
// the dialog fragment with selected text.
Intent intent = new Intent();
intent.setClass(getActivity(), DActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}
public static class DFragment extends Fragment {
String names[]={"aaa","bbb","ccc","ddd","eee"};
public DFragment() {
// TODO Auto-generated constructor stub
}
public static DFragment newInstance(int index){
DFragment f= new DFragment();
Bundle args=new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.i("DF","active");
if (container == null) {
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(names[getShownIndex()]);
return scroller;
}
}
}
如果第二个片段位于第一个片段内,则第二个片段的class属性应如下所示:
class="frag.demo.TFragment$DFragment"
谢谢:)您应该将您的活动添加到清单文件中。查看其中的示例代码 如果上面的代码仍然正确,那么主活动JFragmentActivity会尝试通过xml加载DActivity的片段,但这是不可能的,因为这些片段与DActivity绑定。所以DActivity是唯一能够调用它们的活动 因此,您的
/*意图*/
是正确的。将main.xml
文件的FrameLayout
应用到一个新的DActivity
xml文件,并在创建时加载它,然后在JFragmentActivity
中调用意图。在你的JFragmentActivity
中放入一些随机LinearLayout
,然后运行应用程序
片段的class属性应该如下所示
class=“frag.demo.DActivity$DFragment”
您的标题意味着您试图在2.1上运行此代码,如果是这样,那就是您的问题
Fragment类是在API level 11(即Android 3.0)中添加的,因此除非您有蜂窝平板电脑或ICS手持设备或平板电脑,否则您将无法运行它。现在我已将第二个片段更改为具有静态函数的单独类,并且该静态函数由第一个片段使用……Fragment类可以是静态的吗,如果是这样,那么哪个类应该持有该片段类。我在这个链接中看到了一个,您是在活动类中声明这个片段类,还是在静态片段类中声明这个片段类?因为,正如文档所述:“片段与它所处的活动紧密相连,不能单独使用。”片段类必须是公共静态类。如果您像我所说的那样声明片段类,那么请使用上面的class属性:class=“frag.demo.YourActivity$DFragment”是的,我将我的2个片段类作为静态对象放入一个名为DActivity类的活动中。正如您所说,我更改了main.xml class=“frag.demo.DActivity$TFragment”不,我得到了错误05-25 15:56:32.764:E/AndroidRuntime(1169):java.lang.RuntimeException:无法启动活动组件信息{frag.demo/frag.demo.JFragmentActivity}:android.view.InflateException:二进制XML文件行#5:膨胀类片段时出错,原因是:android.view.InflateException:二进制XML文件行#5:膨胀类片段E/AndroidRuntime时出错(1169):由加载程序dalvik.system中的java.lang.ClassNotFoundException:android.view.fragment引起。PathClassLoader@44c066f005-29 10:42:09.405:E/AndroidRuntime(304):java.lang.NoClassDefFoundError:frag.demo.DActivity我收到了这个错误,ofc将DActivity添加到您的清单中^^对于延迟的回答很抱歉您的权利是在honeycomb中添加了fragment类,但是以前的API通过class@i2v2nr20i,但我们也支持2.1中的Fragment类
public class JFragmentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("main","active");
setContentView(R.layout.main);
/*Intent i = new Intent(getApplicationContext(), DActivity.class);
startActivity(i);*/
}
}
class="frag.demo.TFragment$DFragment"