我的变量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>
此外,它不允许我在horizontalScrollView中使用absoluteLayout。我试图制作一个比屏幕尺寸更大的可绘制区域,你可以上下滚动,我将绘制一个棋盘游戏板。有人可以帮我设置这个可绘制的区域吗?谢谢。
我活动的所有代码:
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);
}
}
答案 0 :(得分:2)
您的观点为空,因为您尝试通过findViewById()
方法获取活动时没有活动的一部分。
您需要在“查找”视图之前致电setContentView()
并在其中传递您的xml布局:setContentView(R.layout.my_acitivty_layout)