标签:
package cn.deu.bztc.happyidiom.db; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import cn.deu.bztc.happyidiom.activity.R; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.os.Environment; public class DBOpenHelper { private final int BuFFWER_SIZE=400000;//缓冲区大小 public static final String DB_NAME="idioms.db";//保存的数据库文件名 public static final String PACKAGE_Name="cn.deu.bztc.happyidiom.activity"; //应用的包名 public static final String DB_PATH="/data" +Environment.getDataDirectory().getAbsolutePath()+"/" +PACKAGE_Name+"/databases";//在手机里存放数据库的位置 private Context context; public DBOpenHelper(Context context) { super(); this.context = context; } public SQLiteDatabase openDatabase(){ try{ File myDataPath=new File(DB_PATH); if(!myDataPath.exists()){ myDataPath.mkdirs(); //如果没有这个目录则创建 } String dbfile=myDataPath+"/"+DB_NAME; if(!(new File(dbfile).exists())){ //判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库 InputStream is=context.getResources().openRawResource(R.raw.idioms); FileOutputStream fos=new FileOutputStream(dbfile); byte[] buffer=new byte[BuFFWER_SIZE]; int count=0; while((count=is.read(buffer))>0){ fos.write(buffer, 0, count); } fos.close(); is.close(); } SQLiteDatabase db=SQLiteDatabase.openOrCreateDatabase(dbfile, null); return db; }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return null; } }上面的代码实现功能主要是使用输入输出流将idioms.db复制到手机中默认存放 数据库的位置
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.deu.bztc.happyidiom.activity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/logo" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" > <span style="color:#ff0000;"> <uses-library android:name="android.test.runner" /></span> <activity android:name="cn.deu.bztc.happyidiom.activity.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="cn.deu.bztc.happyidiom.activity.StudyAnimalActivity" android:label="@string/app_name" > </activity> <activity android:name="cn.deu.bztc.happyidiom.util.DialogUtil" android:label="@string/app_name" > </activity> <activity android:name="cn.deu.bztc.happyidiom.activity.StudyActivity" android:label="@string/app_name" > </activity> <activity android:name="com.tangsci.android.example.ttsdemo.TtsDemoActivity" android:label="@string/app_name" > </activity> </application> <span style="color:#ff0000;"><instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.deu.bztc.happyidiom.activity" > </instrumentation></span> </manifest>
package cn.deu.bztc.happyidiom.test; import java.util.List; import cn.deu.bztc.happyidiom.dao.AnimalDao; import cn.deu.bztc.happyidiom.db.DBOpenHelper; import cn.deu.bztc.happyidiom.entity.Animal; import android.test.AndroidTestCase; public class DBOpenHelpTest extends AndroidTestCase { public void testDBCOpy(){ DBOpenHelper dbopenHelper=new DBOpenHelper(getContext()); dbopenHelper.openDatabase(); } public void testGetAllAnimals(){ AnimalDao animalDao=AnimalDao.getInstance(getContext()); List<Animal> animals=animalDao.getAllAnimals(); System.out.println(animals.size()); for(Animal animal:animals){ System.out.println(animal.getName()); } } }
package cn.deu.bztc.happyidiom.entity; public class Animal { private int id; private String name;//成语名称 private String pronounce;//成语发音 private String explain;//成语解释 private String antonym;//反义词 private String homoionym;//同义词 private String derivation;//源自 private String examples;//例子 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPronounce() { return pronounce; } public void setPronounce(String pronounce) { this.pronounce = pronounce; } public String getExplain() { return explain; } public void setExplain(String explain) { this.explain = explain; } public String getAntonym() { return antonym; } public void setAntonym(String antonym) { this.antonym = antonym; } public String getHomoionym() { return homoionym; } public void setHomoionym(String homoionym) { this.homoionym = homoionym; } public String getDerivation() { return derivation; } public void setDerivation(String derivation) { this.derivation = derivation; } public String getExamples() { return examples; } public void setExamples(String examples) { this.examples = examples; } }然后在数据库管理层 (Dao 层)建立操作类AnimalDao.java
package cn.deu.bztc.happyidiom.dao; import java.util.ArrayList; import java.util.List; import cn.deu.bztc.happyidiom.db.DBOpenHelper; import cn.deu.bztc.happyidiom.entity.Animal; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class AnimalDao { private static AnimalDao animalDao; private static SQLiteDatabase db; /** * 将构造方法私有化 */ public AnimalDao(Context context) { DBOpenHelper dbHelper=new DBOpenHelper(context); db=dbHelper.openDatabase(); } /** * 获取AnimalDao实例 */ public synchronized static AnimalDao getInstance(Context context){ if(animalDao==null){ animalDao=new AnimalDao(context); } return animalDao; } /** * 从数据库读取所有的动物类成语 */ public static List<Animal> getAllAnimals(){ List<Animal> list=new ArrayList<Animal>(); Cursor cursor=db.query("animal", null, null, null, null,null,null); if(cursor.moveToFirst()){ do{ Animal animal=new Animal(); animal.setId(cursor.getInt(cursor.getColumnIndex("_id"))); animal.setName(cursor.getString(cursor.getColumnIndex("name"))); animal.setPronounce(cursor.getString(cursor.getColumnIndex("pronounce"))); animal.setAntonym(cursor.getString(cursor.getColumnIndex("antonym"))); animal.setHomoionym(cursor.getString(cursor.getColumnIndex("homoionym"))); animal.setDerivation(cursor.getString(cursor.getColumnIndex("derivation"))); animal.setExamples(cursor.getString(cursor.getColumnIndex("examples"))); list.add(animal); }while(cursor.moveToNext()); } return list; } }下面进行单元测试
public void testGetAllAnimals(){ AnimalDao animalDao=AnimalDao.getInstance(getContext()); List<Animal> animals=animalDao.getAllAnimals(); System.out.println(animals.size()); for(Animal animal:animals){ System.out.println(animal.getName()); }测试如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="@drawable/bg_animal" > <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> </LinearLayout> </TabHost> </RelativeLayout>
<string-array name="category"> <item>动物类</item> <item>自然类</item> <item>人物类</item> <item>季节类</item> <item>数字类</item> <item>寓言类</item> <item>其他类</item> </string-array>接下来cn.deu.bztc.happyidiom.activity中建立MainActivity.java
package cn.deu.bztc.happyidiom.activity; import android.os.Bundle; import android.app.Activity; import android.app.TabActivity; import android.content.Intent; import android.view.Menu; import android.view.Window; import android.widget.TabHost; public class MainActivity extends TabActivity { private TabHost tabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题栏 setContentView(R.layout.activity_main); tabHost=getTabHost(); addTab("study", R.string.title_study, R.drawable.study,StudyActivity.class); addTab("search", R.string.title_search, R.drawable.search, StudyActivity.class); addTab("study", R.string.title_game, R.drawable.game, StudyActivity.class); addTab("save", R.string.title_save, R.drawable.save, StudyActivity.class); addTab("help", R.string.title_help, R.drawable.search, StudyActivity.class); } private void addTab(String tag,int title_introduction,int title_icon,Class ActivityClass){ tabHost.addTab(tabHost.newTabSpec(tag) .setIndicator(getString(title_introduction), getResources().getDrawable(title_icon)).setContent(new Intent(this,ActivityClass))); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }在这个类里调用TabHost组件, 然后调用了抽取出来的自定义的方法addTob()添加了五个选项卡,方法的四个参数分别为每个选项卡的tag,指示器上显示的标题。,指示器上显示的图片,选项卡对应的内容。
出现以下错误: 这是不能实例类
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
不能实例化activity有如下二种情况:
1.没有在Manifest.xml 清单中注册该activity,或者在创建完activity后,修改了包名或者activity的类名,而配置清单中没有修改,造成不能实例化
2.自己新建一个包,而配置的时候,使用默认包
详细介绍请看:http://blog.csdn.net/zhupengqq/article/details/51591964
下面是项目源码:http://pan.baidu.com/s/1skK7cVN 密码 q6ow
标签:
原文地址:http://blog.csdn.net/zhupengqq/article/details/51593339