将活动转换为片段

时间:2015-10-01 09:39:45

标签: android android-fragments

我正在制作一款应用,并希望将Stack-View添加到其中。 我想在片段而不是活动中添加它。

一旦我从NavDrawer中选择片段,我就会收到错误

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)

在添加堆栈视图之前,它曾经运作良好。

EventsFragment.java

public class EventsFragment extends android.support.v4.app.Fragment {
private static StackView stackView;
private static ArrayList<Stack_Items> list;
TypedArray icons =getResources().obtainTypedArray(R.array.event_stack_icon);

private RelativeLayout rlayout;
private FragmentActivity faActivity;

@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    faActivity  = (FragmentActivity)    super.getActivity();
    rlayout     = (RelativeLayout)    inflater.inflate(R.layout.events_layout, container, false);
    stackView = (StackView) getView().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(faActivity,list);
    stackView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    return rlayout;
} }

events_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>

Stack_Adapter.java

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;
} }

Stack_Items.java

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;
} }

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">

<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();

1 个答案:

答案 0 :(得分:1)

使用View代替RelativeLayout并使用getActivity().getApplicationContext()代替super.getActivity()

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;