2013-10-06 61 views
6

我有一個適合使用GridView的適配器,它可以像Activity一樣工作。我試圖把它放在一個片段中,然後轉換它,但它不起作用。當我在我的活動中包含IconFragmentSystem時,當我嘗試打開活動時,會收到一個關閉的力量。在片段中使用Gridview的Android ImageAdapter

我知道活動的作品,因爲我可以使用其他片段,一切都很好,所以我知道我的問題在於這個文件。

package com.designrifts.ultimatethemeui; 
import com.designrifts.ultimatethemeui.R; 
import android.content.Context; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.*; 

import java.util.ArrayList; 

public class IconFragmentSystem extends Fragment implements AdapterView.OnItemClickListener{ 
private static final String RESULT_OK = null; 
public Uri CONTENT_URI; 

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

    View view = inflater.inflate(R.layout.main, container, false); 

    super.onCreate(savedInstanceState); 
    int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size); 

    GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid); 
    gridview.setAdapter(new IconAdapter(this, iconSize)); 
    gridview.setOnItemClickListener(this); 
    CONTENT_URI=Uri.parse("content://"+iconsProvider.class.getCanonicalName()); 
    return view; 
} 

@Override 
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
    String icon=adapterView.getItemAtPosition(i).toString(); 
    Intent result = new Intent(null, Uri.withAppendedPath(CONTENT_URI,icon)); 
    setResult(RESULT_OK, result); 
    finish(); 
} 
private void setResult(String resultOk, Intent result) { 
    // TODO Auto-generated method stub 

} 

private void finish() { 
    // TODO Auto-generated method stub 

} 
private class IconAdapter extends BaseAdapter{ 
    private Context mContext; 
    private int mIconSize; 
    public IconAdapter(Context mContext, int iconsize) { 
     super(); 
     this.mContext = mContext; 
     this.mIconSize = iconsize; 
     loadIcon(); 
    } 

    public IconAdapter(IconFragmentSystem iconssystem, int iconSize) { 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public int getCount() { 
     return mThumbs.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return mThumbs.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(mIconSize, mIconSize)); 
     } else { 
      imageView = (ImageView) convertView; 
     } 
     imageView.setImageResource(mThumbs.get(position)); 
     return imageView; 
    } 

    private ArrayList<Integer> mThumbs; 
    //////////////////////////////////////////////// 
    private void loadIcon() { 
     mThumbs = new ArrayList<Integer>(); 

     final Resources resources = getResources(); 
     final String packageName = getActivity().getApplication().getPackageName(); 

     addIcon(resources, packageName, R.array.systemicons); 

    } 
    private void addIcon(Resources resources, String packageName, int list) { 
     final String[] extras = resources.getStringArray(list); 
     for (String extra : extras) { 
      int res = resources.getIdentifier(extra, "drawable", packageName); 
      if (res != 0) { 
       final int thumbRes = resources.getIdentifier(extra,"drawable", packageName); 
       if (thumbRes != 0) { 
        mThumbs.add(thumbRes); 
       } 
      } 
      } 
      } 
     } 
    } 

我試過不同的方法來實現這一點,但都失敗了,我真的可以使用幫助指向我在正確的方向。

這是我在佈局XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" >   
    <GridView 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:columnWidth="90dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
/> 

    </RelativeLayout> 
+0

LogCat對此有何評論? –

+0

@Ray Stojonic當我運行logcat時,它給我'code'10-06 19:10:27.450:E/AndroidRuntime(10180):致命例外:主 10-06 19:10:27.450:E/AndroidRuntime(10180) :java.lang.NullPointerException 10-06 19:10:27.450:E/AndroidRuntime(10180):\t at com.designrifts.ultimatethemeui.IconFragmentSystem.onCreateView(IconFragmentSystem.java:28)所以我知道它是從onCreateView。我看了一些例子,並閱讀了Android網站,但沒有任何運氣。 –

回答

9

我能解決這個問題

我改變

GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid); 

GridView gridview = (GridView) view.findViewById(R.id.icon_grid); 

,我也改變

 gridview.setAdapter(new IconAdapter(this, iconSize)); 

 gridview.setAdapter(new IconAdapter(getActivity(), iconSize)); 
0

GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);

應該

GridView gridview = (GridView) container.findViewById(R.id.icon_grid);

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.main, container, false); 

    int iconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size); 

    GridView gridview = (GridView) view.findViewById(R.id.icon_grid); 
    gridview.setAdapter(new IconAdapter(this, iconSize)); 
    gridview.setOnItemClickListener(this); 
    CONTENT_URI = Uri.parse("content://"+iconsProvider.class.getCanonicalName()); 
    return view; 
} 

你會想刪除的通話超,這是一個剩下你的活動,這個片段不會叫這個m ethod。

+0

我補充說,仍然有同樣的錯誤。我把logcat以及http://pastebin.com/ByjyURxs是logcat的代碼版本。這是活動的工作版本https://github.com/designrifts/Ultimate_Theme_UI_Template/blob/master/src/com/designrifts/ultimatethemeui/icons.java –

+1

也不要通過超級在oncreateview – r2DoesInc

+0

我沒有刪除超級。謝謝! –

2

嘗試......

gridView = (GridView) rootView.findViewById(R.id.gridView); 

gridView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view,int pos, long l) { 

       // do something... 

      }    
     }); 

更多信息請參閱此鏈接 Android gridview in fragment adapter constructor missing

請註明這是有用的。