Java 我还需要叫我的复合控件什么?
我创建了一个具有以下内容的复合控件: 爪哇 XMLJava 我还需要叫我的复合控件什么?,java,android,android-layout,Java,Android,Android Layout,我创建了一个具有以下内容的复合控件: 爪哇 XML 为什么这不起作用?我漏了一步吗?将引发NullPointerException。在调用LoadView时,小部件将不在那里。等待onFinishInflate。您永远不会在只接受上下文参数的构造函数中膨胀视图,这意味着quantity.setText。。。;将抛出NPE,因为在findViewById中找不到数量 你有stacktrace可用吗?logcat在QuantityBox上的onCreate上显示一个空指针。你是否调试并进入Quant
为什么这不起作用?我漏了一步吗?将引发NullPointerException。在调用LoadView时,小部件将不在那里。等待onFinishInflate。您永远不会在只接受上下文参数的构造函数中膨胀视图,这意味着quantity.setText。。。;将抛出NPE,因为在findViewById中找不到数量 你有stacktrace可用吗?logcat在QuantityBox上的onCreate上显示一个空指针。你是否调试并进入QuantityBox构造函数代码和LoadView方法以查看什么是空的?可能是您的上下文为空。尝试传递getApplicationContext或CustomHeaderActivity.this或可能是CustomHeaderActivity.class-我不记得了。不相关,但heh://load-views-loadview;感谢上帝的评论!什么意思?我是否需要为onFinishInflate实现其他功能?为什么不能在充气后立即引用充气视图。充气但应该等待onFinishInflate?根据source,在创建视图层次结构时,是LayoutFlater调用onFinishInflate。然而,据我对android源代码的了解,这些回调是在充气方法结束之前完成的,所以我有点困惑在这种情况下是否有必要使用onFinishInflate。
public class QuantityBox extends LinearLayout
{
private TextView partNmbr;
private Button decrease;
private Button increase;
private EditText quantity;
private int qty;
public QuantityBox(Context context)
{
super(context);
// load views
loadViews();
}
/**
* This constructor is necessary in order to use this compound
* control in an XML layout.
* @param context Application context
* @param attrs
*/
public QuantityBox(Context context, AttributeSet attrs)
{
super(context, attrs);
// inflate the view we created
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.quantity_box, this);
// load views
loadViews();
}
/**
* Load the views created from the constructor
*/
private void loadViews()
{
this.qty = 1;
partNmbr = (TextView) findViewById(R.id.tvPartNmbr);
decrease = (Button) findViewById(R.id.btnDecrease);
increase = (Button) findViewById(R.id.btnIncrease);
quantity = (EditText) findViewById(R.id.etQty);
// set initial text
quantity.setText(this.qty);
decrease.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// decrease quantity
qty--;
// update view
quantity.setText(qty);
}
});
increase.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// increase quantity
qty++;
// update view
quantity.setText(qty);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvPartNmbr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
/>
<Button
android:id="@+id/btnDecrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="-"
/>
<EditText
android:id="@+id/etQty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
/>
<Button
android:id="@+id/btnIncrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="+"
/>
</LinearLayout>
public class CustomHeaderActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_title_bar);
QuantityBox qb = new QuantityBox(this);
}
}