2012-07-31 73 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

相關問題