当我尝试运行我的 android 应用程序时,它首先显示带有按钮的 MainActivity。该按钮应该启动一个服务,然后根据服务的结果显示另一个活动。但是当我单击按钮时,应用程序会关闭。我试图调试它,我得到了这个错误堆栈跟踪:DalvikVM [localhost:8615]。我应该怎么办?这是我的主要活动:
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonStart = (Button) findViewById(R.id.button1);
buttonStart.setOnClickListener(this);
}
public void onClick(View src) {
Toast.makeText(this, "OnClick", Toast.LENGTH_SHORT).show();
Intent serviceIntent = new Intent(src.getContext(), ForewarningService.class);
startService(serviceIntent);
}
}
这是我的服务:
public class ForewarningService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
Log.d(TAG, "onCreate");
}
public void onStart(Intent intent, int startid) {
Log.d(TAG, "onStart");
try{
ForewarningAnalysisMod.runForewarning();
}catch(Exception e)
{
e.printStackTrace();
Toast.makeText(this, "Exception e", Toast.LENGTH_SHORT).show();
}
}
public void onDestroy1() {
Log.d(TAG, "onDestroy1");
Intent attackIntent = new Intent(this, Result1Activity.class);
attackIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(attackIntent);
}
public void onDestroy2() {
Log.d(TAG, "onDestroy2");
Intent noneventIntent = new Intent(this, Result2Activity.class);
noneventIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(noneventIntent);
}
}
编辑:我再次运行应用程序,而不是在调试模式下,我的 logcat 说:
07-09 11:28:05.166: D/MyService(1606): onCreate
07-09 11:28:05.166: D/MyService(1606): onStart
07-09 11:28:05.196: W/System.err(1606): java.io.FileNotFoundException: /C:/ORNL-EEG/forewarn-COMPARATIVE-01/b12n49716w29s3d7l56m77gT-dmanalysis.csv (No such file or directory)
07-09 11:28:05.206: W/System.err(1606): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
07-09 11:28:05.206: W/System.err(1606): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
07-09 11:28:05.206: W/System.err(1606): at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
07-09 11:28:05.206: W/System.err(1606): at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
07-09 11:28:05.206: W/System.err(1606): at java.io.FileWriter.<init>(FileWriter.java:80)
07-09 11:28:05.206: W/System.err(1606): at edu.app.forewarn.model.LogWriter.<init>(LogWriter.java:17)
07-09 11:28:05.206: W/System.err(1606): at edu.app.forewarn.ui.ForewarningAnalysisMod.runAnalysis(ForewarningAnalysisMod.java:136)
07-09 11:28:05.206: W/System.err(1606): at edu.app.forewarn.ui.ForewarningAnalysisMod.runForewarning(ForewarningAnalysisMod.java:92)
07-09 11:28:05.206: W/System.err(1606): at com.orhs.forewarningapp.ForewarningService.onStart(ForewarningService.java:40)
07-09 11:28:05.206: W/System.err(1606): at android.app.Service.onStartCommand(Service.java:420)
07-09 11:28:05.216: W/System.err(1606): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
07-09 11:28:05.216: W/System.err(1606): at android.app.ActivityThread.access$3600(ActivityThread.java:125)
07-09 11:28:05.216: W/System.err(1606): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
07-09 11:28:05.216: W/System.err(1606): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 11:28:05.216: W/System.err(1606): at android.os.Looper.loop(Looper.java:123)
07-09 11:28:05.216: W/System.err(1606): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-09 11:28:05.216: W/System.err(1606): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 11:28:05.216: W/System.err(1606): at java.lang.reflect.Method.invoke(Method.java:521)
07-09 11:28:05.216: W/System.err(1606): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-09 11:28:05.216: W/System.err(1606): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-09 11:28:05.216: W/System.err(1606): at dalvik.system.NativeStart.main(Native Method)
07-09 11:28:05.226: I/AndroidRuntime(1606): AndroidRuntime onExit calling exit(-1)
而且文件夹说“它不存在”实际上确实如此,我检查了,所以我不知道为什么我会收到所有这些错误。