2011-11-02 79 views
1

的視圖的場景如下:添加自定義視圖作爲XML佈局

我有一個活動RunTrainingWorkoutsView使用XML佈局_run_workout.xml_一些標籤由CountDownTimer更新。正常工作......

現在,除了由onTick()CountDownTimer對象回調方法每秒更新標籤,我想自定義視圖表面添加到我的_run_workout.xml layout_會得出更新了一些弧線由同一onTick()方法每一秒......

我run_workout.xml:

<training.timer.CounterClockView 
    android:id="@+id/counter_clock_surface" 
    android:layout_width="300dp" 
    android:layout_height="240dp"> 
</training.timer.CounterClockView> 

我的自定義視圖擴展

//counterClockView is declared outside of onCreate() as CounterClockView counterClockView; 

//later in onCreate(){.... 
counterClockView = (CounterClockView) findViewById(R.id.counter_clock_surface); 

問題是改變成員變量的值:surfaceView

public class CounterClockView extends SurfaceView { 

Paint paint = new Paint(); 
Paint paint2 = new Paint(); 

final RectF rect = new RectF(); 
final RectF rect2 = new RectF(); 

int counterArcAngle = 15; 
//constructor 
public CounterClockView(Context context, AttributeSet attributeSet) { 
    super(context); 

    //setting some paint properties... 

    this.setBackgroundColor(Color.TRANSPARENT); 

} 

@Override 
public void onDraw(Canvas canvas) { 

    rect.set(50, 50, 150, 150); 
    rect2.set(50, 50, 150, 150); 

    this.layout(0, 0, 200, 200); 

    canvas.drawArc(rect, -90, 360, false, paint); 
    canvas.drawArc(rect2, -90, counterArcAngle, false, paint2); 

} 

我的主要延伸活性用下面的代碼獲取參考到自定義surfaceView在佈局類的customView對象(counterClockView)

counterClockView.counterArcAngle = 10; 

會崩潰應用程序...

而且,從我的主要活動,我想打電話給無效()方法改變counterArcAngle值後重做表面看來,但這會導致應用程序崩潰太...

爲什麼不能創建counterClockView對象並將其引用到相同類型的xml佈局元素,並更改其appetion,使其無效等。

編輯logcat的

threadid=1: thread exiting with uncaught exception (group=0x40015560) 

ERROR/AndroidRuntime(487): FATAL EXCEPTION: main 

ERROR/AndroidRuntime(487): java.lang.RuntimeException: Unable to start activity ComponentInfo{training.timer/training.timer.RunTrainingWorkoutsView}: java.lang.NullPointerException 

ERROR/AndroidRuntime(487):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
ERROR/AndroidRuntime(487):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
ERROR/AndroidRuntime(487):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
ERROR/AndroidRuntime(487):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
ERROR/AndroidRuntime(487):  at android.os.Handler.dispatchMessage(Handler.java:99) 
ERROR/AndroidRuntime(487):  at android.os.Looper.loop(Looper.java:123) 

ERROR/AndroidRuntime(487):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
ERROR/AndroidRuntime(487):  at java.lang.reflect.Method.invokeNative(Native Method) 
ERROR/AndroidRuntime(487):  at java.lang.reflect.Method.invoke(Method.java:507) 
ERROR/AndroidRuntime(487):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
ERROR/AndroidRuntime(487):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
ERROR/AndroidRuntime(487):  at dalvik.system.NativeStart.main(Native Method) 
ERROR/AndroidRuntime(487): Caused by: java.lang.NullPointerException 
ERROR/AndroidRuntime(487):  at training.timer.RunTrainingWorkoutsView.onCreate(RunTrainingWorkoutsView.java:72) 
ERROR/AndroidRuntime(487):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
ERROR/AndroidRuntime(487):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
ERROR/AndroidRuntime(487):  ... 11 more 
+1

有什麼錯誤日誌?從'LogCat'讀取輸出 –

+0

我添加了日誌。謝謝... – luigi7up

+0

'RunTrainingWorkoutsView.onCreate()'在這個方法中,第72行在這個文件中'RunTrainingWorkoutsView.java',一個對象是'NULL'。一探究竟! –

回答

6

後我敲我的頭撞在牆上了3天,並通過谷歌搜索得到它,stacOverflowing等

其實,這是這個愚蠢的小東西.. 。

我的XML文件,我定義了包含一些常見的Android視圖(textView和按鈕即佈局)和我的自定義視圖CounterClockView我有:

<training.timer.CounterClockView 
android:id="@+id/counter_clock_surface" 
android:layout_width="300dp" 
android:layout_height="240dp"> 

,我不得不增加了一個線!

<training.timer.CounterClockView 
    xmlns:android="http://schemas.android.com/apk/res/android" !!! 
    android:id="@+id/counter_clock_surface" 
    android:layout_width="300dp" 
    android:layout_height="240dp"> 
</training.timer.CounterClockView> 

我不知道爲什麼這個命名空間行做了如此巨大的差異,但它很好用!

現在,我可以從我的主要活動更新每個onTick()CountDownTimer的()我的自定義視圖...

以下的答案是非常有益的: findViewById() returns null for custom component in layout XML, not for other components

2

當初同樣的問題,所以我只是在我的自定義視圖的Java類中實現了所有三個構造函數,所有這三個構造函數都加上了onDrow方法,並且它像一個魅力一樣工作。嘗試一下。

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.util.AttributeSet; 
import android.view.View; 

public class CustomWorldWideView extends View { 

    public CustomWorldWideView(Context context) { 

     super(context); 
    } 

    public CustomWorldWideView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomWorldWideView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     //Some simple draw on the view... 
     Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.FILL); 
     paint.setColor(Color.parseColor("#FFA800")); 


     Path path = new Path(); 

     path.moveTo(0, 0); 
     path.lineTo(getWidth()/2, 0); 
     path.lineTo(getWidth(), getHeight()/2); 
     path.lineTo(getWidth()/2, getHeight()); 
     path.lineTo(0, getHeight()); 
     path.lineTo(0, 0); 

     canvas.drawPath(path, paint); 


    } 
} 

的XML:

 <PackageName.CustomWorldWideView 
      android:layout_width="56.00dp" 
      android:layout_height="43dp" 
      android:id="@+id/world_wide_grid_view_2" 
      />