如何打开从的ImageButton网页视图? [英] How to open a webview from imagebutton?
本文介绍了如何打开从的ImageButton网页视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个按钮,一个片段,当在一个用户点击它会打开应用程序中的特定网页。当我尝试在手机上运行该code当我尝试访问选项卡崩溃。
我的.java类是在这里:
公共类SocialMediaFragment扩展片段{公众的ImageButton fbbutton;
公众的ImageButton twbutton;
公众的ImageButton igbutton;@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ 查看rootView = inflater.inflate(R.layout.fragment_socialmedia,集装箱,FALSE); fbbutton =(的ImageButton)getActivity()findViewById(R.id.fbbutton)。
twbutton =(的ImageButton)getActivity()findViewById(R.id.twbutton)。
igbutton =(的ImageButton)getActivity()findViewById(R.id.igbutton)。 fbbutton.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){ 意图browserIntent =新意图(Intent.ACTION_VIEW,Uri.parse(https://www.facebook.com/universityofhouston));
startActivity(browserIntent);
}
}); twbutton.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){ 意图browserIntent =新意图(Intent.ACTION_VIEW,Uri.parse(简称网站));
startActivity(browserIntent); }}); igbutton.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){ 意图browserIntent =新意图(Intent.ACTION_VIEW,Uri.parse(简称网站));
startActivity(browserIntent); }});
返回rootView;}
}
这是我使用的片段(XML)文件:
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@绘制/ uhwallpaper> <的ImageButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / fbbutton
机器人:背景=@绘制/ FB
机器人:layout_marginStart =46dp
机器人:layout_centerVertical =真
机器人:layout_alignParentStart =真/>
<的ImageButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / twbutton
机器人:背景=@绘制/叽叽喳喳
机器人:layout_above =@ + ID / igbutton
机器人:layout_toEndOf =@ + ID / fbbutton
机器人:layout_marginStart =43dp/> <的ImageButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / igbutton
机器人:背景=@绘制/的Instagram
机器人:layout_below =@ + ID / fbbutton
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =32dp/> < ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / socialslogan
机器人:背景=@绘制/ slogan_social
机器人:layout_alignParentTop =真
机器人:layout_alignParentStart =真
机器人:layout_marginTop =16DP/>< / RelativeLayout的>
我希望这是可以解决!如果您需要任何更多的信息让我知道。
这是我的错误报告:
显示java.lang.NullPointerException:尝试对空对象引用调用虚拟方法无效android.widget.ImageButton.setOnClickListener(android.view.View $ OnClickListener)
在info.teammumu.cougarcardapp.SocialMediaFragment.onCreateView(SocialMediaFragment.java:28)
解决方案
原来,问题是这些家伙:
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);
I have three buttons in a fragment that when the user clicks on one it'll open a specific webpage within the application. When I try to run this code on the phone it crashes when I try to access the tab.
My .java class is right here:
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;
}
}
And here is the fragment (XML) file I am using:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/uhwallpaper">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fbbutton"
android:background="@drawable/fb"
android:layout_marginStart="46dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/twbutton"
android:background="@drawable/twitter"
android:layout_above="@+id/igbutton"
android:layout_toEndOf="@+id/fbbutton"
android:layout_marginStart="43dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/igbutton"
android:background="@drawable/instagram"
android:layout_below="@+id/fbbutton"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/socialslogan"
android:background="@drawable/slogan_social"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp" />
</RelativeLayout>
I hope this is fixable! If you need any more information let me know.
Here is my error report:
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)
解决方案
Turns out the problem was with these guys:
fbbutton = (ImageButton) getActivity().findViewById(R.id.fbbutton);
twbutton = (ImageButton) getActivity().findViewById(R.id.twbutton);
igbutton = (ImageButton) getActivity().findViewById(R.id.igbutton);
they're supposed to be:
fbbutton = (ImageButton) rootView.findViewById(R.id.fbbutton);
twbutton = (ImageButton) rootView.findViewById(R.id.twbutton);
igbutton = (ImageButton) rootView.findViewById(R.id.igbutton);
这篇关于如何打开从的ImageButton网页视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文