URI.parse 和图像按钮错误
URI.parse and imagebutton error
该代码旨在在用户单击按钮时打开 webview,但出现错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at info.teammumu.cougarcardapp.SocialMediaFragment.onCreateView(SocialMediaFragment.java:28)
对于 java 代码:
public class SocialMediaFragment extends Fragment {
public ImageButton fbbutton;
public ImageButton twbutton;
public ImageButton igbutton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_socialmedia, container, false);
fbbutton = (ImageButton) getActivity().findViewById(R.id.fbbutton);
twbutton = (ImageButton) getActivity().findViewById(R.id.twbutton);
igbutton = (ImageButton) getActivity().findViewById(R.id.igbutton);
fbbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/universityofhouston"));
startActivity(browserIntent);
}
});
twbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("website"));
startActivity(browserIntent);
}});
igbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("website"));
startActivity(browserIntent);
}});
return rootView;
}}
我以前问过这样的问题,但是一直没有人回答或者我没看懂...
感谢任何可以提供帮助的人!在过去的几天里,我一直在努力解决这个问题,但没有成功。它崩溃了,我无法打开标签。
使用 rootView
而不是 getActivity()
从 return 从 onCreateView
编辑的 Fragment 布局访问视图:
fbbutton = (ImageButton) rootView.findViewById(R.id.fbbutton);
twbutton = (ImageButton) rootView.findViewById(R.id.twbutton);
.....
因为 getActivity
return 添加了当前片段的 Activity 上下文,所以调用 getActivity().findViewById(<ID>)
意味着从 Activity 布局而不是片段访问视图。
如果您在充气的同一视图中有按钮,那么您应该更改这些行:
fbbutton = (ImageButton) getActivity().findViewById(R.id.fbbutton);
twbutton = (ImageButton) getActivity().findViewById(R.id.twbutton);
igbutton = (ImageButton) getActivity().findViewById(R.id.igbutton);
像 :
fbbutton = (ImageButton) rootView.findViewById(R.id.fbbutton);
twbutton = (ImageButton) rootView.findViewById(R.id.twbutton);
igbutton = (ImageButton) rootView.findViewById(R.id.igbutton);
该代码旨在在用户单击按钮时打开 webview,但出现错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at info.teammumu.cougarcardapp.SocialMediaFragment.onCreateView(SocialMediaFragment.java:28)
对于 java 代码:
public class SocialMediaFragment extends Fragment {
public ImageButton fbbutton;
public ImageButton twbutton;
public ImageButton igbutton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_socialmedia, container, false);
fbbutton = (ImageButton) getActivity().findViewById(R.id.fbbutton);
twbutton = (ImageButton) getActivity().findViewById(R.id.twbutton);
igbutton = (ImageButton) getActivity().findViewById(R.id.igbutton);
fbbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/universityofhouston"));
startActivity(browserIntent);
}
});
twbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("website"));
startActivity(browserIntent);
}});
igbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("website"));
startActivity(browserIntent);
}});
return rootView;
}}
我以前问过这样的问题,但是一直没有人回答或者我没看懂...
使用 rootView
而不是 getActivity()
从 return 从 onCreateView
编辑的 Fragment 布局访问视图:
fbbutton = (ImageButton) rootView.findViewById(R.id.fbbutton);
twbutton = (ImageButton) rootView.findViewById(R.id.twbutton);
.....
因为 getActivity
return 添加了当前片段的 Activity 上下文,所以调用 getActivity().findViewById(<ID>)
意味着从 Activity 布局而不是片段访问视图。
如果您在充气的同一视图中有按钮,那么您应该更改这些行:
fbbutton = (ImageButton) getActivity().findViewById(R.id.fbbutton);
twbutton = (ImageButton) getActivity().findViewById(R.id.twbutton);
igbutton = (ImageButton) getActivity().findViewById(R.id.igbutton);
像 :
fbbutton = (ImageButton) rootView.findViewById(R.id.fbbutton);
twbutton = (ImageButton) rootView.findViewById(R.id.twbutton);
igbutton = (ImageButton) rootView.findViewById(R.id.igbutton);