[英]Put TextView inside the View
我正在嘗試這個SnowFall項目。 http://code.google.com/p/android-30days-apps/source/browse/trunk/08day/src/com/bakhtiyor/android/snowfall/SnowFall.java?r=27
我需要將TextView放在View類中(類SnowFallView擴展了View,第42行),我該怎么做。
我可以實例化TextView並添加它嗎?
package in.isuru.animate;
public class SnowFall extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SnowFallView snowFallView = new SnowFallView(this);
setContentView(snowFallView);
snowFallView.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_image));
}
private class SnowFallView extends View {
private int snow_flake_count = 5;
private final List<Drawable> drawables = new ArrayList<Drawable>();
private int[][] coords;
private final Drawable snow_flake;
private TextView countDownView;
public SnowFallView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
snow_flake = context.getResources().getDrawable(R.drawable.snow_flake);
snow_flake.setBounds(0, 0, snow_flake.getIntrinsicWidth(), snow_flake
.getIntrinsicHeight());
countDownView = new TextView(context);
countDownView.setText("It's working");
addContentView(countDownView, null);
//LayoutInflater inflater = getLayoutInflater();
//getWindow().addContentView(inflater.inflate(R.layout.text_layout, null), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
//ViewGroup.LayoutParams.FILL_PARENT));
}
@Override
protected void onSizeChanged(int width, int height, int oldw, int oldh) {
super.onSizeChanged(width, height, oldw, oldh);
Random random = new Random();
Interpolator interpolator = new LinearInterpolator();
snow_flake_count = Math.max(width, height) / 20;
coords = new int[snow_flake_count][];
drawables.clear();
for (int i = 0; i < snow_flake_count; i++) {
Animation animation = new TranslateAnimation(0, height / 10
- random.nextInt(height / 5), 0, height + 30);
animation.setDuration(10 * height + random.nextInt(5 * height));
animation.setRepeatCount(-1);
animation.initialize(10, 10, 10, 10);
animation.setInterpolator(interpolator);
coords[i] = new int[] { random.nextInt(width - 30), -30 };
drawables.add(new AnimateDrawable(snow_flake, animation));
animation.setStartOffset(random.nextInt(20 * height));
animation.startNow();
}
}
@Override
protected void onDraw(Canvas canvas) {
for (int i = 0; i < snow_flake_count; i++) {
Drawable drawable = drawables.get(i);
canvas.save();
canvas.translate(coords[i][0], coords[i][1]);
drawable.draw(canvas);
canvas.restore();
}
invalidate();
}
}
}
但是,當我這樣做時,我得到一個錯誤。
06-04 00:22:22.364:E / AndroidRuntime(359):java.lang.RuntimeException:無法啟動活動ComponentInfo {in.isuru.animate / in.isuru.animate.SnowFall}:java.lang.NullPointerException
PS。 所有導入完成。
是的,您可以像這樣在運行時添加TextView
。 假設MyClass
子類為android.content.Context
。 我不確定在問題的第二部分中您要問什么布局。 您已經有了容器布局(linearLayout)。
countDownView = new TextView(context);
countDownView.setText("It's working");
addContentView(countDownView, null);
我認為您的問題出在這里:
addContentView(countDownView, null);
你應該使用
addView(countDownView)
而不是addContentView
我不確定這是否100%正確,但是您需要執行以下操作...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
SnowFallView snowFallView = new SnowFallView(this);
TextView txt1 = new TextView(this);
linearLayout.addView(snowFallView);
linearLayout.addView(txt1);
setContentView(linearLayout);
snowFallView.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_image));
}
還要從SnowFallView
類中刪除這些行。
countDownView = new TextView(context);
countDownView.setText("It's working");
addContentView(countDownView, null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.