想要从Android应用程序视图中的图像资源ask使用draw methode创建对象类吗
我想用DrawMethode从Android应用程序视图中的图像资源ask创建对象类。 我有3个java文件: Balle.java:想要从Android应用程序视图中的图像资源ask使用draw methode创建对象类吗,android,view,nullpointerexception,drawable,getresource,Android,View,Nullpointerexception,Drawable,Getresource,我想用DrawMethode从Android应用程序视图中的图像资源ask创建对象类。 我有3个java文件: Balle.java: package com.example.balle; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics
package com.example.balle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
public class Balle extends Activity {
float radius = 75; // Radius of the Bubble
float x = radius + 20; // Center of the Bubble (x,y)
float y = radius + 40;
Context context = BalleView.GlobalContext;
Resources res = context.getResources();
// Constructor
public Balle() {
}
public void draw(Canvas canvas) {
// show objet from the image
Bitmap monImage = BitmapFactory.decodeResource(res, R.drawable.bubble);
canvas.drawBitmap(monImage, x-radius, y-radius, null);
}
}
BalleView.java:
package com.example.balle;
import com.example.balle.Balle;
import android.content.Context;
import android.graphics.Canvas;
import android.view.View;
public class BalleView extends View {
private Balle ball;
public static Context GlobalContext = null;
// Constructor
public BalleView(Context context) {
super(context);
ball = new Balle();
}
@Override
protected void onDraw(Canvas canvas) {
// draw the bubble
// show objet from the image
ball.draw(canvas);
}
}
package com.example.balle;
import com.example.balle.Balle;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
public class BalleView extends View {
public static Context GlobalContext = null;
private Paint paint;
// Constructor
public BalleView(Context context) {
super(context);
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
// show objet from the image
Bitmap monImage = BitmapFactory.decodeResource(getResources(), R.drawable.bubble);
canvas.drawBitmap(monImage, 40, 40, paint);
}
}
MainActivity.java:
package com.example.balle;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View BalleView = new BalleView(this);
setContentView(BalleView);
BalleView.setBackgroundColor(Color.WHITE);
}
}
作为我在res/drawable中的图像资源:
控制台上有一条空指针错误消息:
08-09 04:05:14.791: D/AndroidRuntime(1232): Shutting down VM
08-09 04:05:14.791: W/dalvikvm(1232): threadid=1: thread exiting with uncaught exception (group=0xb4a81ba8)
08-09 04:05:14.851: E/AndroidRuntime(1232): FATAL EXCEPTION: main
08-09 04:05:14.851: E/AndroidRuntime(1232): Process: com.example.balle, PID: 1232
08-09 04:05:14.851: E/AndroidRuntime(1232): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.balle/com.example.balle.MainActivity}: java.lang.NullPointerException
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.os.Handler.dispatchMessage(Handler.java:102)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.os.Looper.loop(Looper.java:136)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-09 04:05:14.851: E/AndroidRuntime(1232): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 04:05:14.851: E/AndroidRuntime(1232): at java.lang.reflect.Method.invoke(Method.java:515)
08-09 04:05:14.851: E/AndroidRuntime(1232): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-09 04:05:14.851: E/AndroidRuntime(1232): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-09 04:05:14.851: E/AndroidRuntime(1232): at dalvik.system.NativeStart.main(Native Method)
08-09 04:05:14.851: E/AndroidRuntime(1232): Caused by: java.lang.NullPointerException
08-09 04:05:14.851: E/AndroidRuntime(1232): at com.example.balle.Balle.<init>(Balle.java:15)
08-09 04:05:14.851: E/AndroidRuntime(1232): at com.example.balle.BalleView.<init>(BalleView.java:16)
08-09 04:05:14.851: E/AndroidRuntime(1232): at com.example.balle.MainActivity.onCreate(MainActivity.java:13)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.Activity.performCreate(Activity.java:5231)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
08-09 04:05:14.851: E/AndroidRuntime(1232): ... 11 more
08-09 04:05:14.791:D/AndroidRuntime(1232):关闭虚拟机
08-09 04:05:14.791:W/dalvikvm(1232):线程ID=1:线程以未捕获异常退出(组=0xb4a81ba8)
08-09 04:05:14.851:E/AndroidRuntime(1232):致命异常:主
08-09 04:05:14.851:E/AndroidRuntime(1232):进程:com.example.balle,PID:1232
08-09 04:05:14.851:E/AndroidRuntime(1232):java.lang.RuntimeException:无法启动活动组件信息{com.example.balle/com.example.balle.MainActivity}:java.lang.NullPointerException
08-09 04:05:14.851:E/AndroidRuntime(1232):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
08-09 04:05:14.851:E/AndroidRuntime(1232):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
08-09 04:05:14.851:E/AndroidRuntime(1232):在android.app.ActivityThread.access$800(ActivityThread.java:135)
08-09 04:05:14.851:E/AndroidRuntime(1232):位于android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-09 04:05:14.851:E/AndroidRuntime(1232):位于android.os.Handler.dispatchMessage(Handler.java:102)
08-09 04:05:14.851:E/AndroidRuntime(1232):在android.os.Looper.loop(Looper.java:136)上
08-09 04:05:14.851:E/AndroidRuntime(1232):位于android.app.ActivityThread.main(ActivityThread.java:5001)
08-09 04:05:14.851:E/AndroidRuntime(1232):位于java.lang.reflect.Method.Invokenactive(本机方法)
08-09 04:05:14.851:E/AndroidRuntime(1232):位于java.lang.reflect.Method.invoke(Method.java:515)
08-09 04:05:14.851:E/AndroidRuntime(1232):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-09 04:05:14.851:E/AndroidRuntime(1232):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-09 04:05:14.851:E/AndroidRuntime(1232):在dalvik.system.NativeStart.main(本机方法)
08-09 04:05:14.851:E/AndroidRuntime(1232):由以下原因引起:java.lang.NullPointerException
08-09 04:05:14.851:E/AndroidRuntime(1232):在com.example.balle.balle.(balle.java:15)
08-09 04:05:14.851:E/AndroidRuntime(1232):位于com.example.balle.BalleView。(BalleView.java:16)
08-09 04:05:14.851:E/AndroidRuntime(1232):位于com.example.balle.MainActivity.onCreate(MainActivity.java:13)
08-09 04:05:14.851:E/AndroidRuntime(1232):在android.app.Activity.performCreate(Activity.java:5231)上
08-09 04:05:14.851:E/AndroidRuntime(1232):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)上
08-09 04:05:14.851:E/AndroidRuntime(1232):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
08-09 04:05:14.851:E/AndroidRuntime(1232):。。。还有11个
谢谢你的帮助
我使用了调试器,我认为代码和资源是正确的,但它有一些与IDE Eclipse相关的文件/文件夹没有正确配置。我有两个警告:
错误:找不到BaseDexClassLoader.class>类文件编辑器>源
错误:ActivityThread;performLaunchActivity(ActivityThread$ActivityClientRecord,Intent)>未找到源>编辑源查找路径
我有一个“Android.jar”文件,但我不知道如何在Eclipse中将源代码加入到我的项目中
谢谢你的帮助
注意:最终目标是移动屏幕上的气泡图像。在类上使用断点无法启动调试器。 文件“Android.jar”必须与Android sdk文件夹“sources”链接 最后,我在MainActivity.java中添加了“import com.example.balle.BalleView”。 对于此BalleView.java,文件Balle.java不是必需的:
package com.example.balle;
import com.example.balle.Balle;
import android.content.Context;
import android.graphics.Canvas;
import android.view.View;
public class BalleView extends View {
private Balle ball;
public static Context GlobalContext = null;
// Constructor
public BalleView(Context context) {
super(context);
ball = new Balle();
}
@Override
protected void onDraw(Canvas canvas) {
// draw the bubble
// show objet from the image
ball.draw(canvas);
}
}
package com.example.balle;
import com.example.balle.Balle;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
public class BalleView extends View {
public static Context GlobalContext = null;
private Paint paint;
// Constructor
public BalleView(Context context) {
super(context);
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
// show objet from the image
Bitmap monImage = BitmapFactory.decodeResource(getResources(), R.drawable.bubble);
canvas.drawBitmap(monImage, 40, 40, paint);
}
}
有什么想法吗?我从Eclipse市场安装了“Java Source Attacher”,但在android.jar和android-support-v4.jar的右侧clic中,我收到了一条错误消息:Library Source Location>抱歉,但在我们的数据库中找不到源代码。您可以通过在下面提供源代码存档的URL位置来帮助社区。