1
私のSQLiteデータベースから読み込まれたGridViewを挿入しようとしていますが、エラーを取得するフラグメント:nullオブジェクト参照で仮想メソッド 'android.view.View android.view.View.findViewById(int)'を呼び出そうとしました
MyAdapter.javaクラス "NULLオブジェクト参照に 'android.view.View android.view.View.findViewById(INT)' 仮想メソッドを起動しようとし、"
public class MyAdapter extends BaseAdapter {
Context context;
ArrayList<Players> playerList;
private static LayoutInflater inflater = null;
public MyAdapter(Context context, ArrayList<Players> playerList){
this.context = context;
this.playerList = playerList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return playerList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(R.layout.layout_grid_item, null);
TextView idTextView = (TextView) convertView.findViewById(R.id.tv_player_id);
TextView nameTextView = (TextView) convertView.findViewById(R.id.tv_player_name);
TextView roleTextView = (TextView) convertView.findViewById(R.id.tv_player_role);
Players p = new Players();
p = playerList.get(position);
idTextView.setText("ID " + String.valueOf(p.getId()));
nameTextView.setText("Name " + String.valueOf(p.getName()));
roleTextView.setText("Role " + String.valueOf(p.getRole()));
return convertView;
}
}
GameActivity.javaクラス
public class GameActivity extends Activity {
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_activity);
db = new DatabaseHelper(this);
}
public void FragmentOneClick(View view) {
Fragment myfragment;
myfragment = new RolesFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_switch, myfragment);
fragmentTransaction.commit();
}
}
RolesFragment.java
public class RolesFragment extends Fragment {
GridView gridView;
ArrayList<Players> playersList;
MyAdapter adapter;
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_viewroles, container, false);
gridView = (GridView) getView().findViewById(R.id.gv_players);
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
playersList = new ArrayList<Players>();
playersList = databaseHelper.getPlayers();
adapter = new MyAdapter(getActivity(), playersList);
gridView.setAdapter(adapter);
return view;
}
}
勿論!どうもありがとうございました。コードを使って何時間も演奏しています。 –
@TussRamzan答えがうまくいったら...それを正しいものとしてマークしてください。感謝祭 – rafsanahmad007