Caused by: java.lang.ClassCastException: org.winplus.sufing.SecoandActivity
最好把这个activity贴出来!

解决方案 »

  1.   

    http://www.cnblogs.com/wangyuehome/archive/2013/04/25/3042656.html
      

  2.   

    01-14 16:59:15.229: E/AndroidRuntime(7931):  at org.winplus.sufing.SecoandActivity.onCreate(SecoandActivity.java:35)
    从代码层面来看是运行到了SecoandActivity.java第35行,有错,停止运行的。
      

  3.   

    这是activity的代码package org.winplus.sufing;import android.app.Activity;
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteException;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.Spinner;public class SecoandActivity extends Activity {
    public static final String DB_NAME="My.db";
    public static final int VERSION=1;
    public MyHelper helper;
    public SQLiteDatabase mydb;
    private Spinner mySpinner;
    private ArrayAdapter<String> adapter1;
    private Button button1,button2,button3,button4,button5,button6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_secoand);
    button1=(Button)this.findViewById(R.id.button1);
    button2=(Button)this.findViewById(R.id.button2);
    button3=(Button)this.findViewById(R.id.button3);
    button4=(Button)this.findViewById(R.id.button4);
    button5=(Button)this.findViewById(R.id.button5);
    button6=(Button)this.findViewById(R.id.button6);
    mySpinner=(Spinner)this.findViewById(R.id.spinner1);
    button1.setOnClickListener((OnClickListener) this);
    button2.setOnClickListener((OnClickListener) this);
    button3.setOnClickListener((OnClickListener) this);
    button4.setOnClickListener((OnClickListener) this);
    button5.setOnClickListener((OnClickListener) this);
    button6.setOnClickListener((OnClickListener) this);
    //初始化数据库辅助对象
    helper=new MyHelper(this, DB_NAME, null, VERSION);
    try {
    //获取可读写的SQLiteDatabase对象
    mydb=helper.getWritableDatabase();
    CreateTable();
    } catch (SQLiteException e) {
    // TODO: handle exception
    mydb=helper.getReadableDatabase();
    e.printStackTrace();
    }
    } @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.secoand, menu);
    return true;
    }

    public void onClick(View v)
    {
    if(v==button1)//插入两条记录
    {
    InsertData();
    }
    else if(v==button2)//删除一条记录
    {
    DeleteData();
    }
    else if(v==button3)//修改记录
    {
    ModifyData();
    }
    else if(v==button4)//删除数据表
    {
    DeleteTable();
    }
    else if(v==button5)//查询数据库
    {
    QueryData();
    }
    else if(v==button6)//重建数据库
    {
    CreateTable();
    }
    }

    void InsertData()
    {
    ContentValues values=new ContentValues();
    values.put(helper.ID, 1);
    values.put(helper.NAME, "孙刚");
    values.put(helper.HEIGHT, 1.75);
    mydb.insert(helper.TB_NAME, null, values);
    mydb.execSQL("insert into studenttable(id,name,height) values(2,'张超',1.80)");
    UpdateSpinner();
    }

    void UpdateSpinner()
    {
    QueryData();
    }

    void DeleteData()
    {
    mydb.delete(helper.TB_NAME, helper.ID, null);
    UpdateSpinner();
    }

    void ModifyData()
    {
    ContentValues values=new ContentValues();
    values.put(helper.NAME, "张华");
    values.put(helper.HEIGHT, 1.60);
    mydb.update(helper.TB_NAME, values, helper.ID, null);
    UpdateSpinner();
    }

    void DeleteTable()
    {
    mydb.execSQL("DROP TABLE IF EXISTS"+helper.TB_NAME);
    UpdateSpinner();
    }

    void CreateTable()
    {
    mydb.execSQL("CREATE TABLE IF NOT EXISTS"+helper.TB_NAME+"("+helper.ID+"integer primary key,"+helper.NAME+"text,"+helper.HEIGHT+"float)");
    UpdateSpinner();
    }

    void QueryData()
    {
    adapter1=null;
    mySpinner.setAdapter(adapter1);
    Cursor result=mydb.query(false,"studenttable", null, null, null, null, null, null, null, null);
    int count=result.getCount();
    if(count==0)return;
    String[] Str=new String[count];
    result.moveToFirst();int i=0;
    while(!result.isAfterLast())
    {
    int id=result.getInt(0);
    String name=result.getString(1);
    float height=result.getInt(2);
    Str[i]=String.valueOf(id)+""+name+""+String.valueOf(height);
    i++;
    result.moveToNext();
    }
    result.close();
    adapter1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,Str);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_item);
    //将adapter添加到spinner对象中
    mySpinner.setAdapter(adapter1);
    }

    @Override
    public void onDestroy()
    {
    if(mydb!=null)
    {
    mydb.close();
    mydb=null;
    }
    super.onDestroy();
    }
    }
      

  4.   

    问题是我忘了实现一个OnClickListener