Android 碎片额外代码

Android Fragments Extra Code

我的 Android 应用程序遇到了问题。我是初学者,我倾向于自己学习。所以我正在尝试制作一个 MediaPlayer,为了做到这一点,我需要使用 Fragments。问题不在于片段本身,而是我需要在 MediaPlayer 旁边添加一些带有 onClickListeners 的额外按钮,我的应用程序崩溃了,因为 fragment_main.xml 文件具有布局参数,但我无法在片段(我在 findViewById 处出错(无法对非静态方法进行静态引用)。请帮助我。

 public class MainActivity extends Activity {

 Button blade;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();


        /* blade = (Button)findViewById(R.id.fantab);
            blade.setOnClickListener(new OnClickListener(){
                @Override
                //On click function
                public void onClick(View view) {
                    //Create the intent to start another activity
                    Intent vent = new Intent(MainActivity.this,     Ventscreen.class);
                    startActivity(vent);
                    finish();
                }
            });

    */      
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends ListFragment {

     private static final String[] FROM = { MediaStore.Audio.Media.TITLE };
        private static final int[] TO = { android.R.id.text1 };
        private CursorAdapter mAdapter;
        Button blade;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_main, container, false);
            return view;
            blade = (Button)findViewById(R.id.fantab);
            blade.setOnClickListener(new OnClickListener(){
                @Override
                //On click function
                public void onClick(View view) {
                    //Create the intent to start another activity
                    Intent vent = new Intent(MainActivity.this, Ventscreen.class);
                    startActivity(vent);
                    finish();
                }
            });
        }

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            Context context = getActivity();
            int layout = android.R.layout.simple_list_item_1;
            Cursor cursor = context.getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    null,
                    null,
                    null,
                    MediaStore.Audio.Media.TITLE + " ASC");
            int flags = 0; 
            mAdapter = new SimpleCursorAdapter(context, layout, cursor, FROM, TO, flags);

        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState)
        {
            super.onActivityCreated(savedInstanceState); 

            setListAdapter(mAdapter); 
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            mAdapter.notifyDataSetChanged();
        }
    }
    }

不是只有一个按钮,上面的代码只是举例,总共应该有五个按钮。 谢谢。

blade = (Button)findViewById(R.id.fantab);

您正在尝试调用外部 ActivityfindViewById(int) 方法。这是不可能的,因为你内心的 Fragment class 是静态的。您要做的是在 Fragment View:

上调用 findViewById(int)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_main, container, false);
    blade = (Button) view.findViewById(R.id.fantab);
    blade.setOnClickListener(new OnClickListener(){
        @Override
        //On click function
        public void onClick(View view) {
            //Create the intent to start another activity
            Intent vent = new Intent(getActivity(), Ventscreen.class);
            getActivity().startActivity(vent);
            getActivity().finish();
        }
    });
    return view;
}

另外,请注意 return view; 语句的位置。