无法获得片段上下文?显示无法访问的代码

时间:2015-06-19 08:36:03

标签: android database sqlite fragment android-context

运行应用程序时无法获取片段上下文,显示无法访问的代码。我用上下文创建了DatabaseHelper参数化构造函数。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_direct_search,container,false);

    try {
        myDbHelper.createDataBase(getActivity());
    } 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)view.findViewById(R.id.listView);
    populateListView();
                 return view;
}

这是DatabaseHelper() Class:

中的DatabaseHelper构造函数
     public DatabaseHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
    DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}

在运行无法访问的代码的应用程序时出现以下错误。错误发生在以下行:

 myDbHelper.createDataBase(getActivity());

 error : unreachable code

4 个答案:

答案 0 :(得分:1)

return inflater.inflate(R.layout.fragment_direct_search,container,false);

在您的方法中很早就完成了,因此之后不会执行任何操作。要解决此问题,请将其更改为:

View view = inflater.inflate(R.layout.fragment_direct_search,container,false);

并将其放在方法的最后;

return view;

编辑:

要正确获取列表视图,您可以更改

listview=(ListView)listview.findViewById(R.id.listView);

listview=(ListView)view.findViewById(R.id.listView);

并获取上下文传递getActivity()而不是this

答案 1 :(得分:0)

onCreateView方法中使用return语句两次,首先是开始,第二次是结束,第一次返回后的代码无法访问。

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);// this line should be change
   try {
    myDbHelper.createDataBase();
   } catch (IOException e) {
    e.printStackTrace();
   }catch(SQLException sqle){
    throw sqle;
   }
 .......
}  

改为:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_direct_search,container,false);
try {
    myDbHelper.createDataBase();
} 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) view.findViewById(R.id.listView);//change listview.find... to view.find...
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 view;//and finally return view instead of inflate layout again
}
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);
}
}

答案 2 :(得分:0)

Remove this code from first declaration

return inflater.inflate(R.layout.fragment_direct_search,container,false);
and use 
View view=inflater.inflate(R.layout.fragment_direct_search,container,false);
and exit return the view at last.because this function will return the view without process the next line.

答案 3 :(得分:0)

请使用isAdded()检查Fragment是否已添加到Activity中,然后使用Handler检查它,在您的情况下:

new Handler().post(new Runnable() {
    @Override
    public void run() {
        if (isAdded()) {
            try {
                myDbHelper.createDataBase();
            } catch (IOException e) {
                e.printStackTrace();
            }catch(SQLException sqle){
            throw sqle;
            }
        }
    }
});