2

我是安卓新手。我遵循了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 作为

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

    }
}
4

2 回答 2

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 意味着你想把一个对象放入一个不是它的对象中......学校示例你有 BaseObjects 'Animal' 它有一些 ChildObjects 'Dogg' & 'Cat' 但现在你正在尝试执行以下操作:

Apple apple = giveMeThatApple();
Animal a = apple;

好吧,那是行不通的,因为“苹果”与“动物”类无关……希望您理解……

于 2012-04-10T07:41:04.550 回答
0

我得到了解决方案..错误是由于 import android.content.DialogInterface.OnClickListener 的错误导入;而不是 import View.OnClickListener setOnClickListener(View.OnClickListener) 类型的 View 不适用于参数 (SequencerActivity) back.setOnClickListener(this);

于 2012-04-12T04:05:14.860 回答