Android - 如何将代码 Activity 转换为代码片段?

Android - How to convert code Activity a code Fragment?

我遇到了问题 运行 我的活动变成了碎片。我搜索了几种方法来做到这一点,但即便如此我还是很困惑。

我知道,这是很多代码。我希望你能帮助我。

显示了我需要的东西,但是当执行按钮的动作时,出现以下错误。

E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at ..TaskFragment.onActivityCreated(TaskFragment.java:29)
        at ..TaskFragment.onCreateView(TaskFragment.java:37)

Activity class..有效

 public class TaskActivity extends Activity {


TareaAdapter adapter;
ListView lvTask;
ArrayList<Task> listTask;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_tareas);
    lvTask =(ListView)findViewById(R.id.lvTareas);
    lvTask.setDividerHeight(1);
    listTask =new ArrayList<Task>();
}

@Override
protected void onResume()
{
    if(listTask.size()==0){
        populateList();
    }
    super.onResume();
}

public Task getTask(int level, int haschild){
    Task task =new Task();
    task.nombre ="Nombre Tarea";
    task.isAbierto =false;
    task.nivel =level;
    task.tieneHijo = haschild;
    return task;
}
public void populateList()
{
    try
    {
        for(int i=0;i<2;i++){
            listTask.add(getTask(0, 1));
        }
        adapter = new TareaAdapter(TaskActivity.this, listTask);
        lvTask.setAdapter(adapter);
    }
    catch (Exception e)
    {

    }
}
public void CellButtonClick(View v){
    try{
        Button b=(Button)v;
        int index;
        index=(Integer) b.getTag();

        if(b.getText().toString().equals("+")){
            b.setText("-");
            Task temp[]=new Task[2];
            int PLevel= listTask.get(index).nivel +1;
            for(int i=0;i<2;i++){
                temp[i]= getTask(PLevel, 1);
            }
            listTask.get(index).isAbierto =true;
            if(temp!=null){
                int addindex=index+1;
                for(int i=0;i<temp.length;i++){
                    listTask.add(addindex, temp[i]);
                    addindex++;
                }
            }
            temp=null;
        }
        else{
            b.setText("+");
            listTask.get(index).isAbierto =false;
            int removeindex=index+1;
            for(int i=0;i<2;i++){
                if(listTask.get(removeindex).isAbierto){
                    removeChilds(removeindex);
                }
                listTask.remove(removeindex);
            }
        }
        adapter.notifyDataSetChanged();
    }
    catch(Exception e){
        adapter.notifyDataSetChanged();

    }
}
public void removeChilds(int index){
    try {
        int removeindex=index+1;
        for(int i=0;i<2;i++){
            if(listTask.get(removeindex).isAbierto){
                removeChilds(removeindex);
            }
            listTask.remove(removeindex);
        }
    } catch (Exception e) {
        // TODO: handle exception

    }
}

片段class..应用程序崩溃

public class 任务片段扩展片段 {

TareaAdapter adapter;
ListView lvTask;
ArrayList<Task> listTask;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    lvTask =(ListView) getView().findViewById(R.id.lvTareas);
    lvTask.setDividerHeight(1);
    listTask =new ArrayList<Task>();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View viewRoot =  inflater.inflate(R.layout.fragment_tareas, container, false);
    Button btnExpandir = (Button) viewRoot.findViewById(R.id.btnExpandirTarea);
    btnExpandir.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CellButtonClick(v);
        }
    });
    return viewRoot;
}

@Override
public void onResume()
{
    if(listTask.size()==0){
        populateList();
    }
    super.onResume();
}

public Task getTask(int level, int haschild){
    Task task =new Task();
    task.nombre ="Nombre Tarea";
    task.isAbierto =false;
    task.nivel =level;
    task.tieneHijo = haschild;
    return task;
}
public void populateList()
{
    try
    {
        for(int i=0;i<2;i++){
            listTask.add(getTask(0, 1));
        }
        adapter = new TareaAdapter(getActivity(), listTask);
        lvTask.setAdapter(adapter);
    }
    catch (Exception e)
    {

    }
}
public void CellButtonClick(View v){
    try{
        Button b=(Button)v;
        int index;
        index=(Integer) b.getTag();

        if(b.getText().toString().equals("+")){
            b.setText("-");
            Task temp[]=new Task[2];
            int PLevel= listTask.get(index).nivel +1;
            for(int i=0;i<2;i++){
                temp[i]= getTask(PLevel, 1);
            }
            listTask.get(index).isAbierto =true;
            if(temp!=null){
                int addindex=index+1;
                for(int i=0;i<temp.length;i++){
                    listTask.add(addindex, temp[i]);
                    addindex++;
                }
            }
            temp=null;
        }
        else{
            b.setText("+");
            listTask.get(index).isAbierto =false;
            int removeindex=index+1;
            for(int i=0;i<2;i++){
                if(listTask.get(removeindex).isAbierto){
                    removeChilds(removeindex);
                }
                listTask.remove(removeindex);
            }
        }
        adapter.notifyDataSetChanged();
    }
    catch(Exception e){
        adapter.notifyDataSetChanged();

    }
}
public void removeChilds(int index){
    try {
        int removeindex=index+1;
        for(int i=0;i<2;i++){
            if(listTask.get(removeindex).isAbierto){
                removeChilds(removeindex);
            }
            listTask.remove(removeindex);
        }
    } catch (Exception e) {
        // TODO: handle exception

    }
}

将这三行放在onCreateView中(并删除onActivityCreated中的相应行)

lvTask =(ListView) viewRoot.findViewById(R.id.lvTareas);
lvTask.setDividerHeight(1);
listTask =new ArrayList<Task>();