I'm trying this SnowFall project. http://code.google.com/p/android-30days-apps/source/browse/trunk/08day/src/com/bakhtiyor/android/snowfall/SnowFall.java?r=27
And I need to put TextView inside View class(class SnowFallView extends View, line 42), how can I do it.
Can I instantiate TextView and add it like this?
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();
}
}
}
But when I do this I get an error.
06-04 00:22:22.364: E/AndroidRuntime(359): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.isuru.animate/in.isuru.animate.SnowFall}: java.lang.NullPointerException
PS. All imports done.
Yes, you can add a TextView
at run-time like that. This assumes that MyClass
subclasses android.content.Context
. I'm not sure what layout you are asking about in the second part of the question. You already have the container layout (linearLayout).
countDownView = new TextView(context);
countDownView.setText("It's working");
addContentView(countDownView, null);
I think that your problem lays here:
addContentView(countDownView, null);
you should use
addView(countDownView)
instead of addContentView
I'm not sure if this is 100% correct but you need to do something like this...
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));
}
Also remove these lines from the SnowFallView
class.
countDownView = new TextView(context);
countDownView.setText("It's working");
addContentView(countDownView, null);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.