0

現在、FRAGMENT 内で ABS(ActionBarSherlock) を使用して GridView を作成しようとしています。しかし、NPE(NullPointerException)を取得するという問題があります。ここに私のフラグメントコードがあります:

public class Fragment2 extends SherlockFragment {

GridView gridView;
ImageAdapter imgAdapter;
private Context context;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment2, container, false);
    gridView = (GridView) rootView.findViewById(R.id.gridview);
    context = getSherlockActivity();
    gridView.setAdapter(new ImageAdapter(context));

    return rootView;
}   

そして私のImageAdapterClass:

public class ImageAdapter extends BaseAdapter {

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};

private static Context mContext;

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

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

}

そして最後に私のLogCat:

10-25 01:06:32.239: E/AndroidRuntime(12344):    at com.AlexPrograms.NavDrawerSherlock.Fragment2.onCreateView(Fragment2.java:24)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at android.os.Handler.handleCallback(Handler.java:587)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at android.os.Looper.loop(Looper.java:130)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at android.app.ActivityThread.main(ActivityThread.java:3687)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at java.lang.reflect.Method.invokeNative(Native Method)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at java.lang.reflect.Method.invoke(Method.java:507)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-25 01:06:32.239: E/AndroidRuntime(12344):    at dalvik.system.NativeStart.main(Native Method)

あなたが何が間違っているのかを見つけることができれば、私は最善を尽くします。前もって感謝します

4

2 に答える 2

0

さて、まず最初に。そのフラグメントは適切に設計されていません。;)

この行:

context = getSherlockActivity(); 

クラッシュする可能性があります。

コンテキストを Fragment に渡す正しい (推奨される標準的な) 方法は、次のようなものです。

public class YourFragment extends SherlockFragment  {
    private static Context sContext;
    public static YourFragment newInstance(Context context) {
        if (context != null) {
            sContext = context.getApplicationContext();
        } else {
            sContext = YourAppInstance.getInstance().getApplicationContext();
        }
        return new YourFragment();
    }

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }
    private void init() {
       // Add anything else you need (and can) init.
        if (sContext == null) {
            sContext = mApp.getApplicationContext();
        }

    }
@Override
protected void onResume() {
    super.onResume();
            init(); // in case of a resume overnight where stuff may be null
    }
    // NOW YOUR STUFF
    private GridView mGridView;
    private ImageAdapter mImgAdapter;

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
         final Bundle savedInstanceState) {
         final View rootView = inflater.inflate(R.layout.fragment2, container, false);
         mGridView = (GridView) rootView.findViewById(R.id.gridview);
         if ( mGridView != null && sContext != null ) {
             mImgAdapter = new ImageAdapter(sContext);
             mGridView.setAdapter(mImgAdapter);
         }

         return rootView;
    }  
}

それはより Android らしく見えます…</p>

ここで、フラグメント内で getSherlockActivity() を使用する前に、次のことを行うことをお勧めします

if (getSherlockActivity() != null && !getSherlockActivity().isFinishing() ) {
   //Safe to use getSherlockActivity here
}

最後になりましたが…</p>

あなたの ImageAdapter…</p>

public Object getItem(int position) {
    return null;
}

は null を返すため、グリッドにはすべて null の項目があります…

多分あなたは次のようなことを意味していました…</p>

  public Object getItem(int position) {
        return mThumbIds[position]
  }

つまり、あなたのイメージアダプターは正確には何のアダプターですか? 画像への参照?ハードコーディングされているのでちょっと奇妙ですが、何かを返す必要があります。グリッドはアダプターにビューを要求し、アダプターはそのビューを作成するためのデータを見つける必要があることを覚えておいてください。

于 2013-10-25T00:11:49.867 に答える