使用

问题描述 投票:0回答:1

这是我的MainFragment

public class MainFragment extends Fragment {
ViewPager viewPager;
CustomSwipeAdapter adapter;

public MainFragment() {
    // Required empty public constructor
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
  viewPager = (ViewPager) getView().findViewById(R.id.view_pager);
    adapter = new CustomSwipeAdapter(this.getActivity());
    viewPager.setAdapter(adapter);

    return inflater.inflate(R.layout.fragment_main, container, false);

}



}

这是我的 CustomSwipeAdapter

package my.edu.tarc.hilti;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class CustomSwipeAdapter extends PagerAdapter {
private int[] image_resource = {R.drawable.sample1, R.drawable.sample2, R.drawable.sample3, R.drawable.sample4};
private Context ctx;
private LayoutInflater layoutInflater;

public CustomSwipeAdapter(Context ctx) {
    this.ctx = ctx;
}

@Override
public int getCount() {
    return image_resource.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return (view == (LinearLayout)object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item_view = layoutInflater.inflate(R.layout.swipe_layout, container, false);
    ImageView imageview = (ImageView) item_view.findViewById(R.id.image_view);
    TextView textView = (TextView)  item_view.findViewById(R.id.image_count);
    imageview.setImageResource(image_resource[position]);
    textView.setText("" + position);
    container.addView(item_view);
    return item_view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((LinearLayout)object);
}

}

这是我的 SwipeLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/image_count"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="Hello World"
    android:textSize="25dp"
    android:textStyle="bold"
    android:gravity="center"
    android:layout_marginTop="25dp"
    />


<ImageView
    android:id="@+id/image_view"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_marginTop="25dp"
    />
</LinearLayout>

我的 Logcat 是

 12-13 10:19:14.866 9070-9070/? I/SELinux: Function:    selinux_android_load_priority [0], There is no sepolicy file.

 12-13 10:19:14.891 9070-9070/? I/SELinux: Function: selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_GT-N7100_4.4.2_0040


 12-13 10:19:14.891 9070-9070/? I/SELinux: selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
 12-13 10:19:14.891 9070-9070/? D/dalvikvm: Late-enabling CheckJNI
 12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
 12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
 12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti W/dalvikvm: VFY: unable to resolve interface method 17990: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
 12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
 12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
 12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti W/dalvikvm: VFY: unable to resolve interface method 17994: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
 12-13 10:19:15.061 9070-9070/my.edu.tarc.hilti D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
 12-13 10:19:15.151 9070-9070/my.edu.tarc.hilti I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
 12-13 10:19:15.151 9070-9070/my.edu.tarc.hilti W/dalvikvm: VFY: unable to resolve virtual method 436: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
 12-13 10:19:15.151 9070-9070/my.edu.tarc.hilti D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
 12-13 10:19:15.151 9070-9070/my.edu.tarc.hilti I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
 12-13 10:19:15.151 9070-9070/my.edu.tarc.hilti W/dalvikvm: VFY: unable to resolve virtual method 458: Landroid/content/res/TypedArray;.getType (I)I
 12-13 10:19:15.151 9070-9070/my.edu.tarc.hilti D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
 12-13 10:19:15.286 9070-9070/my.edu.tarc.hilti D/AndroidRuntime: Shutting down VM
 12-13 10:19:15.286 9070-9070/my.edu.tarc.hilti W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41c0fc08)
 12-13 10:19:15.286 9070-9070/my.edu.tarc.hilti E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process:     my.edu.tarc.hilti, PID: 9070
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{my.edu.tarc.hilti/my.edu.tarc.hilti.MainActivity}: java.lang.NullPointerException
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:175)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:146)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5602)
                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                 at java.lang.reflect.Method.invoke(Method.java:515)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                                                                 at dalvik.system.NativeStart.main(Native Method)
                                                              Caused by: java.lang.NullPointerException
                                                                 at my.edu.tarc.hilti.MainFragment.onCreateView(MainFragment.java:27)
                                                                 at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                 at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
                                                                 at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
                                                                 at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1177)
                                                                 at android.app.Activity.performStart(Activity.java:5461)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2386)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) 
                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:175) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:146) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5602) 
                                                                 at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                 at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
                                                                 at dalvik.system.NativeStart.main(Native Method) 

我现在面临的问题是,它在

MainFragment
处有错误,我在.xml文件中有一个
viewPager
,我使用片段(MainFragment)来显示我的图片滑块,但我遇到了问题,下面3行就是问题所在,我不知道如何解决这个问题。

    viewPager = (ViewPager) getView().findViewById(R.id.view_pager);
    adapter = new CustomSwipeAdapter(this.getActivity());
    viewPager.setAdapter(adapter);
android android-fragments android-viewpager android-adapter
1个回答
0
投票

你的问题就在这里

viewPager = (ViewPager) getView().findViewById(R.id.view_pager);
在 UI 实际膨胀之前,您尝试在
onCreateView
中操作 UI 元素。

要解决此问题,您必须首先膨胀视图,然后才能在那里找到您的视图分页器。请参阅固定的 MainFragment

public class MainFragment extends Fragment {
    ViewPager viewPager;
    CustomSwipeAdapter adapter;

    public MainFragment() {
        // Required empty public constructor
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        viewPager = (ViewPager) view.findViewById(R.id.view_pager);
        adapter = new CustomSwipeAdapter(this.getActivity());
        viewPager.setAdapter(adapter);
        return view;
    }
}

为了确保其正常工作 - 我已将修复版本上传到我的 Dropbox - 请随意下载!

不过,我建议也看看thisSO问题

希望对您有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.