Android:ActionBarActivity findViewById return NULL

江枫思渺然 提交于 2019-12-30 14:43:32

问题


I can't to get the button from fragment, what's wrong? Recieve a uncaught exception - nullpointerexception; Button can't be found.

Early all will be find, but after update android sdk, all is changed ((

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;
        }
    }

}

p.s. sorry for my english


回答1:


You are trying to search inside Activity's layout, and your widgets are inside a Fragment's layout.

Put your logic inside your PlaceHolderFragment class:

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) {

    } 
}

P.S. Салам алейкум =)



来源:https://stackoverflow.com/questions/22743370/androidactionbaractivity-findviewbyid-return-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!