好的,所以我创建了一个活动" area"其中有按钮的网格视图和文本视图,因此您可以在按钮上看到该区域的名称。 当你点击按钮,它会带你到"表"活动,你看到实际相同+表的编号(区域名称+表格的编号),但当我点击区域时,应用程序崩溃,因为它不会获得表格中区域的名称&#39的活动。
我知道这令我感到困惑我用我的解释尽力而为,我把这两个适配器,一个用于区域,第二个用于桌子,以最短的方式,请告诉我我做错了什么。
区域适配器:
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);
}
我得到了这个错误:
错误:(43,19)错误:找不到适合MyTableAdapter的构造函数(Activity_Table,ArrayList) 构造函数MyTableAdapter.MyTableAdapter(Context,ArrayList,String,String)不适用 (实际和正式的参数列表长度不同) 构造函数MyTableAdapter.MyTableAdapter(Activity_Table)不适用 (实际和正式的参数列表长度不同)
答案 0 :(得分:1)
您的应用程序崩溃是因为您尝试从areas
ArrayList检索数据而不实例化它。因此,没有应用程序要检索的数据,因此错误。
确保您的areas
ArrayList具有有效数据,例如通过在其构造函数中为您的适配器提供areas
列表的副本:
public MyTableAdapter(Context mContext, ArrayList<Table> tables, ArrayList<Area> areas) {
this.mContext = mContext;
this.tables = tables;
this.areas = areas;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
答案 1 :(得分:0)
查看这部分代码。
for (int i = 0; i < Integer.parseInt(numTables); i++) {
//What are you adding here.. an empty table object?
tables.add(new Table());
}
adapter = new MyTableAdapter(Activity_Table.this, tables);
答案 2 :(得分:0)
由于您尚未发布完整代码,我假设您正在使用两种不同的活动。一个使用MyAreaAdapter
,另一个使用MyTableAdapter
,因此我们称之为MyAreaActivity
和MyTableActivity
。有了这个,我知道你真的不需要MyTableActivity
中的所有领域。我认为你应该从MyTableActivity
传递用于启动MyAreaActivity
的意图中的区域名称和表格数量:
Intent i = new Intent(this, MyTableActivity.class);
i.putExtra("area_name", area.getName());
i.putExtra("area_tables", area.getNumOFTables());
startActivity(i);
然后,在MyTableActivity
:
Intent intent = getIntent();
String area_name = intent.getExtras().getString("area_name");
int area_tables = intent.getExtras().getInt("area_tables");
并将其传递给MyTableAdapter
:
String area_name;
int area_tables
public MyTableAdapter(Context mContext, ArrayList<Table> tables, String area_name, int area_tables) {
this.mContext = mContext;
this.tables = tables;
this.area_name = area_name;
this.area_tables = area_tables;
}
@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(area_name + " " + area_tables);
return convertView;
}
如果您需要该地区的更多信息,可以使用Serializable
或Parcelable
传递整个对象。
如果这不是您的问题,请向我们提供更多详细信息并发布您的更新代码。否则,很难帮助你。