0

我的變量hz爲null,這是爲什麼?引用佈局視圖時出現空指針異常

代碼:

 HorizontalScrollView hz = (HorizontalScrollView) findViewById((R.id.horizontalScrollView2)); 
     hz.addView(imageView, params); 
     hz.setBackgroundColor(Color.BLACK); 

// Show this layout in our activity. 
     ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView); 
     setContentView(scrollView); 

XML:

<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollView"> 

    <HorizontalScrollView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/horizontalScrollView2"> 

     <AbsoluteLayout 
      android:layout_width="1000dp" 
      android:layout_height="1000dp" 
      android:id="@+id/al"/> 
    </HorizontalScrollView> 

</ScrollView> 

而且它不會讓我有horizo​​ntalScrollView內我absoluteLayout。我正在嘗試製作比屏幕大小更大的可繪製區域,您可以上下滾動,在這裏繪製棋盤遊戲棋盤。有人可以幫我把這個可繪製區域設置好嗎?謝謝。

我的活動的所有代碼:因爲沒有在時間的活動的一部分,你試圖通過findViewById()方法,讓他們

package com.example.btf.game; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.os.Bundle; 
import android.widget.HorizontalScrollView; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.ScrollView; 


public class SplashScreen extends Activity { 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

// We'll be creating an image that is 100 pixels wide and 200 pixels tall. 
     int width = 1000; 
     int height = 3000; 

// Create a bitmap with the dimensions we defined above, and with a 16-bit pixel format. We'll 
// get a little more in depth with pixel formats in a later post. 
     Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

// Create a paint object for us to draw with, and set our drawing color to blue. 
     Paint paint = new Paint(); 
     paint.setColor(Color.BLUE); 

// Create a new canvas to draw on, and link it to the bitmap that we created above. Any drawing 
// operations performed on the canvas will have an immediate effect on the pixel data of the 
// bitmap. 
     Canvas canvas = new Canvas(bitmap); 

// Fill the entire canvas with a red color. 
     canvas.drawColor(Color.RED); 

// Draw a rectangle inside our image using the paint object we defined above. The rectangle's 
// upper left corner will be at (25,50), and the lower left corner will be at (75,150). Since we set 
// the paint object's color above, this rectangle will be blue. 
     canvas.drawRect(25, 50, 75, 150, paint); 

// In order to display this image in our activity, we need to create a new ImageView that we 
// can display. 
     ImageView imageView = new ImageView(this); 

// Set this ImageView's bitmap to the one we have drawn to. 
     imageView.setImageBitmap(bitmap); 

// Create a simple layout and add our image view to it. 
     RelativeLayout layout = new RelativeLayout(this); 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 
     params.addRule(RelativeLayout.CENTER_IN_PARENT); 


     // LinearLayout container = (LinearLayout) findViewById(R.id.container); 
     HorizontalScrollView hz = (HorizontalScrollView) findViewById((R.id.horizontalScrollView2)); 
     hz.addView(imageView, params); 
     hz.setBackgroundColor(Color.BLACK); 

// Show this layout in our activity. 
     ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView); 
     setContentView(scrollView); 
    } 
} 

回答

2

你的觀點是空的。

你需要調用setContentView()之前,「發現」的觀點,並把它傳給你的XML佈局:setContentView(R.layout.my_acitivty_layout)

+0

我認爲這是問題。我正在等待Android Studio在您回答時加載。感謝您的確認。 – AdamMc331 2014-10-26 21:19:38

+0

是的,這解決了這個問題。謝謝! – btf21 2014-10-26 21:29:38