我在 layout/XML 中创建了一个自定义组件,它有 2 个 TextViews 和 2 个按钮。在它的支持类中,我只是让按钮增加和减少其中一个 TextViews 的值
public class NumberStepper extends LinearLayout implements View.OnClickListener {
//Define the elements (Children) of the compound view
public TextView tvStepperName, tvStepperValue;
public Button btnIncrement, btnDecrement;
private int statValue;
public NumberStepper(Context context, AttributeSet attrs) {
    super(context, attrs);
    //tvStepperName.setText(name);
    statValue = 0;
    LayoutInflater inflater = LayoutInflater.from(context);
    inflater.inflate(R.layout.numberstepper, this);
    loadComponents();
    }
private void loadComponents(){
    tvStepperName = (TextView) findViewById(R.id.tvStepperName);
    tvStepperValue = (TextView) findViewById(R.id.tvStepperValue);
    btnIncrement = (Button) findViewById(R.id.btnIncrement);
    btnDecrement = (Button) findViewById(R.id.btnDecrement);
    btnIncrement.setOnClickListener(this);
    btnDecrement.setOnClickListener(this);
    tvStepperValue.setText("" + statValue);
}
public void setLabel(String str){
    tvStepperName.setText(str);
}
public void onClick(View v) {
    switch(v.getId()){
    case R.id.btnIncrement:
            statValue++;
            tvStepperValue.setText("" + statValue);
            tvStepperName.setText("HELLO");
        break;
    case R.id.btnDecrement:
        if(statValue > 0){
            statValue--;
        }
        tvStepperValue.setText("" + statValue);
        break;
    }
}
}
接下来,我创建了另一个 XML 布局,其中包含 5 或 6 个这样的复合组件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.nsholmes.bballStats.componets.NumberStepper
    android:id="@+id/ccOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></com.nsholmes.bballStats.componets.NumberStepper>
<com.nsholmes.bballStats.componets.NumberStepper
    android:id="@+id/ccTwo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></com.nsholmes.bballStats.componets.NumberStepper>
在这个布局的支持类(它扩展了 Activity)中,我设置了复合组件变量 setContentView(),并尝试了以下操作:
ccOne = (NumberStepper)findViewById(R.id.ccOne);
    ccOne.setLabel("Label Name");
我收到一条错误消息 - 无法启动活动 ComponentInfo)。我只在调用 setLabel() 时收到此错误。
07-31 17:54:25.657: E/AndroidRuntime(11454): 致命异常: main 
07-31 17:54:25.657: E/AndroidRuntime(11454): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. nsholmes.bballStats/com.nsholmes.bballStats.games.Game}:java.lang.NullPointerException 
07-31 17:54:25.657:E/AndroidRuntime(11454):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970 ) 
07-31 17:54:25.657: E/AndroidRuntime(11454): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) 
07-31 17:54:25.657: E/AndroidRuntime(11454): 在 android .app.ActivityThread.access$600(ActivityThread.java:128) 
07-31 17:54:25.657: E/AndroidRuntime(11454): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-31 17:54:25.657: E/AndroidRuntime(11454): 在 android.os.Handler.dispatchMessage(Handler.java:99) 
07-31 17:54:25.657: E/AndroidRuntime(11454): 在 android. os.Looper.loop(Looper.java:137) 
07-31 17:54:25.657: E/AndroidRuntime(11454): 在 android.app.ActivityThread.main(ActivityThread.java:4514) 
07-31 17:54: 25.657: E/AndroidRuntime(11454): 在 java.lang.reflect.Method.invokeNative(Native Method) 
07-31 17:54:25.657: E/AndroidRuntime(11454): 在 java.lang.reflect.Method.invoke( Method.java:511) 
07-31 17:54:25.657: E/AndroidRuntime(11454): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 
07-31 17:54: 25.657: E/AndroidRuntime(11454): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-31 17:54:25.657: E/AndroidRuntime(11454): at dalvik.system.NativeStart.main(Native Method) 
07-31 17:54:25.657: E/AndroidRuntime(11454): 引起:java.lang .NullPointerException 07-31 17:54:25.657: E/AndroidRuntime(11454): 在 com.nsholmes.bballStats.games.Game.loadComponents(Game.java:34) 
07-31 17:54:25.657: E/AndroidRuntime( 11454): 在 com.nsholmes.bballStats.games.Game.onCreate(Game.java:27) 
07-31 17:54:25.657: E/AndroidRuntime(11454): 在 android.app.Activity.performCreate(Activity.java :4465) 
07-31 17:54:25.657: E/AndroidRuntime(11454): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053) 
07-31 17:54:25.657: E/AndroidRuntime(11454):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-31 17:54:25.657: E/AndroidRuntime(11454): ... 11 更多
任何帮助将不胜感激,谢谢这是 numberstepper 的完整布局
<TextView
    android:id="@+id/tvStepperName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="FreeThrow"
    android:layout_weight="10" />
<Button
    android:id="@+id/btnIncrement"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="30"
    android:text="@string/plusSign" />"
<Button
    android:id="@+id/btnDecrement"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="30"
    android:text="@string/minusSign" />
<TextView
    android:id="@+id/tvStepperValue"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="2"
    android:layout_weight="30" />
</LinearLayout>