2012-07-31 79 views
1

我在layout/XML中创建了一个具有2个TextViews和2个按钮的自定义组件。 在其配套的I类简单地具有按钮增量和减量自定义组件类方法在调用时抛出错误

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; 
    } 

} 

} 

接着我创建的具有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> 

在该另一XML布局TextViews中的一个的值布局的支持类(其延伸活性)予设定的化合物组分变量的setContentView(),并且我尝试以下:

ccOne = (NumberStepper)findViewById(R.id.ccOne); 
    ccOne.setLabel("Label Name"); 

我得到一个n错误说明 - 无法启动活动ComponentInfo)。我只在调用setLabel()时才会出现此错误。

07-31 17:54:25.657:E/AndroidRuntime(11454):致命异常:主
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):at 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):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1161)
07-31 17:54:25.657:E/AndroidRuntime 11454):at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 17:54:25.657:E/AndroidRuntime(11454):at android.os.Looper.loop(Looper.java:137 )
07-31 17:54:25.657:E/AndroidRuntime(11454):at 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):at 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):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):at 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):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-31 17:54:25.657:E/AndroidRuntime(11454):...11多

任何帮助,将不胜感激,谢谢 这是

<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> 
+0

您应该发布自定义组件的完整代码。 – Luksprog 2012-08-01 04:51:27

+0

并从logcat附上一个错误 – 2012-08-01 06:38:04

+0

很可能findViewById返回null,但我真的很希望看到你的完整异常堆栈跟踪。 – 2012-08-01 14:22:34

回答

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); 
..... 
} 
..... 
..... 
+0

就是这样。有效。我不确定我完全理解为什么。 LinearLayout类不提供setContentView(),因此创建View变量允许...? – DeekMan 2012-08-01 15:49:35

+0

我对android开发相对较新,所以有人可能会更好地解释这一点。我会尽我所能知道的:) ''setContentView(...)'在Activity类以及扩展它的任何自定义类上都可用。这基本上是你在'onCreate(...)'方法中加载主布局的地方。 当基于'LinearLayout'(或任何其他布局)创建自定义布局时,您基本上正在创建'View'。所以一个视图本身不会有'setContentView(...)'方法。使用自定义视图,您必须充气自定义布局并使用它。 – shri046 2012-08-01 16:20:13

+0

感谢您的帮助! – DeekMan 2012-08-01 16:39:35

1

你的一个findViewById通话将返回空值,这可能对以下行来发生的numberstepper的完整布局(您没有张贴您的完整布局和代码,所以我在这里采取了瞎炮):

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

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

编辑:

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

+0

是的,这里是xml。 – DeekMan 2012-08-01 14:42:32

+0

看起来像你的第二个XML片段的内容是在两个NumberSteppers之外。 NumberStepper标签应该包含它应该包含的视图。如果不是它应该如何工作,你应该找到NumberStepper的父视图,它将是你的LinearLayout,然后你应该在这个LinearLayout上调用'findViewById'。 – 2012-08-01 15:15:25

+0

这里有两个布局文件。一个是包含复合组件的numberStepper.xml。另一个使用复合组件的多个实例(numberStepper) – DeekMan 2012-08-01 15:40:09

相关问题