Java 某些行在生成片段代码时未使用错误

Java 某些行在生成片段代码时未使用错误,java,android,arrays,android-fragments,fragment,Java,Android,Arrays,Android Fragments,Fragment,实际上,我在一个正常的活动中完成了部分代码,但当我想将其转换为一个片段类时,我遇到了一些问题,我尽力将代码转换为一个片段类 这是片段代码 /** * A simple {@link Fragment} subclass. */ public class ShoppingLstFragment extends Fragment { public ShoppingLstFragment() { // Required empty public constructor

实际上,我在一个正常的活动中完成了部分代码,但当我想将其转换为一个片段类时,我遇到了一些问题,我尽力将代码转换为一个片段类

这是片段代码

/**
 * A simple {@link Fragment} subclass.
 */
public class ShoppingLstFragment extends Fragment {


    public ShoppingLstFragment() {
        // Required empty public constructor
    }
    private ArrayList<String> arraylisttodo;
    private ArrayAdapter<String> arrayadaptertodo;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shopping_lst, container, false);
        arraylisttodo = new ArrayList<String>();
        arrayadaptertodo = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_activated_1 , arraylisttodo);
        ListView listViewtodo = (ListView) view.findViewById(R.id.listviewtodo);
        listViewtodo.setAdapter(arrayadaptertodo);

        registerForContextMenu(listViewtodo);

        try {
            Log.i("ON CREATE", "on create has occured");
            Scanner scanner = new Scanner(getContext().openFileInput("todo.txt"));

            while (scanner.hasNextLine()){
                String todo = scanner.nextLine();
                arrayadaptertodo.add(todo);
            }
            scanner.close();
        }catch (Exception e){
            Log.i("ON CREATE", e.getMessage());
        }

        return view;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        if(v.getId() != R.id.listviewtodo) {
            return;
        }
        menu.setHeaderTitle("What would you like to do?");
        String[] options = {"Delete Product", "Return"};

        for (String option : options) {
            menu.add(option);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        int selectedIndex = info.position;

        if (item.getTitle().equals("Delete Product")) {
            arraylisttodo.remove(selectedIndex);
            arrayadaptertodo.notifyDataSetChanged();
        }
        return true;
    }


    public void onBackPressed() {
        try {
            Log.i("ON BACK PRESSED", "On back is pressed");
            PrintWriter pw = new PrintWriter(getContext().openFileOutput("todo.txt", Context.MODE_PRIVATE));

            for(String todo : arraylisttodo){
                pw.println(todo);
            }
            pw.close();
        } catch(Exception e) {
            Log.i("ON BACK PRESSED", e.getMessage());
        } getActivity().onBackPressed();
    }


    public void buttonaddClick(View v){
            EditText todo = (EditText)getView().findViewById(R.id.todo);
        String toDo = todo.getText().toString().trim();


        if (toDo.isEmpty()) {

            arrayadaptertodo.add(toDo);
            todo.setText("");
        }


        }}
/**
*一个简单的{@link Fragment}子类。
*/
公共类shoppinglstragment扩展了片段{
公众购物者(){
//必需的空公共构造函数
}
私有ArrayList arraylisttodo;
私人ArrayAdapter arrayadaptertodo;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(R.layout.fragment\u shopping\u lst,container,false);
arraylisttodo=新的ArrayList();
arrayadaptertodo=新的ArrayAdapter(getActivity(),android.R.layout.simple\u list\u item\u activated\u 1,arraylisttodo);
ListView listViewtodo=(ListView)view.findViewById(R.id.listViewtodo);
setAdapter(arrayadaptertodo);
registerForContextMenu(listViewtodo);
试一试{
Log.i(“创建时”,“创建时已发生”);
Scanner Scanner=new Scanner(getContext().openFileInput(“todo.txt”);
while(scanner.hasNextLine()){
字符串todo=scanner.nextLine();
arrayadaptertodo.add(todo);
}
scanner.close();
}捕获(例外e){
Log.i(“在创建时”,例如getMessage());
}
返回视图;
}
@凌驾
public void onCreateContextMenu(ContextMenu菜单,视图v,ContextMenu.ContextMenuInfo菜单信息){
if(v.getId()!=R.id.listviewtodo){
返回;
}
菜单。设置标题(“您想做什么?”);
字符串[]选项={“删除产品”,“返回”};
用于(字符串选项:选项){
菜单。添加(选项);
}
}
@凌驾
公共布尔值onContextItemSelected(MenuItem项){
AdapterView.AdapterContextMenuInfo信息=(AdapterView.AdapterContextMenuInfo)项。getMenuInfo();
int selectedIndex=info.position;
if(item.getTitle().equals(“删除产品”)){
arraylisttodo.remove(selectedIndex);
arrayadaptertodo.notifyDataSetChanged();
}
返回true;
}
public void onBackPressed(){
试一试{
Log.i(“背面按下”、“背面按下”);
PrintWriter pw=新的PrintWriter(getContext().openFileOutput(“todo.txt”,Context.MODE_PRIVATE));
for(字符串todo:arraylisttodo){
pw.println(todo);
}
关闭();
}捕获(例外e){
Log.i(“背压”,例如getMessage());
}getActivity().onBackPressed();
}
公共作废按钮点击(视图五){
EditText todo=(EditText)getView().findViewById(R.id.todo);
字符串toDo=toDo.getText().toString().trim();
if(toDo.isEmpty()){
arrayadaptertodo.add(toDo);
todo.setText(“”);
}
}}
这些方法被称为未使用

我不熟悉android和java。如何解决此错误?

对于
onBackPressed()
方法,您需要查看

对于
buttonadclick()
方法,我认为您需要在
fragment\u shopping\u lst
布局中编写
android:onClick“buttonadclick”

如果没有,请向我展示您的XML代码,如果您需要任何其他帮助,请向我发表评论

@Jason欢迎使用Stack Overflow。请注意,在这里说“谢谢”的首选方式是投票选出好的问题和有用的答案(一旦你有足够的声誉这么做),并接受对你提出的任何问题最有用的答案(这也会给你的声誉带来一点提升)。请参阅本页,并