Android 将活动转换为片段
我正在制作一个应用程序,希望将堆栈视图添加到其中。 我想将其添加到片段中,而不是活动中 当我从NavDrawer中选择片段时,我得到了错误Android 将活动转换为片段,android,android-fragments,Android,Android Fragments,我正在制作一个应用程序,希望将堆栈视图添加到其中。 我想将其添加到片段中,而不是活动中 当我从NavDrawer中选择片段时,我得到了错误 at android.support.v4.app.Fragment.getResources(Fragment.java:639) at <package>.EventsFragment.<init>(EventsFragment.java:20) at <package>.DrawerA
at android.support.v4.app.Fragment.getResources(Fragment.java:639)
at <package>.EventsFragment.<init>(EventsFragment.java:20)
at <package>.DrawerAdapter$ViewHolder.onClick(DrawerAdapter.java:68)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
stack_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<StackView
android:id="@+id/stackView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"></StackView>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:textColor="#FFFFFF" />
使用
View
而不是RelativeLayout
并使用getActivity().getApplicationContext()
而不是super.getActivity()
视图=充气机。充气(R.layout.events\u布局,容器,false);
stackView=(stackView)view.findViewById(R.id.stackView1);
列表=新的ArrayList();
//向列表中添加项目
对于(int i=0;i
错误消息是什么?您仅显示邮件中的行号。。我认为这是支持库的问题。当我单击NavDrawerCheck中的事件时,应用程序强制退出。我已更新,使用AdPatterNot中的getActivity()。getApplicationContext()
。java.lang.IllegalStateException:片段事件片段{9570ee0}未附加到活动添加了在收到单击时如何调用我的片段
public class Stack_Adapter extends BaseAdapter {
ArrayList<Stack_Items> arrayList;
LayoutInflater inflater;
ViewHolder holder = null;
public Stack_Adapter(Context context, ArrayList<Stack_Items> arrayList) {
this.arrayList = arrayList;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Stack_Items getItem(int pos) {
return arrayList.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int pos, View view, ViewGroup parent) {
if (view == null) {
view = inflater.inflate(R.layout.stack_layout, parent, false);
holder = new ViewHolder();
holder.text = (TextView) view.findViewById(R.id.textView1);
holder.image = (ImageView) view.findViewById(R.id.imageView1);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.text.setText(arrayList.get(pos).getName());
holder.image.setBackgroundResource(arrayList.get(pos).getImage());
return view;
}
public class ViewHolder {
TextView text;
ImageView image;
} }
public class Stack_Items {
String name;
Integer image;
public Stack_Items(String name, Integer image) {
this.name = name;
this.image = image;
}
public String getName() {
return name;
}
public int getImage() {
return image;
} }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:textColor="#FFFFFF" />
Fragment eventsFragment = new EventsFragment();
fragmentTransaction.replace(R.id.containerView,eventsFragment);
fragmentTransaction.commit();
View view = inflater.inflate(R.layout.events_layout, container, false);
stackView = (StackView) view.findViewById(R.id.stackView1);
list = new ArrayList<Stack_Items>();
//Adding items to the list
for (int i = 0; i < icons.length(); i++) {
list.add(new Stack_Items("Item " + i, icons.getResourceId(i,-1 )));
}
//Calling adapter and setting it over stackview
Stack_Adapter adapter = new Stack_Adapter(getActivity().getApplicationContext(),list);
stackView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return view;