2016-05-10 44 views
1

因此,我在Android Studio中執行的項目中遇到了NullPointerException異常。我需要獲得一個圖像以顯示在我稱爲DrawSurface的自定義類中。我似乎無法找到任何與我的問題有關的事情。我檢查了以前的各種問題並觀看了很多視頻。自定義類中的Android SurfaceView

我拋出的錯誤是這樣的:

顯示java.lang.NullPointerException在android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1269)在android.graphics.Canvas.drawBitmap(帆布.java:1325)at edu。########。#####。cse.treasurehunter。###########。DrawSurface.onDraw_Original(DrawSurface.java:60)at EDU。########。#####。cse.treasurehunter。###########。DrawSurface.onDraw(DrawSurface.java:-1)

我的.xml如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".MainActivity"> 
<edu.########.####.cse.treasurehunter.###########.DrawSurface android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/dsField"/> 
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="50dp"> 
    <Button android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.5" android:text="View Inventory" android:id="@+id/btnInventory"/> 
    <Button android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.5" android:text="Credits" android:id="@+id/btnCredits"/> 
</LinearLayout> 

而且我DrawSurface類看起來是這樣的:

public class DrawSurface extends SurfaceView { 
private Bitmap mBMPField; 
private SurfaceHolder holder; 

public DrawSurface(Context context) { 
    super(context); 
    holder = getHolder(); 
    holder.addCallback(new SurfaceHolder.Callback() { 

     @Override 
     public void surfaceCreated(SurfaceHolder holder) { 

     } 

     @Override 
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
      Canvas c = holder.lockCanvas(); 
      onDraw(c); 
      holder.unlockCanvasAndPost(c); 
     } 

     @Override 
     public void surfaceDestroyed(SurfaceHolder holder) { 

     } 
    }); 
    mBMPField = BitmapFactory.decodeResource(getResources(), R.drawable.field); 
} 

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

public DrawSurface(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawColor(Color.DKGRAY); 
    canvas.drawBitmap(mBMPField, 100, 200, null); 
} 

}

回答

0
  1. 不覆蓋onDraw()如果你的目標是繪製在SurfaceView的表面(而不是SurfaceView的視圖)。 onDraw()用於custom views。如果您真的想使用Surface,請將該方法命名爲其他內容。
  2. 除非您發佈靜態圖像,否則不要僅在surfaceChanged()中繪製。當Surface首次創建時,該方法通常會被調用一次,除非改變Surface的大小。如果您只想從位圖獲取靜態圖像,請考慮使用ImageView。
  3. 不要假設lockCanvas()會成功。從surfaceChanged()調用它非常安全,但通常情況下,您需要檢查返回值。
  4. 不要假設BitmapFactory.decodeResource()會成功。

由於您的drawColor()呼叫成功,而你drawBitmap()調用失敗,我的猜測是,mBMPField爲空。根據該文檔爲decodeResource()

[返回]解碼的位圖,或NULL,如果圖像數據不能被解碼,...

它返回的錯誤空,而不是拋出異常,所以你需要檢查返回值。

+0

好吧,所以我從'onDraw()'清除了我的覆蓋並重新命名了它。我也想要設置靜態圖像,所以這個imageView就足夠了?我試着在頂部附近定義'mBMPField',但是會用私人掛斷嗎?我非常感謝你的幫助,我對Android和Java都很陌生,但我很興奮學習。 – cleverCorvid

+0

如果你是新手,我會遠離SurfaceView。請使用自定義視圖(請參閱答案中的鏈接)或者僅使用一個股票ImageView,它專門用於顯示靜態位圖。這些都不能解決核心問題:是'decodeResource()'失敗,如果是這樣,爲什麼?記錄返回值,並檢查logcat輸出。 – fadden

+0

我從logcat獲得這個錯誤:'05-10 18:43:40.389 2461-2583/edu。########。cse。###########。treasurehunter _ ## ######### E /表面:getSlotFromBufferLocked:未知的緩衝區:0xb4058040' – cleverCorvid

0

好吧,發生了什麼是我沒有在我的構造函數中聲明的初始化函數。所以一旦我開始使用Init();我得到了它的工作。再次感謝您的指示和方向。沒有它,我不會注意到這部分。

相關問題