hi i have a "strange" problem with my code this is what log says:
你好,我的代码有个“奇怪”的问题这就是log说的
06-15 17:06:38.054: E/AndroidRuntime(5699): FATAL EXCEPTION: main
06-15 17:06:38.054: E/AndroidRuntime(5699): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ball_anim/com.example.ball_anim.Main}: java.lang.NullPointerException
06-15 17:06:38.054: E/AndroidRuntime(5699): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-15 17:06:38.054: E/AndroidRuntime(5699): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-15 17:06:38.054: E/AndroidRuntime(5699): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-15 17:06:38.054: E/AndroidRuntime(5699): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-15 17:06:38.054: E/AndroidRuntime(5699): at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 17:06:38.054: E/AndroidRuntime(5699): at android.os.Looper.loop(Looper.java:137)
06-15 17:06:38.054: E/AndroidRuntime(5699): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-15 17:06:38.054: E/AndroidRuntime(5699): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 17:06:38.054: E/AndroidRuntime(5699): at java.lang.reflect.Method.invoke(Method.java:511)
06-15 17:06:38.054: E/AndroidRuntime(5699): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-15 17:06:38.054: E/AndroidRuntime(5699): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-15 17:06:38.054: E/AndroidRuntime(5699): at dalvik.system.NativeStart.main(Native Method)
06-15 17:06:38.054: E/AndroidRuntime(5699): Caused by: java.lang.NullPointerException
06-15 17:06:38.054: E/AndroidRuntime(5699): at com.example.ball_anim.Main$MyView.<init>(Main.java:86)
06-15 17:06:38.054: E/AndroidRuntime(5699): at com.example.ball_anim.Main.onCreate(Main.java:48)
06-15 17:06:38.054: E/AndroidRuntime(5699): at android.app.Activity.performCreate(Activity.java:5008)
06-15 17:06:38.054: E/AndroidRuntime(5699): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-15 17:06:38.054: E/AndroidRuntime(5699): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-15 17:06:38.054: E/AndroidRuntime(5699): ... 11 more
the error appears when calling class custom methods GameTime in the Main class..this is my class GameTime.java
当在主类中调用类自定义方法游戏时间时出现错误。这是我的类游戏。java。
public class GameTime extends CountDownTimer{
public long Tempo_Trascorso;
private final long startTime = 10000;
private boolean End=false;
private long Tempo_Stop;
public GameTime(long startTime, long interval){
super(startTime, interval);
}
@Override
public void onFinish(){
System.out.println("Finish!");
End = true;
}
@Override
public void onTick(long millisUntilFinished){
System.out.println("Time Restante:" + millisUntilFinished);
Tempo_Trascorso = startTime - millisUntilFinished;
if(End == false){
Tempo_Stop=millisUntilFinished;
}
}
//Custom method #1 get the time when the game thread is stopped
public long getLastTime(){
return Tempo_Stop;
}
//Custom method #2 get the true/false when the time is end
public boolean getEndGame(){
return End;
}
}
Main.java
Main.java
public class Main extends Activity {
private Bitmap Player_bmp,Player_bmp_StateLV1,Player_bmp_StateLV2,Player_bmp_StateLV3,Life_bmp,Lose_bmp;
private SurfaceHolder holder;
private AnimationThread AnimThread;
private GameTime GameTime;
private DialogBox Dbox;
private Random rnd;
private int Display_width,Display_height;
private int RandomPosx;
private int Rosso,Verde,Blu;
private Paint paint,paint_L;
private float deltaX,deltaY;
private boolean Drag = false,Collision = false,Stop = false;
private int Static_PositionX,Static_PositionY;
private List<Sfere> C_Sfere = new ArrayList<Sfere>();
private int Life,LostLife=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new MyView(this));
}
public class MyView extends SurfaceView {
public MyView(Context context) {
super(context);
// More code(too long)
}
Edit: this is the OnCreate of the main and myview class when create surfaceview and game stuff, do not place all of the code because it is too long,if need more info tell me;)
编辑:这是主视图和myview类的OnCreate,当创建surfaceview和游戏内容时,不要把所有的代码放在一起,因为太长了,如果需要更多的信息告诉我;
anywhere in the main class, call the custom methods of the application GameTime gives me. I want to clarify that where the main class gives me error in the class dedicated to the dialogbox (for example) do not have errors error
在主类的任何地方,调用应用程序游戏时间的自定义方法给我。我想要澄清的是,在专用于对话框(例如)的类中,主类给我错误的地方没有错误。
2 个解决方案
#1
3
Having a NullPointerException
is one of the most common error you'll get when writing a program. Hopefully, it's one of the easiest error to fix.
在编写程序时,使用NullPointerException是最常见的错误之一。希望这是最容易修正的错误之一。
When a NullPointerException
is thrown ?
何时抛出NullPointerException ?
Thrown when an application attempts to use null in a case where an object is required. These include:
当应用程序试图在需要对象的情况下使用null时抛出。这些包括:
- Calling the instance method of a null object.
- 调用null对象的实例方法。
- Accessing or modifying the field of a null object.
- 访问或修改空对象的字段。
- Taking the length of null as if it were an array.
- 取空的长度,就像它是一个数组一样。
- Accessing or modifying the slots of null as if it were an array.
- 访问或修改空槽,好像它是一个数组。
- Throwing null as if it were a Throwable value.
- 扔空,好像它是一个可扔的值。
- Applications should throw instances of this class to indicate other illegal uses of the null object.
- 应用程序应该抛出该类的实例,以指示空对象的其他非法使用。
Now that you read the possible cases when it is thrown, learn to read messages from the logcat, they're often informative. In your case the important information to read is from the Caused By
line :
现在您已经阅读了可能的情况,当它被抛出时,学习从logcat读取消息,它们通常是信息丰富的。在你的情况下,阅读的重要信息是由线路引起的:
06-15 17:06:38.054: E/AndroidRuntime(5699): Caused by: java.lang.NullPointerException
06-15 17:06:38.054: E/AndroidRuntime(5699): at com.example.ball_anim.Main$MyView.<init>(Main.java:86)
06-15 17:06:38.054: E/AndroidRuntime(5699): at com.example.ball_anim.Main.onCreate(Main.java:48)
06-15 17:06:38.054: E/AndroidRuntime(5699): at android.app.Activity.performCreate(Activity.java:5008)
If you look closely, you will see that the error is thrown at the line 86 of your Main.java
file (i.e at com.example.ball_anim.Main$MyView.<init>(Main.java:86)
).
如果仔细观察,就会发现错误被抛出到Main的第86行。(我的java文件。e com.example.ball_anim.Main MyView美元。< init >(Main.java:86))。
So take a look at this line and see what might be null
. In your case you declared your variable GameTime
but you forgot to initialize it so when you try to call the method getLastTime()
on it, a NPE
is thrown.
看一下这条线,看看什么是空的。在您的情况下,您声明了可变的游戏时间,但是您忘记了初始化它,所以当您尝试调用getLastTime()方法时,会抛出一个NPE。
#2
0
In the log you can see that the exception occurred at
在日志中,您可以看到异常发生在。
06-15 17:06:38.054: E/AndroidRuntime(5699): Caused by: java.lang.NullPointerException 06-15 17:06:38.054: E/AndroidRuntime(5699): at com.example.ball_anim.Main$MyView.<init>(Main.java:86)
So you need to check your constructor of MyView
at line 86 in the file Main.java
.
因此,您需要在文件Main.java中的第86行检查MyView的构造函数。
You did not post its code so we can't help you more then that.
你没有发布它的代码,所以我们不能再帮助你了。
#1
3
Having a NullPointerException
is one of the most common error you'll get when writing a program. Hopefully, it's one of the easiest error to fix.
在编写程序时,使用NullPointerException是最常见的错误之一。希望这是最容易修正的错误之一。
When a NullPointerException
is thrown ?
何时抛出NullPointerException ?
Thrown when an application attempts to use null in a case where an object is required. These include:
当应用程序试图在需要对象的情况下使用null时抛出。这些包括:
- Calling the instance method of a null object.
- 调用null对象的实例方法。
- Accessing or modifying the field of a null object.
- 访问或修改空对象的字段。
- Taking the length of null as if it were an array.
- 取空的长度,就像它是一个数组一样。
- Accessing or modifying the slots of null as if it were an array.
- 访问或修改空槽,好像它是一个数组。
- Throwing null as if it were a Throwable value.
- 扔空,好像它是一个可扔的值。
- Applications should throw instances of this class to indicate other illegal uses of the null object.
- 应用程序应该抛出该类的实例,以指示空对象的其他非法使用。
Now that you read the possible cases when it is thrown, learn to read messages from the logcat, they're often informative. In your case the important information to read is from the Caused By
line :
现在您已经阅读了可能的情况,当它被抛出时,学习从logcat读取消息,它们通常是信息丰富的。在你的情况下,阅读的重要信息是由线路引起的:
06-15 17:06:38.054: E/AndroidRuntime(5699): Caused by: java.lang.NullPointerException
06-15 17:06:38.054: E/AndroidRuntime(5699): at com.example.ball_anim.Main$MyView.<init>(Main.java:86)
06-15 17:06:38.054: E/AndroidRuntime(5699): at com.example.ball_anim.Main.onCreate(Main.java:48)
06-15 17:06:38.054: E/AndroidRuntime(5699): at android.app.Activity.performCreate(Activity.java:5008)
If you look closely, you will see that the error is thrown at the line 86 of your Main.java
file (i.e at com.example.ball_anim.Main$MyView.<init>(Main.java:86)
).
如果仔细观察,就会发现错误被抛出到Main的第86行。(我的java文件。e com.example.ball_anim.Main MyView美元。< init >(Main.java:86))。
So take a look at this line and see what might be null
. In your case you declared your variable GameTime
but you forgot to initialize it so when you try to call the method getLastTime()
on it, a NPE
is thrown.
看一下这条线,看看什么是空的。在您的情况下,您声明了可变的游戏时间,但是您忘记了初始化它,所以当您尝试调用getLastTime()方法时,会抛出一个NPE。
#2
0
In the log you can see that the exception occurred at
在日志中,您可以看到异常发生在。
06-15 17:06:38.054: E/AndroidRuntime(5699): Caused by: java.lang.NullPointerException 06-15 17:06:38.054: E/AndroidRuntime(5699): at com.example.ball_anim.Main$MyView.<init>(Main.java:86)
So you need to check your constructor of MyView
at line 86 in the file Main.java
.
因此,您需要在文件Main.java中的第86行检查MyView的构造函数。
You did not post its code so we can't help you more then that.
你没有发布它的代码,所以我们不能再帮助你了。