我认为程序因此而不断崩溃
public class ViewAll extends Activity {
TableLayout t;
MyDBManager d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_all);
t = (TableLayout)findViewById(R.id.TableLayout1);
d = new MyDBManager(ViewAll.this);
Inflated();
}
private void Inflated()
{
Item[] items = d.GetAllItemsArray();
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row;
Button buttonInfo;
TextView priceInfo;
TextView itemInfo;
for (int i=0;i<items.length; i++)
{
row = vi.inflate(R.layout.onetask, null); //Here lies the problem
itemInfo = (TextView) row.findViewById(R.id.textView1);
priceInfo = (TextView) row.findViewById(R.id.textView2);
itemInfo.setText(items[i].getName());
priceInfo.setText(items[i].getPrice());
buttonInfo = (Button) row.findViewById(R.id.button1);
buttonInfo.setTag(items[i].getId() + "");
buttonInfo.setOnClickListener(l);
t.addView(row);
}
}
OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ViewAll.this, v.getTag().toString(),Toast.LENGTH_LONG).show();
Intent i = new Intent(ViewAll.this, UpdateItems.class);
i.putExtra("itemid", v.getTag().toString());
startActivity(i);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {enter code here
getMenuInflater().inflate(R.menu.view_all, menu);
return true;
}
}
答案 0 :(得分:0)
它不会崩溃。
您可以获得的更糟糕的是布局显示不正确。但是,关于避免将null作为父级发送的警告是正确的。您应该始终传递根视图(有例外)。如果你传递根视图,你还必须传递一个布尔值作为第三个参数。那个布尔值意味着你要么想要在你刚刚传递的根上注入膨胀的视图。因此,如果您传递了真实,您的父视图将在其中包含膨胀的视图。