无法将To Do Activity用于片段中

时间:2015-04-26 11:35:25

标签: java android android-fragments

抱歉,我对Android很新,所以我似乎总是陷入最简单的事情。

我有两个项目,我已经完成了一个课程。一个是简单的待办事项列表,另一个允许使用不同的片段,具体取决于您是处于纵向还是横向模式。两者都有效,但是当我试图将两者结合起来时,如果我扩展Fragment(findByViews和setContentView不起作用),我在片段中放入To Do活动会出错。

我可以用FragmentActivity替换Fragment来解决这个问题,然后修复我的MainActivity.java的fragmentTransaction.replace(android.R.id.content,pm_fragment);得到一个错误说: " FragmentTransaction中的replace(int,android.app.Fragment)无法应用于(int,com.android.MyFragmentsTodo.PM_Fragment)"

有谁能告诉我我能做些什么来使这项工作?我明天有一个测试,我担心讲师可能会要求我们将活动与碎片混合在一起。 任何帮助将不胜感激。

MainActivity.java

    package com.android.myfragmentstodo;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Configuration config = getResources().getConfiguration();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        //check device orientation and act upon it
        if(config.orientation == Configuration.ORIENTATION_LANDSCAPE){
            // If orientation is landscape then
            LM_Fragment ls_fragment = new LM_Fragment();
            fragmentTransaction.replace(android.R.id.content, ls_fragment);

        }else{
            // If orientation is portrait then
            PM_Fragment pm_fragment = new PM_Fragment();
            fragmentTransaction.replace(android.R.id.content, pm_fragment);
        }
        //apply (commit) the changes
        fragmentTransaction.commit();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

PM_Fragment.java

    package com.android.myfragmentstodo;

        import android.app.Activity;

        import android.app.Fragment;
        import android.os.Bundle;


        import android.support.v4.app.FragmentActivity;
        import android.support.v7.app.ActionBarActivity;
        import android.view.LayoutInflater;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.AdapterView;
        import android.widget.ArrayAdapter;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.ListView;
        import android.widget.Toast;

        import java.util.ArrayList;

/**
 * Created by Malan on 4/20/2015.
 */
public class PM_Fragment extends android.support.v4.app.Fragment {

    private ArrayList<String> items;
    private ArrayAdapter<String> adapter;
    private ListView listView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        listView = (ListView) findViewById(R.id.todoItems);
        items = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

        listView.setAdapter(adapter);


        Button button = (Button) findViewById(R.id.btnAddItem);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText = (EditText) findViewById(R.id.todoEdit);
                String itemText = editText.getText().toString();
                adapter.add(itemText);
                editText.setText("");
                Toast toast = Toast.makeText(getApplicationContext(), "Item Added", Toast.LENGTH_SHORT);
                toast.show();
            }
        });

        setupListViewListener();

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

    private void setupListViewListener() {
        listView.setOnItemLongClickListener(
                new AdapterView.OnItemLongClickListener(){
                    @Override
                    public boolean onItemLongClick(AdapterView<?> av,
                                                   View item, int pos, long id){
                        items.remove(pos);
                        adapter.notifyDataSetChanged();
                        return true;
                    }
                });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void AddItem(View view) {
        EditText editText = (EditText) findViewById(R.id.todoEdit);
        String itemText = editText.getText().toString();
        adapter.add(itemText);
        editText.setText("");
    }
}

1 个答案:

答案 0 :(得分:1)

好的,我已经找到了解决问题的方法。必须继续扩展Fragment但是在使用findViewById时必须使用:

rootView = inflater.inflate(R.layout.pm_fragment, container, false);

然后在所有findViewById之前使用rootView,如下所示:

listView = (ListView) rootView.findViewById(R.id.todoItems);

每当使用“this”时,请使用“getActivity()”,例如:

adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);

使用getApplicationContext时,必须使用getActivity()。getApplicationContext(),例如:

Toast toast = Toast.makeText(getActivity().getApplicationContext(), "Item Added", Toast.LENGTH_SHORT);

最后我的PM_Fragment被改为看起来像这样: PM_Fragment.java

    package com.android.myfragmentstodo;




        import android.app.Fragment;
        import android.os.Bundle;


        import android.view.LayoutInflater;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.AdapterView;
        import android.widget.ArrayAdapter;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.ListView;
        import android.widget.Toast;

        import java.util.ArrayList;

/**
 * Created by Malan on 4/20/2015.
 */
public class PM_Fragment extends Fragment {

    private ArrayList<String> items;
    private ArrayAdapter<String> adapter;
    private ListView listView;
    View rootView;


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState){

        rootView = inflater.inflate(R.layout.pm_fragment, container, false);


        listView = (ListView) rootView.findViewById(R.id.todoItems);
        items = new ArrayList<String>();
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);

        listView.setAdapter(adapter);


        Button button = (Button) rootView.findViewById(R.id.btnAddItem);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                EditText editText = (EditText) rootView.findViewById(R.id.todoEdit);
                String itemText = editText.getText().toString();
                adapter.add(itemText);
                editText.setText("");
                Toast toast = Toast.makeText(getActivity().getApplicationContext(), "Item Added", Toast.LENGTH_SHORT);
                toast.show();
            }
        });

        setupListViewListener();

        return rootView;
    }

    private void setupListViewListener() {
        listView.setOnItemLongClickListener(
                new AdapterView.OnItemLongClickListener(){
                    @Override
                    public boolean onItemLongClick(AdapterView<?> av,
                                                   View item, int pos, long id){
                        items.remove(pos);
                        adapter.notifyDataSetChanged();
                        return true;
                    }
                });
    }




    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void AddItem(View view) {
        EditText editText = (EditText) rootView.findViewById(R.id.todoEdit);
        String itemText = editText.getText().toString();
        adapter.add(itemText);
        editText.setText("");
    }
}