2017-04-24 4 views
0

アダプタをonCreate()で追加しようとしましたが、ヌルポインタ例外が発生しました。 'nullでlistVIew.setAdapterを呼び出そうとしましたオブジェクト参照。nullオブジェクト照会でAdapter.notifyDataSetChanged()を呼び出そうとしました

私の主なアクティビティ内でnotifyDataSetChange()を使用するコードを次に示します。

import static com.name.sample.Tab1.adapter; 


public class Requests extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... params) { 

     Cursor c = myDB.rawQuery("SELECT * FROM requests", null); 

     int aIndex = c.getColumnIndex("name"); 
     int bIndex = c.getColumnIndex("nick"); 
     int cIndex = c.getColumnIndex("num"); 

     name.clear(); 
     nick.clear(); 
     number.clear(); 

     if (c.moveToFirst()){ 

      do { 

       name.add(c.getString(aIndex)); 
       nick.add(c.getString(bIndex)); 
       number.add(c.getInt(cIndex)); 

      } while (c.moveToNext()); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 


     adapter.notifyDataSetChanged(); 


    } 
} 

はここにここに私のTab1をコード ある

public class Tab1 extends Fragment { 

static ListView listNotifications; 
final static ArrayList<String> name = new ArrayList<>(); 
static ArrayList<Integer> number = new ArrayList<>(); 
static ArrayList<String> nick = new ArrayList<>(); 

static ImageAdapter adapter; 


public static class ImageAdapter extends ArrayAdapter<String> { 
    private final Context context; 
    private final String[] values; 

    public ImageAdapter (Context context, String[] values) { 
     super(context, R.layout.list_with_icons, values); 
     this.context = context; 
     this.values = values; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View rowView = inflater.inflate(R.layout.list_with_icons, parent, false); 

     TextView textView = (TextView) rowView.findViewById(R.id.textViewName); 

     ImageView imageView = (ImageView) rowView.findViewById(R.id.imagePicture); 

     textView.setText(values[position]); 
     // Change the icon for Windows and iPhone 


     return rowView; 
    } 
} 

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

    View rootView = inflater.inflate(R.layout.tab1layout, container, false); 

    String[] nameString = new String[name.size()]; 
    nameString = name.toArray(nameString); 

    adapter = new ImageAdapter (getActivity(), nameString); 



    listNotifications =(ListView)rootView.findViewById(R.id.listNotifications); 

listNotifications.setAdapter(adapter); 




    return rootView; 


}} 

答えて

1

を使用してみてくださいアダプタのコードです:adapterClass.this.notifyDataSetChanged()

@Override 
protected void onPostExecute(String s) { 
    super.onPostExecute(s); 
    InvitedAdapter.this.notifyDataSetChanged(); 
} 
+0

()を試してみてください。それはそれが囲むクラスではないことをエラーにします –

+0

あなたが使用することができます: 'Tab1.InvitedAdapter.notifyDataSetChanged();' – rafsanahmad007

+0

notifyDataSetChanged();静的コンテキストからは参照できません。はい、アダプターを非静的に変更しました。 –

1

onPostExcecute()であなたはリストビューで間違ったアダプタを追加しました。それはonCreateView()で次のようにする必要があります:

listNotifications.setAdapter(adapter); 

は私がTab1.InvitedAdapter.this.notifyDataSetChangedとしてそれを使用し、この:)

+0

誤解をおかけして申し訳ありませんが、事実を単純に保つために名前を変数に変更しましたが、私は1つを逃した。これは解決策ではありません。ありがとうございます –

+0

私はあなたの問題は、適切なアダプタを取得していないので、リストビューと思う。 @ハムザミミン – Sam

関連する問題