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

时间:2015-01-15 16:28:08

标签: android android-activity android-fragments

我在将活动分成碎片时遇到了问题。我搜索了几种如何做到这一点的方法,但即便如此,我感到很困惑。

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

显示我需要的内容,但在执行按钮操作时,会出现以下错误。

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

活动类..它有效

 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

    }
}

Fragment class ..应用程序崩溃

public class TaskFragment扩展了Fragment {

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

    }
}

1 个答案:

答案 0 :(得分:0)

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

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