0

我无法从片段中获取按钮,怎么了?收到未捕获的异常 - nullpointerexception;找不到按钮。

早期都会找到,但是在更新android sdk之后,一切都改变了((

package com.example.test;

import java.io.IOException;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener {


    Button play, pause;
    MediaPlayer mplayer;
    RadioGroup rgroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("MyTAG", "1");
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        mplayer = new MediaPlayer();
        rgroup = (RadioGroup) findViewById(R.id.radioGroup1);

        play = (Button) findViewById(R.id.play);
        pause = (Button) findViewById(R.id.pause);



            play.setOnClickListener(this);
            pause.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {


    }

    @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 Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

ps对不起我的英语

4

1 回答 1

3

您正在尝试在Activity's 的布局内进行搜索,并且您的小部件在 aFragment的布局内。

把你的逻辑放在你的PlaceHolderFragment班级里:

public static class PlaceholderFragment extends Fragment implements OnClickListener {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

        mplayer = new MediaPlayer();
        rgroup = (RadioGroup) rootView.findViewById(R.id.radioGroup1);

        play = (Button) rootView.findViewById(R.id.play);
        pause = (Button) rootView.findViewById(R.id.pause);

        play.setOnClickListener(this);
        pause.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View v) {

    } 
}

PS Салам алейкум =)

于 2014-03-30T11:27:31.147 回答