我无法从片段中获取按钮,这有什么问题? 接受未捕获的异常 - 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;
}
}
}
P.S。对不起我的英文
答案 0 :(得分: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。 Саламалейкум=)