Monday, April 13, 2015

Android : Error while accessing external sqlite database from the assets folder



am creating navigation drawer list view that make changes in a specific text view according to the position of the list view , but always had that exception many times and i don't know how to solve it


MainActivity Class



public class MainActivity extends ActionBarActivity {

ListView lv;
DrawerList adapter;
DrawerLayout drawer;
String[] duaaList;
ActionBarDrawerToggle drawerToggle;
TextView tv;
ArrayList<String>data;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

duaaList = getResources().getStringArray(R.array.duaa);

drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

tv = (TextView) findViewById(R.id.tvDesc);

lv = (ListView) findViewById(R.id.left_drawer);

adapter = new DrawerList();

lv.setAdapter(adapter);

try {
readOrCopyFile();
} catch (IOException e1) {
e1.printStackTrace();
}


drawerToggle = new ActionBarDrawerToggle(this,drawer,0,0);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

selectItem(position);

changeText(position);


}
public void selectItem(int position){
lv.setItemChecked(position, true);
setTitle(duaaList[position]);
}
public void setTitle(String title){
getSupportActionBar().setTitle(title);
}

//set data into a textview according to the position of the list
public void changeText(int position){
try{

lv.setItemChecked(position, true);
lv.setSelection(position);
drawer.closeDrawer(lv);

tv.setText(data.get(position));
}catch(Exception e){
e.printStackTrace();
}
}

});

}



//reading file from the assets folder


public void readOrCopyFile() throws IOException{

String destPath = "/data/data/" + getPackageName() + "/databases/";

File f = new File(destPath);

if (!f.exists()) {

f.mkdirs();

Log.d("TAG", "directory " + destPath + " has been created");

f.createNewFile();

Log.d("TAG", "createNewFile Return " + f.createNewFile() );

importDb(getBaseContext().getAssets().open("duaa.sqlite"),new FileOutputStream(destPath + "duaa.sqlite"));
Log.d("TAG", "Database has been imported successfully");

}
}

// read database file from databases folder

public void importDb(InputStream inputStream,
OutputStream outputStream) throws IOException {

byte[] buffer = new byte[1024];
int length;

while ((length = inputStream.read(buffer)) > 0) {

outputStream.write(buffer, 0 , length);
}

DbHelper helper = new DbHelper(getBaseContext());

SQLiteDatabase db = helper.getReadableDatabase();

SQLiteDatabase.openDatabase("/data/data/" + getPackageName() + "/databases/", null,
SQLiteDatabase.OPEN_READONLY);

Cursor c = db.query("main", new String[]{"desc"} ,null,null, null, null, null);

if(c.moveToFirst() && c != null)
{
do {
data.add(c.getString(c.getColumnIndex("id")));
} while (c.moveToNext());
}

Log.d("TAG" , "the database has been successfully readed :)" );

//inputStream.close();
//outputStream.close();
//outputStream.flush();

}

//reading the file from the databases folder which has been copied before in the readOrCopyFile() method




@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.action_settings) {
finish();
}

return super.onOptionsItemSelected(item);
}

class DrawerList extends BaseAdapter{


@Override
public int getCount() {
return duaaList.length;
}

@Override
public Object getItem(int position) {
return duaaList[position];
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null){
v = getLayoutInflater().inflate(R.layout.duaa_list_item , parent , false);
}
TextView tv = (TextView) v.findViewById(R.id.tv2);
tv.setText(duaaList[position]);

return v;
}
}
}


DbHelper Class that extends SQLiteOpenHelper



public class DbHelper extends SQLiteOpenHelper {


public DbHelper(Context context) {
super(context, null , null, 2);
}



@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

public SQLiteDatabase getDatabase(String path){
return SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);

}

}


and here the xml file



<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ScrollView
android:layout_width="match_parent"
android:layout_height="480dp" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/tvDesc"
android:layout_gravity="center"
android:paddingTop="10dp"
/>
</LinearLayout>

</ScrollView>
</FrameLayout>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:splitMotionEvents="true"
android:soundEffectsEnabled="true"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp"
android:background="#ffbababa"/>

</android.support.v4.widget.DrawerLayout>


here is the exception



04-13 14:26:00.573: W/System.err(18010): java.lang.NullPointerException 04-13 14:26:00.573: W/System.err(18010): at com.anabil.duaaapp.MainActivity$1.changeText(MainActivity.java:98) 04-13 14:26:00.573: W/System.err(18010): at com.anabil.duaaapp.MainActivity$1.onItemClick(MainActivity.java:78) 04-13 14:26:00.573: W/System.err(18010): at android.widget.AdapterView.performItemClick(AdapterView.java:299) 04-13 14:26:00.573: W/System.err(18010): at android.widget.AbsListView.performItemClick(AbsListView.java:1113) 04-13 14:26:00.573: W/System.err(18010): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911) 04-13 14:26:00.573: W/System.err(18010): at android.widget.AbsListView$3.run(AbsListView.java:3645) 04-13 14:26:00.573: W/System.err(18010): at android.os.Handler.handleCallback(Handler.java:733) 04-13 14:26:00.573: W/System.err(18010): at android.os.Handler.dispatchMessage(Handler.java:95) 04-13 14:26:00.573: W/System.err(18010): at android.os.Looper.loop(Looper.java:136) 04-13 14:26:00.573: W/System.err(18010): at android.app.ActivityThread.main(ActivityThread.java:5001) 04-13 14:26:00.573: W/System.err(18010): at java.lang.reflect.Method.invokeNative(Native Method) 04-13 14:26:00.573: W/System.err(18010): at java.lang.reflect.Method.invoke(Method.java:515) 04-13 14:26:00.573: W/System.err(18010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 04-13 14:26:00.573: W/System.err(18010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 04-13 14:26:00.573: W/System.err(18010): at dalvik.system.NativeStart.main(Native Method)



No comments:

Post a Comment