我是android新手。我遵循了http://www.youtube.com/watch?v=1JyC_xv20yE的教程 程序中没有错误。但是模拟器给出了“应用程序意外停止”
将日志文件另存为
04-11 19:50:14.295: D/AndroidRuntime(338): Shutting down VM
04-11 19:50:14.295: W/dalvikvm(338): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-11 19:50:14.315: E/AndroidRuntime(338): FATAL EXCEPTION: main
04-11 19:50:14.315: E/AndroidRuntime(338): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sql/com.sql.SQLiteExample}: java.lang.ClassCastException: com.sql.SQLiteExample
04-11 19:50:14.315: E/AndroidRuntime(338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-11 19:50:14.315: E/AndroidRuntime(338): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-11 19:50:14.315: E/AndroidRuntime(338): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-11 19:50:14.315: E/AndroidRuntime(338): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-11 19:50:14.315: E/AndroidRuntime(338): at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 19:50:14.315: E/AndroidRuntime(338): at android.os.Looper.loop(Looper.java:123)
04-11 19:50:14.315: E/AndroidRuntime(338): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-11 19:50:14.315: E/AndroidRuntime(338): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 19:50:14.315: E/AndroidRuntime(338): at java.lang.reflect.Method.invoke(Method.java:507)
04-11 19:50:14.315: E/AndroidRuntime(338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-11 19:50:14.315: E/AndroidRuntime(338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-11 19:50:14.315: E/AndroidRuntime(338): at dalvik.system.NativeStart.main(Native Method)
04-11 19:50:14.315: E/AndroidRuntime(338): at com.sql.SQLiteExample.onCreate(SQLiteExample.java:28)
04-11 19:50:14.315: E/AndroidRuntime(338): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-11 19:50:14.315: E/AndroidRuntime(338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-11 19:50:14.315: E/AndroidRuntime(338): ... 11 more
04-11 19:55:14.435: I/Process(338): Sending signal. PID: 338 SIG: 9
04-11 20:02:38.005: W/dalvikvm(367): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-11 20:02:38.045: E/AndroidRuntime(367): FATAL EXCEPTION: main
04-11 20:02:38.045: E/AndroidRuntime(367): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sql/com.sql.SQLiteExample}: java.lang.ClassCastException: com.sql.SQLiteExample
04-11 20:02:38.045: E/AndroidRuntime(367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-11 20:02:38.045: E/AndroidRuntime(367): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-11 20:02:38.045: E/AndroidRuntime(367): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-11 20:02:38.045: E/AndroidRuntime(367): at android.os.Looper.loop(Looper.java:123)
04-11 20:02:38.045: E/AndroidRuntime(367): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-11 20:02:38.045: E/AndroidRuntime(367): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 20:02:38.045: E/AndroidRuntime(367): at java.lang.reflect.Method.invoke(Method.java:507)
04-11 20:02:38.045: E/AndroidRuntime(367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-11 20:02:38.045: E/AndroidRuntime(367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-11 20:02:38.045: E/AndroidRuntime(367): at dalvik.system.NativeStart.main(Native Method)
04-11 20:02:38.045: E/AndroidRuntime(367): Caused by: java.lang.ClassCastException: com.sql.SQLiteExample
04-11 20:02:38.045: E/AndroidRuntime(367): at com.sql.SQLiteExample.onCreate(SQLiteExample.java:28)
04-11 20:02:38.045: E/AndroidRuntime(367): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-11 20:02:38.045: E/AndroidRuntime(367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-11 20:02:38.045: E/AndroidRuntime(367): ... 11 more
04-11 20:07:38.165: I/Process(367): Sending signal. PID: 367 SIG: 9
SQLiteexample.java as
package com.sql;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SQLiteExample extends Activity implements OnClickListener{
/** Called when the activity is first created. */
Button sqlUpdate,sqlView;
EditText sqllocation,sqllongi,sqllati;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqliteexample);
sqlUpdate = (Button)findViewById(R.id.bsqlupdate);
sqlView = (Button)findViewById(R.id.bsqlopenview);
sqllongi = (EditText)findViewById(R.id.longi);
sqllati = (EditText)findViewById(R.id.lati);
sqllocation = (EditText)findViewById(R.id.location1);
sqlView.setOnClickListener((android.view.View.OnClickListener) this);
sqlUpdate.setOnClickListener((android.view.View.OnClickListener) this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId())
{
case R.id.bsqlupdate:
boolean wrk=true;
try{
String location = sqllocation.getText().toString();
String longi = sqllongi.getText().toString();
String lati = sqllongi.getText().toString();
Cornte entry = new Cornte(this);
entry.open();
entry.createEntry(location , longi, lati);
entry.close();
}catch (Exception e){
wrk= false;
String error=e.toString();
Dialog d=new Dialog(this);
d.setTitle("NO!!!");
TextView tv =new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}finally{
if(wrk)
{
Dialog d=new Dialog(this);
d.setTitle("YUP!!!");
TextView tv =new TextView(this);
tv.setText("SUCCESS");
d.setContentView(tv);
d.show();
}
}
break;
case R.id.bsqlopenview:
Intent i =new Intent("com.sql.SQLVIEW");
startActivity(i);
break;
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
错误在:
sqlView.setOnClickListener((android.view.View.OnClickListener) this);
您可以在logcat中看到它:
04-11 20:02:38.045: E/AndroidRuntime(367): at com.sql.SQLiteExample.onCreate(SQLiteExample.java:28)
我会解释一下,首先尝试学习使用logcat,大多数情况下你应该从错误的底部开始并搜索你的命名空间的行(在这种情况下是com.sql.SQLiteExample),现在你可以看到在这个例子中,你的应用程序崩溃了什么方法:'onCreate'但最重要的是发生错误的文件+行号!
ClassCastException意味着你想把一个Object放到一个对象中它不是...... 学校的例子你得到了BaseObjects'Fall',它有一些ChildObjects'Dogg'& '猫',但现在你正在努力做到以下几点:
Apple apple = giveMeThatApple();
Animal a = apple;
那不行,因为'苹果'与“动物”类没有关系......我希望你能理解......
答案 1 :(得分:0)
我得到了解决方案..错误是由于错误导入导入android.content.DialogInterface.OnClickListener;而不是导入View.OnClickListener setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (SequencerActivity) back.setOnClickListener(this);