我一直在youtube上学习android的自定义视图等等。在我尝试做一个表面视图(相当简单的描述在这个视频)。
我所做的事情与视频中所显示的几乎是同步的。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainActivity extends AppCompatActivity {
Ui ui;
Bitmap ball;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
ui = new Ui(this);
ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
setContentView(ui);
}
@Override
protected void onResume() {
super.onResume();
ui.resume();
}
@Override
protected void onPause() {
super.onPause();
ui.pause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class Ui extends SurfaceView implements Runnable {
private Thread thread;
private SurfaceHolder surfaceHolder;
boolean ok = false;
public Ui(Context context) {
super(context);
thread = null;
surfaceHolder = getHolder();
}
@Override
public void run() {
while (ok) {
if (!surfaceHolder.getSurface().isValid()) {
continue;
}
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawARGB(200, 150, 130, 120);
canvas.drawBitmap(ball, 0, 0, null);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
public void pause() {
ok = false;
while (true) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
thread = null;
}
public void resume() {
ok = true;
thread = new Thread(this);
thread.run();
}
}
}
@Override
public void run() {
while (ok) {
if (!surfaceHolder.getSurface().isValid()) {
continue;
}
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawARGB(200, 150, 130, 120);
canvas.drawBitmap(ball, 0, 0, null);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
if (!surfaceHolder.getSurface().isValid())
06-25 15:23:46.211 2012-2012/com.pchakraverti.canvasapp D/AndroidRuntime﹕ Shutting down VM
06-25 15:23:46.211 2012-2012/com.pchakraverti.canvasapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa62f8288)
06-25 15:23:46.211 2012-2012/com.pchakraverti.canvasapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {com.pchakraverti.canvasapp/com.pchakraverti.canvasapp.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.pchakraverti.canvasapp.Ui.run(Ui.java:44)
at java.lang.Thread.run(Thread.java:856)
at com.pchakraverti.canvasapp.Ui.resume(Ui.java:66)
at com.pchakraverti.canvasapp.MainActivity.onPostResume(MainActivity.java:24)
at android.app.Activity.performResume(Activity.java:5095)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
06-25 15:23:46.227 2012-2015/com.pchakraverti.canvasapp D/dalvikvm﹕ GC_CONCURRENT freed 208K, 3% free 10919K/11207K, paused 12ms+0ms, total 15ms
06-25 15:25:15.043 2067-2067/com.pchakraverti.canvasapp D/dalvikvm﹕ Late-enabling CheckJNI
06-25 15:25:15.091 2067-2070/com.pchakraverti.canvasapp D/dalvikvm﹕ GC_CONCURRENT freed 98K, 3% free 11074K/11335K, paused 11ms+0ms, total 23ms
06-25 15:25:15.095 2067-2067/com.pchakraverti.canvasapp D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 3% free 11073K/11335K, paused 4ms, total 4ms
06-25 15:25:15.131 2067-2067/com.pchakraverti.canvasapp I/dalvikvm-heap﹕ Grow heap (frag case) to 11.887MB for 1048588-byte allocation
06-25 15:25:15.155 2067-2070/com.pchakraverti.canvasapp D/dalvikvm﹕ GC_CONCURRENT freed 0K, 3% free 12097K/12423K, paused 15ms+0ms, total 22ms
06-25 15:25:15.155 2067-2067/com.pchakraverti.canvasapp D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 0ms
06-25 15:25:15.163 2067-2067/com.pchakraverti.canvasapp I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
06-25 15:25:15.163 2067-2067/com.pchakraverti.canvasapp W/dalvikvm﹕ VFY: unable to resolve virtual method 13337: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
06-25 15:25:15.163 2067-2067/com.pchakraverti.canvasapp D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
06-25 15:25:15.163 2067-2067/com.pchakraverti.canvasapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
06-25 15:25:15.163 2067-2067/com.pchakraverti.canvasapp W/dalvikvm﹕ VFY: unable to resolve virtual method 408: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
06-25 15:25:15.163 2067-2067/com.pchakraverti.canvasapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
06-25 15:25:15.167 2067-2067/com.pchakraverti.canvasapp I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
06-25 15:25:15.167 2067-2067/com.pchakraverti.canvasapp W/dalvikvm﹕ VFY: unable to resolve virtual method 430: Landroid/content/res/TypedArray;.getType (I)I
06-25 15:25:15.167 2067-2067/com.pchakraverti.canvasapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
06-25 15:25:15.171 2067-2067/com.pchakraverti.canvasapp D/AndroidRuntime﹕ Shutting down VM
06-25 15:25:15.171 2067-2067/com.pchakraverti.canvasapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa62f8288)
06-25 15:25:15.171 2067-2067/com.pchakraverti.canvasapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {com.pchakraverti.canvasapp/com.pchakraverti.canvasapp.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.pchakraverti.canvasapp.Ui.run(Ui.java:45)
at java.lang.Thread.run(Thread.java:856)
at com.pchakraverti.canvasapp.Ui.resume(Ui.java:66)
at com.pchakraverti.canvasapp.MainActivity.onPostResume(MainActivity.java:24)
at android.app.Activity.performResume(Activity.java:5095)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
06-25 15:26:56.039 2120-2125/com.pchakraverti.canvasapp I/dalvikvm﹕ threadid=3: reacting to signal 3
也请看我自己发的这个问题,其实很相似:
view.surfaceView,为什么它的成员mSurfaceHolder从getSurface()返回null?
我还阅读了android sdk 22中SurfaceView.java的源代码,但我注意到成员surfaceView.msurfaceHolder.getSurface()总是返回null。真让我受不了!
问题内容: 我最近正在学习Java并发编程。我知道关键字可以保证安全的发布。但是,当我阅读源代码时,发现and 字段未使用关键字。我发现该方法在方法中被调用,并且该方法直接将值分配给。此时,可能是因为未使用声明。我的理解正确吗?虽然可以保证读写线程的安全性,但是可以 保证不是一个正确的初始值 问题答案: 根据此博客文章https://shipilev.net/blog/2014/safe-publ
当你点击btnChangePass,我得到错误。 我有一个错误: FATAL EXCEPTION:主进程:com.company.learn2,PID:18854java.lang.NullPointerExcture atcom.company.learn2。Heslo.change密码(Heslo.java:84)在com.company.learn2。Heslo.on点击(Heslo.jav
问题内容: 有可能这可能是一个双重问题。我将String变量初始化为null。我可能会或可能不会使用一个值更新它。现在我想检查此变量是否不等于null以及我尝试执行的操作是否会得到null指针异常。空指针异常,因为它代价高昂。是否有任何有效的解决方法.TIA 问题答案: 如果您使用 你 不会 得到。 我怀疑你在做什么: 这是因为null 而引发,而不是因为null。 如果仍然无法解释,请发布您用于
我已经更新了我的项目中的一些依赖关系之后,我的Hibernate配置类显示Nullpointerx的。 我将SpringDataJPA存储库与hibernate一起使用,已经超过24小时了,仍然没有找到任何关于小问题的适当解决方案。 我已经尝试过的一些解决方案:- 使用@bean(name=“entityManagerFactory”)提供bean名称 我面临的问题 波姆。xml文件 配置类 db
当我试图从DB查询结果时,我得到一个空指针异常。以下是错误: 这是错误产生的函数。 如果我启动应用程序信息服务(=new ApplicationInfoService()),我会得到一个指向我的服务类的空指针错误,在这里进行查询 错误转到createQuery行,如果我打印出EntityManager,它将返回“null”。为什么我的Entitymanager不能初始化。我在我的登录系统中使用了几
问题内容: 嗨,我们一直在使用apache编写我们的Java程序来读取xls和xlsx文件,问题是我们由于两个原因而得到了空指针异常。第一个是已经解决的空白单元格,另一个是当我们选择一个没有任何记录的列。 我们的程序要求输入excel文件的路径,然后输入要读取的文件的特定工作表号和特定的工作表的列号。这是读取xls文件的代码 变量工作表名称用于excel文件的工作表编号 变量cols用于您要阅读的