我一直在尝试在片段上使用ImageButton,但我一直得到NullPointerException,并且不知道如何解决它。我已经阅读了许多类似的主题和教程,但实际上无法弄明白。这是我的片段:
public class OneFragment extends Fragment{
ViewGroup viewGroup;
ImageButton startButton, stopButton;
private Context context = null;
boolean isPlaying;
Intent streamService;
SharedPreferences prefs;
TextView textView;
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//start & play buttons
context = getActivity();
viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_one, container, false);
stopButton = (ImageButton) viewGroup.findViewById(R.id.stopButton);
startButton = (ImageButton) viewGroup.findViewById(R.id.startButton);
prefs = PreferenceManager.getDefaultSharedPreferences(context);
getPrefs();
streamService = new Intent(super.getActivity(), StreamService.class);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getActivity().startService(streamService);
startButton.setEnabled(false);
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getActivity().stopService(streamService);
startButton.setEnabled(true);
}
});
return viewGroup;
}
public void getPrefs() {
isPlaying = prefs.getBoolean("isPlaying", false);
if (isPlaying) startButton.setEnabled(false);
}}
这是我的日志:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at info.androidhive.materialtabs.fragments.OneFragment.onCreateView(OneFragment.java:78)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1026)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:545)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:607)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:674)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
at android.view.View.measure(View.java:15518)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
如果您有任何想法,我会非常乐意为您提供帮助!
答案 0 :(得分:2)
将大部分内容移至onViewCreated
,因为在此之前视图实际上并不存在!
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getActivity();
viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_one, container, false);
return viewGroup;
}
@Override
public void onViewCreated (final View view, final Bundle savedInstanceState) {
super.onViewCreated (view, savedInstanceState);
//start & play buttons
stopButton = (ImageButton) viewGroup.findViewById(R.id.stopButton);
startButton = (ImageButton) viewGroup.findViewById(R.id.startButton);
prefs = PreferenceManager.getDefaultSharedPreferences(context);
getPrefs();
streamService = new Intent(super.getActivity(), StreamService.class);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getActivity().startService(streamService);
startButton.setEnabled(false);
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getActivity().stopService(streamService);
startButton.setEnabled(true);
}
});
}
public void getPrefs() {
isPlaying = prefs.getBoolean("isPlaying", false);
if (isPlaying) startButton.setEnabled(false);
}}