vendredi 19 juin 2015

unable to get fragment context? showing unreachable code

Unable to get fragment context while running application,showing unreachable code. i have created the DatabaseHelper parameterized constructor with context.

package example.com.navigationdrawer;


import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;


public class DirectSearchFragment extends Fragment {

Cursor c=null;
ListView listview;
Context context = getActivity();
DatabaseHelper myDbHelper=myDbHelper = new DatabaseHelper(context);

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_direct_search,container,false);
    try {
        myDbHelper.createDataBase(this);
    } catch (IOException e) {
        e.printStackTrace();
    }catch(SQLException sqle){
        throw sqle;
    }


    try {

        myDbHelper.openDataBase();

    }catch(SQLException sqle){

        throw sqle;

    }
    Toast.makeText(getActivity(), "Successfully copied...", Toast.LENGTH_SHORT).show();

    listview=(ListView)listview.findViewById(R.id.listView);
    populateListView();
            /*
            c=myDbHelper.query("EMP_TABLE", null, null, null, null,null, null);
            if(c.moveToFirst())
            {
                do {

                    Toast.makeText(CopyDbActivity.this,
                            "_id: " + c.getString(0) + "\n" +
                                    "E_NAME: " + c.getString(1) + "\n" +
                                    "E_AGE: " + c.getString(2) + "\n" +
                                    "E_DEPT:  " + c.getString(3),
                            Toast.LENGTH_LONG).show();

                } while (c.moveToNext());
            }

            */
    return inflater.inflate(R.layout.fragment_direct_search,container,false);
}
private void populateListView()
{
    Cursor c=myDbHelper.getAlldetails();
    String[] fromfieldname=new String[]{"NAME","CATEGORY"};
    int[] tofield=new int[]{R.id.nameText,R.id.categoryText};

    SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter(getActivity(),R.layout.custom_list_view,c,fromfieldname,tofield);
    listview.setAdapter(simpleCursorAdapter);
}
}

getting following error while running the application that is unreachable code. error is occuring at the following line :

 myDbHelper.createDataBase(this);

 error : unreachable code

Aucun commentaire:

Enregistrer un commentaire