0

我目前正在尝试创建一个应用程序,该应用程序包含一个 gridview 作为主布局,并用 Fragments 填充 gridview,单击时可能会展开以替换主视图。我的问题是我的自定义适配器为我的片段视图返回 null 并在启动应用程序时导致强制关闭......或者我相信。这些片段也都以编程方式声明了它们的布局......我添加了空白ID并使用setId(R.id.blahblah)设置它们......想知道这是否与问题有关

11-06 17:18:58.119  28254-28254/com.osiris.basket E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at android.widget.AbsListView.obtainView(AbsListView.java:2179)
        at android.widget.GridView.onMeasure(GridView.java:1040)
        at android.view.View.measure(View.java:15855)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15855)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15855)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:15855)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2451)
        at android.view.View.measure(View.java:15855)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
        at android.view.Choreographer.doCallbacks(Choreographer.java:562)
        at android.view.Choreographer.doFrame(Choreographer.java:532)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5289)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
        at dalvik.system.NativeStart.main(Native Method)

自定义适配器:

public class FragmentAdapter extends BaseAdapter {

private Context mContext;

public FragmentAdapter(Context c) {
    mContext = c;
}

private Fragment[] mEggs = {
        //new DessertCase(mContext),
        //new Nyandroid(mContext),
        new Logo(mContext)
};

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

@Override
public Fragment getItem(int i) {
    return mEggs[i];
}

@Override
public long getItemId(int i) {
    Fragment frag = mEggs[i];
    return frag.getId();
}

public View getView(int position, View convertView, ViewGroup parent) {
    Fragment frag = mEggs[position];
    View view;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        view = frag.getView();
        //view.setLayoutParams(new GridView.LayoutParams(85, 85));
        //view.setPadding(8, 8, 8, 8);
    } else {
        view = convertView;
    }
    return view;
}
}

片段之一:

public class Logo extends Fragment {
Toast mToast;
ImageView mContent;
int mCount;
final Handler mHandler = new Handler();
public static int SDK_INT = android.os.Build.VERSION.SDK_INT;
public String n;
private Context context;

public Logo(Context context) {
    this.context = context;

}

private View makeView() {
    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

    LinearLayout view = new LinearLayout(context);
    view.setOrientation(LinearLayout.VERTICAL);
    view.setId(R.id.beanbag);

    view.setLayoutParams(
            new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            ));
    final int p = (int) (8 * metrics.density);
    view.setPadding(p, p, p, p);

    Typeface light = Typeface.create("sans-serif-light", Typeface.NORMAL);
    Typeface normal = Typeface.create("sans-serif", Typeface.BOLD);

    final float size = 14 * metrics.density;
    final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.gravity = Gravity.CENTER_HORIZONTAL;
    lp.bottomMargin = (int) (-4 * metrics.density);

    TextView tv = new TextView(context);
    if (light != null) tv.setTypeface(light);
    tv.setTextSize(1.25f * size);
    tv.setTextColor(0xFFFFFFFF);
    tv.setShadowLayer(4 * metrics.density, 0, 2 * metrics.density, 0x66000000);
    tv.setText("Android " + Build.VERSION.RELEASE);
    view.addView(tv, lp);

    tv = new TextView(context);
    if (normal != null) tv.setTypeface(normal);
    tv.setTextSize(size);
    tv.setTextColor(0xFFFFFFFF);
    tv.setShadowLayer(4 * metrics.density, 0, 2 * metrics.density, 0x66000000);

    if (SDK_INT == 9 || SDK_INT == 10) {
        n = "Gingerbread";
    } else if (SDK_INT == 11 || SDK_INT == 12 || SDK_INT == 13) {
        n = "Honeycomb";
    } else if (SDK_INT == 14 || SDK_INT == 15) {
        n = "ICS";
    } else if (SDK_INT >= 15) {
        n = "JELLY BEAN";
    }

    tv.setText(n);
    view.addView(tv, lp);

    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getActivity().requestWindowFeature(Window.FEATURE_NO_TITLE);
    getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    mToast = Toast.makeText(context, "", Toast.LENGTH_LONG);
    mToast.setView(makeView());

    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

    mContent = new ImageView(context);
    mContent.setImageResource(R.drawable.platlogo_alt);
    mContent.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

    final int p = (int) (32 * metrics.density);
    mContent.setPadding(p, p, p, p);

    mContent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mToast.show();
            mContent.setImageResource(R.drawable.platlogo);
        }
    });

    mContent.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            try {
                startActivity(new Intent(Intent.ACTION_MAIN)
                        .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                | Intent.FLAG_ACTIVITY_CLEAR_TASK
                                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
                        .addCategory("com.osiris.beanbag.BeanBag"));
                //.setClassName("com.android.systemui","com.android.systemui.beanbag"));
            } catch (ActivityNotFoundException ex) {
                android.util.Log.e("PlatLogoActivity", "Couldn't find a bag of beans.");
            }
            getActivity().finish();
            return true;
        }
    });
    //mContent.setId(R.id.beanbag);
}
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.id.beanbag, container, false);
}
}
4

1 回答 1

0

您正在片段“frag.getView()”上调用 getView 方法。如果提供,它返回获取片段布局的根视图(由 onCreateView(LayoutInflater, ViewGroup, Bundle) 返回的那个)。: 在 developer.android.com 上找到。在 onCreateView 中,您正在膨胀“R.id.beanbag”。如果您查看一些示例,您可以看到在这里您应该像“R.layout.xml_file_in_res_layout”这样膨胀布局

于 2013-11-07T05:24:47.767 回答