2014-03-30 189 views
0

我不能從片段獲取按鈕,怎麼了? 接收未捕獲的異常 - nullpointerexception; 無法找到按鈕。Android:ActionBarActivity findViewById返回NULL

早期都將被發現,但更新的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對不起我的英語

+0

哪個佈局包含這些按鈕? –

+0

片段,我如何得到它? – IceJOKER

回答

3

您正在嘗試Activity的佈局裏面搜索,你小部件是一個Fragment的佈局裏面

把你的邏輯你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) { 

    } 
} 

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

+0

我明白,但我怎麼得到按鈕? – IceJOKER

+0

Ваатейкумассалам。謝謝; )) – IceJOKER

+0

...和我所有的屬性應該是靜態的? – IceJOKER