好吧,所以我創建了一個活動「區域」,其中的按鈕和文本視圖的網格視圖,因此您可以看到按鈕上的區域名稱。 當你點擊按鈕,它會帶你到「表」活動,你實際上看到+表的數量(地區名稱+表的數量),但當我點擊區域應用程序崩潰,因爲它贏得' t在表格活動中獲取該區域的名稱。從另一個活動獲取名稱
我知道這很混亂,我盡我最大努力與我的解釋,我把這兩個適配器,一個地區和第二個表格,以最短的方式,請告訴我我做錯了什麼。
地區的適配器:
public ArrayList<Area> areas;
public static LayoutInflater inflater = null;
public MyAreaAdapter(Context mContext, ArrayList<Area> areas) {
this.mContext = mContext;
this.areas = areas;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public MyAreaAdapter(Activity_Table activity_zone){
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
ImageView imageView;
if (convertView == null) {
convertView = inflater.inflate(R.layout.imagebutton_layout, null);
}
ImageView myImageView = (ImageView) convertView.findViewById(R.id.myImageView);
TextView areaName = (TextView) convertView.findViewById(R.id.areaName);
areaName.setText(areas.get(position).getName());
return convertView;
}
,這是表的適配器:
ArrayList<Area> areas;
ArrayList<Table> tables;
public static LayoutInflater inflater = null;
public MyTableAdapter(Context mContext, ArrayList<Table> tables) {
this.mContext = mContext;
this.tables = tables;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public MyTableAdapter(Activity_Table activity_table){
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
ImageView imageView;
if (convertView == null){
convertView = inflater.inflate(R.layout.imageview_table_layout, null);
}
ImageView tableImageView = (ImageView) convertView.findViewById(R.id.tableImageView);
TextView tableName = (TextView) convertView.findViewById(R.id.tableName);
tableName.setText(areas.get(position).getName() + " " + areas.get(position).getNumOFTables());
return convertView;
}
錯誤日誌:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
這是它崩潰行:
TableName.setText(areas.get(position).getName() + " " + areas.get(position).getNumOFTables());
這是調用我的表適配器類:
GridView gridView;
ArrayList<Table> tables;
ImageView imageView;
MyTableAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity__table);
Intent startIntent = getIntent();
String numTables = startIntent.getStringExtra("numTables");
gridView = (GridView) findViewById(R.id.gridView2);
OrdersDBAdapter.OrdersDBHelper ordersDBHelper = new OrdersDBAdapter.OrdersDBHelper(Activity_Table.this);
tables = new ArrayList<Table>();
for (int i = 0; i < Integer.parseInt(numTables); i++) {
tables.add(new Table());
}
adapter = new MyTableAdapter(Activity_Table.this, tables);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(getApplicationContext(), MyOrderMainActivity.class));
}
});
}
編輯
我加了這段代碼到我的適配器,意圖:
public MyTableAdapter(Context mContext, ArrayList<Table> tables, String area_name, String numTables) {
this.mContext = mContext;
this.tables = tables;
this.area_name = area_name;
this.numTables = numTables;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
我得到這個錯誤:
Error:(43, 19) error: no suitable constructor found for MyTableAdapter(Activity_Table,ArrayList) constructor MyTableAdapter.MyTableAdapter(Context,ArrayList,String,String) is not applicable (actual and formal argument lists differ in length) constructor MyTableAdapter.MyTableAdapter(Activity_Table) is not applicable (actual and formal argument lists differ in length)
你的arrayList區域是初始化的嗎?在哪裏調用適配器類?你得到這個異常是因爲你的數組列表是空的嗎?張貼你已經初始化並調用適配器的類。 – Ritesh
@Ritesh我發佈了它,請檢查出來 –
好吧,我已經發布了一段代碼,你在哪裏存儲一個空對象?你還可以在你正在初始化區域的地方發佈代碼嗎? – Ritesh