【问题标题】:Custom Component class methods are throwing error when called自定义组件类方法在调用时抛出错误
【发布时间】:2012-07-31 23:38:37
【问题描述】:

我在 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): at 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): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
07-31 17:54:25.657: E/AndroidRuntime(11454): at 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): at com.nsholmes.bballStats.games.Game.loadComponents(Game.java:34)
07-31 17:54:25.657: E/AndroidRuntime(11454): at 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>

【问题讨论】:

  • 您应该发布自定义组件的完整代码。
  • 并附上来自 logcat 的错误
  • findViewById 很可能返回 null,但我真的很想查看您的完整异常堆栈跟踪。
  • 这是我得到的所有错误信息

标签: android android-layout custom-component


【解决方案1】:

问题可能在于您尝试查找视图的方式。据我了解,findViewById(..) 基于setContentView(..) 工作,并且由于您的类中没有设置内容视图,因此返回的视图为空。

也许你可以尝试从膨胀的视图中找到 TextView。

.....
private int statValue;
private View mView;

public NumberStepper(Context context, AttributeSet attrs) {
    super(context, attrs);
    //tvStepperName.setText(name);
    statValue = 0;

    LayoutInflater inflater = LayoutInflater.from(context);
    mView = inflater.inflate(R.layout.numberstepper, this);
    loadComponents();

}

private void loadComponents(){
    tvStepperName = (TextView) mView.findViewById(R.id.tvStepperName);
    tvStepperValue = (TextView) mView.findViewById(R.id.tvStepperValue);
    btnIncrement = (Button) mView.findViewById(R.id.btnIncrement);
    btnDecrement = (Button) mView.findViewById(R.id.btnDecrement);
.....
}
.....
.....

【讨论】:

  • 就是这样。有效。我不确定我是否完全理解为什么。 LinearLayout 类不提供 setContentView() 因此创建 View 变量允许...?
  • 我对 android 开发比较陌生,所以有人可以更好地解释这一点。我会尽我所能:) setContentView(...) 可用于 Activity 类和任何扩展它的自定义类。这基本上是您在 onCreate(...) 方法中加载主布局的地方。当基于LinearLayout(或任何其他布局)创建自定义布局时,您实际上是在创建View。所以视图本身不会有setContentView(...) 方法。使用自定义视图,您必须扩展自定义布局并使用它。
【解决方案2】:

您的一个 findViewById 调用返回 null,这可能发生在以下几行中(您没有发布完整的布局和代码,所以我在这里盲目拍摄):

ccOne = (NumberStepper)findViewById(R.id.ccOne);

tvStepperName = (TextView) findViewById(R.id.tvStepperName);

编辑:

由于现在我们看到您的完整堆栈跟踪,因此错误位于您的 loadComponents 函数的第 34 行。您在 NumberStepper 上调用 findViewById 来查找 R.id.tvStepperName 和 R.id.tvStepperValue,您确定在布局 xml 中的 NumberStepper 中有带有这些名称的 Views?

【讨论】:

  • 看起来你的第二个 xml sn-p 的内容在两个 NumberSteppers 之外。 NumberStepper 标签应该环绕它应该包含的视图。如果不是这样,您应该找到 NumberStepper 的父视图,这将是您的 LinearLayout,然后您应该在此 LinearLayout 上调用 findViewById
  • 这里有两个布局文件。一个是包含复合组件的 numberStepper.xml。另一个使用复合组件的多个实例(numberStepper)
  • 按原样发布您的布局。你明白我在之前的评论中所说的吗?
猜你喜欢
  • 1970-01-01
  • 2020-05-30
  • 2022-07-01
  • 1970-01-01
  • 2011-06-16
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多