2016-03-25 9 views
2
  • findViewById()はフラグメントクラスでnullを返します。 ImageButtonためToastは....findViewByIdはフラグメントクラスでnullを返します

    public MyAdapter(Context context, List<ListItems> values) { 
    
        super(context, R.layout.list_item, values); 
    } 
    
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    
        View v = convertView; 
        LayoutInflater inflater = LayoutInflater.from(getContext()); 
    
        if(v == null){ 
         v = inflater.inflate(R.layout.list_item, parent, false); 
        } 
    
        ListItems currLoc = LocationList.get(position); 
    
        TextView theTextView = (TextView) v.findViewById(R.id.list_item_textview); 
        theTextView.setText(currLoc.getLocation()); 
    
        TextView theTextView2 = (TextView) v.findViewById(R.id.list_item_textviewP); 
        theTextView2.setText(currLoc.getProfile()); 
    
        ImageView theImageView = (ImageView) v.findViewById(R.id.imageview1); 
        Toast.makeText(getActivity(), "image view="+theImageView, Toast.LENGTH_SHORT).show(); 
        theImageView.setImageResource(R.drawable.bullet); 
    
    
        Switch mySwitch=(Switch) v.findViewById(R.id.onoff); 
        Toast.makeText(getActivity(), ""+mySwitch, Toast.LENGTH_SHORT).show(); 
    
    
        return v; 
    } 
    
+0

ここにxmlを表示してください –

+0

そしてエラーをしてください。それはあなたの 'フラグメントクラス'ではなく、あなたの 'adapter'クラスで、 –

+0

は、' LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)によって 'LayoutInflater inflater = LayoutInflater.from(getContext()); ; ' – ELITE

答えて

0

を助けてください、私はonGetViewでfindViewByIdを定義していた ....それを得た... nullを返しmySwitchSwitch IE用のオブジェクトが、何かを示してい 私はOnCreateViewで宣言しなければならなかった 申し訳ありません.....

+1

仲間の査読者:これは答えです。問題の原因を説明しているOPです。 –

+0

私は** onCreateView **はレイアウトを返すと思った。私はあなたが実際に 'findViewById'をその中で呼び出すのを知らなかった。 –

関連する問題