我正在使用eclipse开发一个Android应用程序。每次我在AVD中运行它都会出现错误“不幸已停止”。我试图使用Android动画在启动活动和主菜单活动之间转换。 以下是支持主要活动的java代码的一部分:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_up);
startAnimating();
}
private void startAnimating() {
// TODO Auto-generated method stub
TextView logo1 = (TextView) findViewById(R.id.title);
Animation fade1 = AnimationUtils.loadAnimation(this, R.animator.fade_in);
logo1.startAnimation(fade1);
TextView logo2 = (TextView) findViewById(R.id.secondTV);
Animation fade2 = AnimationUtils.loadAnimation(this, R.animator.fade_in2);
logo2.startAnimation(fade2);
fade2.setAnimationListener(new AnimationListener(){
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
startActivity(new Intent(Startup.this, SymptomViewer.class));
Startup.this.finish();
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
}
protected void onPause(){
super.onPause();
TextView logo1 = (TextView) findViewById(R.id.title);
logo1.clearAnimation();
TextView logo2 = (TextView) findViewById(R.id.secondTV);
logo2.clearAnimation();
}
protected void onResume(){
super.onResume();
startAnimating();
}
}
我一直在互联网上的论坛上搜索,我似乎无法解决这个问题。但是我检查了logcat,我发现了与Null指针异常有关的事情,这在引用时会涉及到。我已经多次运行我的代码,看起来很好。可能导致这种情况的原因。
这是LogCat
05-06 18:24:58.925: D/AndroidRuntime(1355): Shutting down VM
05-06 18:24:58.925: W/dalvikvm(1355): threadid=1: thread exiting with uncaught exception (group=0xb2a98ba8)
05-06 18:24:58.965: E/AndroidRuntime(1355): FATAL EXCEPTION: main
05-06 18:24:58.965: E/AndroidRuntime(1355): Process: com.example.ubumitrial1, PID: 1355
05-06 18:24:58.965: E/AndroidRuntime(1355): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ubumitrial1/com.example.ubumitrial1.Startup}: java.lang.NullPointerException
05-06 18:24:58.965: E/AndroidRuntime(1355): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-06 18:24:58.965: E/AndroidRuntime(1355): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-06 18:24:58.965: E/AndroidRuntime(1355): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-06 18:24:58.965: E/AndroidRuntime(1355): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-06 18:24:58.965: E/AndroidRuntime(1355): at android.os.Handler.dispatchMessage(Handler.java:102)
05-06 18:24:58.965: E/AndroidRuntime(1355): at android.os.Looper.loop(Looper.java:136)
05-06 18:24:58.965: E/AndroidRuntime(1355): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-06 18:24:58.965: E/AndroidRuntime(1355): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 18:24:58.965: E/AndroidRuntime(1355): at java.lang.reflect.Method.invoke(Method.java:515)
05-06 18:24:58.965: E/AndroidRuntime(1355): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-06 18:24:58.965: E/AndroidRuntime(1355): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-06 18:24:58.965: E/AndroidRuntime(1355): at dalvik.system.NativeStart.main(Native Method)
05-06 18:24:58.965: E/AndroidRuntime(1355): Caused by: java.lang.NullPointerException
05-06 18:24:58.965: E/AndroidRuntime(1355): at com.example.ubumitrial1.Startup.startAnimating(Startup.java:27)
05-06 18:24:58.965: E/AndroidRuntime(1355): at com.example.ubumitrial1.Startup.onCreate(Startup.java:17)
05-06 18:24:58.965: E/AndroidRuntime(1355): at android.app.Activity.performCreate(Activity.java:5231)
05-06 18:24:58.965: E/AndroidRuntime(1355): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-06 18:24:58.965: E/AndroidRuntime(1355): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-06 18:24:58.965: E/AndroidRuntime(1355): ... 11 more
05-06 18:25:02.275: I/Process(1355): Sending signal. PID: 1355 SIG: 9
答案 0 :(得分:0)
05-06 18:24:58.965: E/AndroidRuntime(1355): Caused by: java.lang.NullPointerException
05-06 18:24:58.965: E/AndroidRuntime(1355): at com.example.ubumitrial1.Startup.startAnimating(Startup.java:27)
这些日志行显示NPE发生在第27行。转到该行(双击" at" Eclipse中的行跳转到代码)
这可能是一行:widget.doSomething()
。由于widget
为null
(之前通过findViewById
初始化可能失败,您必须修复布局[或代码]中的ID),因此抛出NPE。
答案 1 :(得分:0)
FATAL EXCEPTION: main
05-06 18:24:58.965: E/AndroidRuntime(1355): Process: com.example.ubumitrial1, PID: 1355
05-06 18:24:58.965: E/AndroidRuntime(1355): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ubumitrial1/com.example.ubumitrial1.Startup}: java.lang.NullPointerException
05-06 18:24:58.965: E/AndroidRuntime(1355): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
根据此错误消息,我感觉您可能没有在Manifest文件中正确声明此活动。你有正确的定义吗?请仔细检查声明
Caused by: java.lang.NullPointerException
05-06 18:24:58.965: E/AndroidRuntime(1355): at com.example.ubumitrial1.Startup.startAnimating(Startup.java:27)
05-06 18:24:58.965: E/AndroidRuntime(1355): at com.example.ubumitrial1.Startup.onCreate(Startup.java:17)
根据此消息,您指的是未正确初始化的组件。请检查第17和27行。这些都有助于解决您的问题。
如果无法解决问题,请随意张贴突出显示行号的代码以及清单文件。
答案 2 :(得分:0)
我得到了ubuntu 14.04 LTS的解决方案 转到终端类型" android AVD"然后等到模拟器启动。现在去eclipse运行你的android项目并强制项目运行抛出在线模拟器(仍在运行)。 希望对你有效。 干杯!