我正在研究NAvigation Drawer的片段.Andi将Activity的代码复制到片段中以执行片段.我已经这样做了,布局是可见的,但是没有按钮动作在Fragment Activity下工作.

你能告诉我我做错了什么吗?

FragmentTwo.java

public class FragmentTwo   extends Fragment {

    Button blog,fb,twitter,mail;
    ImageView dateimg;
    private boolean snet;


    public FragmentTwo()
    {

    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.contact_lay,container,false);


        System.out.println("Kaks7");
        dateimg = (ImageView) getActivity().findViewById(R.id.img);
        blog= (Button) getActivity().findViewById(R.id.btnGplus);
        fb = (Button) getActivity().findViewById(R.id.btnFb);
        twitter = (Button)getActivity().findViewById(R.id.btnTwitter);
        mail = (Button)getActivity().findViewById(R.id.btnMail);
        System.out.println("K17");
        new LongOperation().execute();


        return view;
    }



    private class LongOperation extends AsyncTask<Void,Void,Void> {



        protected void onPreExecute() {



        }


        @Override
        protected Void doInBackground(Void... params) {
            // Todo Auto-generated method stub


            return null;
        }


        @Override
        protected void onPostExecute(Void result) {

            try{

                 fb.setonClickListener(new OnClickListener()
                    {

                            public void onClick(View vb) 
                             {  
                                try{
                                    System.out.println("7");
                                    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("fb://profile/10000003242252"));
                                    startActivity(intent);

                                    }catch(Exception e){

                                    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://m.facebook.com/facebook")));
                                    }

                                }

                    });

                twitter.setonClickListener(new OnClickListener()
                {
                        public void onClick(View vc)
                         {  
                            try {

                                  Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("twitter://user?screen_name=dhoom"));
                                    startActivity(intent);

                                    }catch (Exception e) {
                                        startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://mobile.twitter.com/#!/dhoom"))); 
                                    } 

                            }

                });

                blog.setonClickListener(new OnClickListener()
                {
                        public void onClick(View va)
                         {  System.out.println("7s");
                            try {
                                System.out.println("5s");
                                String url = "http://google.com";
                                Intent i = new Intent(Intent.ACTION_VIEW);
                                i.setData(Uri.parse(url));
                                startActivity(i);


                            }

                            catch(Exception e){ 
                                System.out.println("6s");
                                            startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"))); 
                            } 

                        }

                });


                mail.setonClickListener(new OnClickListener()
                {
                        public void onClick(View va)
                         {  
                            try {
                                Intent send = new Intent(Intent.ACTION_SENDTO);
                                String uriText = "mailto:" + Uri.encode("serIoUslife@gmail.com") + 
                                          "?subject=" + Uri.encode("the subject") + 
                                          "&body=" + Uri.encode("the body of the message");
                                Uri uri = Uri.parse(uriText);

                                send.setData(uri);
                                startActivity(Intent.createChooser(send,"Send mail..."));
                            }

                            catch(Exception e){ 
                                Toast.makeText(getActivity().getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
                            //  this.progressDialog.dismiss();
                            } 
                        }

                });

                dateimg.setonClickListener(new OnClickListener()
                {
                        public void onClick(View va)
                         {  
                            if(snet==false)
                            {
                            soundstart();
                            snet=true;
                            }
                        }

                });





            }

            catch(Exception e){ 
                //Toast.makeText(getApplicationContext(),Toast.LENGTH_LONG).show();

            } 
        }



    }


    public void soundstart()
    {
        // Todo Auto-generated method stub
        MediaPlayer mplayer =MediaPlayer.create(getActivity(),R.raw.contact_sound);
        mplayer.start();

    }

}

活动

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"

    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:background="@drawable/abc">

    <RelativeLayout 
    android:layout_width="wrap_content"
    android:id="@+id/text"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:paddingTop="-30dp"
            android:text="© iamrajkaran || Raj Karan"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:textStyle="bold" />

    </RelativeLayout> 
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/linearlayout_bg"
        android:orientation="vertical"
        android:layout_marginTop="30dp"
        android:padding="7dp" >

        <Button
            android:id="@+id/btnGplus"
            style="@style/ButtonText"
            android:layout_width="fill_parent"
            android:layout_height="48dp"
            android:layout_margin="4dp"
            android:background="@drawable/layers_blog_button_bg"
            android:padding="10dp"
            android:text="KNow me on my blog" />
        <Button
            android:id="@+id/btnFb"
            style="@style/ButtonText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:background="@drawable/layers_fb_button_bg"
            android:padding="10dp"
            android:text="Join me on facebook" />
        <Button
            android:id="@+id/btnTwitter"
            style="@style/ButtonText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:background="@drawable/layers_twitter_button_bg"
            android:padding="10dp"
            android:text="Follow me on twitter" />
        <Button
            android:id="@+id/btnMail"
            style="@style/ButtonText"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_margin="4dp"
            android:background="@drawable/layers_mail_button_bg"
            android:padding="10dp"
            android:text="Mail Your Feedback" />


        </LinearLayout>


    <RelativeLayout
    android:layout_width="fill_parent"
    android:id="@+id/texwt"
    android:orientation="vertical"
    android:gravity="bottom"
    android:layout_gravity="center"
    android:layout_marginTop="240dp"
    android:layout_height="wrap_content"  >
        <ImageView
            android:id="@+id/img"
            android:layout_width="180dp"
            android:layout_height="180dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:paddingLeft="30dp"
              android:paddingRight="0dp"
            android:src="@drawable/ic_form" />

    </RelativeLayout>
    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:layout_marginTop="40dp"
        android:text="Touch Me"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF"
        android:textSize="22sp"
        android:textStyle="bold" />

</RelativeLayout>**

解决方法

像这样初始化你的小部件.
dateimg = (ImageView) view.findViewById(R.id.img);
 blog= (Button) view.findViewById(R.id.btnGplus);
 fb = (Button) view.findViewById(R.id.btnFb);
 twitter = (Button) view.findViewById(R.id.btnTwitter);
 mail = (Button) view.findViewById(R.id.btnMail);

android – setOnClickListener(新的OnClickListener()在Fragment中不起作用的更多相关文章

  1. HTML5 input新增type属性color颜色拾取器的实例代码

    type 属性规定 input 元素的类型。本文较详细的给大家介绍了HTML5 input新增type属性color颜色拾取器的实例代码,感兴趣的朋友跟随脚本之家小编一起看看吧

  2. amazeui模态框弹出后立马消失并刷新页面

    这篇文章主要介绍了amazeui模态框弹出后立马消失并刷新页面,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  3. 移动HTML5前端框架—MUI的使用

    这篇文章主要介绍了移动HTML5前端框架—MUI的使用的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  4. html5 canvas合成海报所遇问题及解决方案总结

    这篇文章主要介绍了html5 canvas合成海报所遇问题及解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  5. Html5 video标签视频的最佳实践

    这篇文章主要介绍了Html5 video标签视频的最佳实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  6. HTML5在微信内置浏览器下右上角菜单的调整字体导致页面显示错乱的问题

    HTML5在微信内置浏览器下,在右上角菜单的调整字体导致页面显示错乱的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

  7. AmazeUI 模态窗口的实现代码

    这篇文章主要介绍了AmazeUI 模态窗口的实现代码,代码简单易懂,非常不错,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  8. ios – containerURLForSecurityApplicationGroupIdentifier:在iPhone和Watch模拟器上给出不同的结果

    我使用默认的XCode模板创建了一个WatchKit应用程序.我向iOSTarget,WatchkitAppTarget和WatchkitAppExtensionTarget添加了应用程序组权利.(这是应用程序组名称:group.com.lombax.fiveminutes)然后,我尝试使用iOSApp和WatchKitExtension访问共享文件夹URL:延期:iOS应用:但是,测试NSURL

  9. ios – UIPopoverController出现在错误的位置

    所以我花了一些时间寻找答案,但到目前为止还没有找到任何答案.我正在尝试从UIInputAccessoryView上的按钮呈现弹出窗口.UIBarButtonItem我想显示popover来自定制视图,所以我可以使用图像.我创建这样的按钮:当需要显示popover时,我这样做:但我得到的是:弹出窗口看起来很好,但它应该出现在第一个按钮上时出现在第二个按钮上.然后我发现了这个问题:UIBarButto

  10. ios – 关闭UIBarButtonItem上的突出显示

    我正在尝试使用UIBarButtonItem在我的UIToolbar上添加标题.我使用简单的风格,看起来很好,但我似乎无法让它停止突出显示触摸.“突出显示时触摸”选项不适用于条形按钮项目.有没有快速简便的方法来做到这一点?

随机推荐

  1. bluetooth-lowenergy – Altbeacon库无法在Android 5.0上运行

    昨天我在Nexus4上获得了Android5.0的更新,并且altbeacon库停止了检测信标.似乎在监视和测距时,didEnterRegion和didRangeBeaconsInRegion都没有被调用.即使RadiusNetworks的Locate应用程序现在表现不同,一旦检测到信标的值,它们就不再得到更新,并且通常看起来好像信标超出了范围.我注意到的一点是,现在在logcat中出现以下行“B

  2. android – react-native动态更改响应者

    我正在使用react-native进行Android开发.我有一个视图,如果用户长按,我想显示一个可以拖动的动画视图.我可以使用PanResponder实现这一点,它工作正常.但我想要做的是当用户长按时,用户应该能够继续相同的触摸/按下并拖动新显示的Animated.View.如果您熟悉Google云端硬盘应用,则它具有类似的功能.当用户长按列表中的任何项目时,它会显示可拖动的项目.用户可以直接拖

  3. android – 是否有可能通过使用与最初使用的证书不同的证书对其进行签名来发布更新的应用程序

    是否可以通过使用与最初使用的证书不同的证书进行签名来发布Android应用程序的更新?我知道当我们尝试将这样的构建上传到市场时,它通常会给出错误消息.但有没有任何出路,比如将其标记为主要版本,指定市场中的某个地方?解决方法不,你不能这样做.证书是一种工具,可确保您是首次上传应用程序的人.所以总是备份密钥库!

  4. 如何检测Android中是否存在麦克风?

    ..所以我想在让用户访问语音输入功能之前检测麦克风是否存在.如何检测设备上是否有麦克风.谢谢.解决方法AndroidAPI参考:hasSystemFeature

  5. Android – 调用GONE然后VISIBLE使视图显示在错误的位置

    我有两个视图,A和B,视图A在视图B上方.当我以编程方式将视图A设置为GONE时,它将消失,并且它正下方的视图将转到视图A的位置.但是,当我再次将相同的视图设置为VISIBLE时,它会在视图B上显示.我不希望这样.我希望视图B回到原来的位置,这是我认为会发生的事情.我怎样才能做到这一点?编辑–代码}这里是XML:解决方法您可以尝试将两个视图放在RelativeLayout中并相对于彼此设置它们的位置.

  6. android – 获得一首歌的流派

    我如何阅读与歌曲相关的流派?我可以读这首歌,但是如何抓住这首歌的流派,它存放在哪里?解决方法检查此代码:

  7. android – 使用textShadow折叠工具栏

    我有一个折叠工具栏的问题,在展开状态我想在文本下面有一个模糊的阴影,我使用这段代码:用:我可以更改textColor,它可以工作,但阴影不起作用.我为阴影尝试了很多不同的值.是否可以为折叠文本投射阴影?

  8. android – 重用arm共享库

    我已经建立了armarm共享库.我有兴趣重用一个函数.我想调用该函数并获得返回值.有可能做这样的事吗?我没有任何头文件.我试过这个Android.mk,我把libtest.so放在/jni和/libs/armeabi,/lib/armeabi中.此时我的cpp文件编译,但现在是什么?我从objdump知道它的名字编辑:我试图用这个android.mk从hello-jni示例中添加prebuild库:它工作,但libtest.so相同的代码显示以下错误(启动时)libtest.so存在于libhello-j

  9. android – 为NumberPicker捕获键盘’Done’

    我有一个AlertDialog只有一些文本,一个NumberPicker,一个OK和一个取消.(我知道,这个对话框还没有做它应该保留暂停和恢复状态的事情.)我想在软键盘或其他IME上执行“完成”操作来关闭对话框,就像按下了“OK”一样,因为只有一个小部件可以编辑.看起来处理IME“Done”的最佳方法通常是在TextView上使用setonEditorActionListener.但我没有任何Te

  10. android – 想要在调用WebChromeClient#onCreateWindow时知道目标URL

    当我点击一个带有target=“_blank”属性的超链接时,会调用WebChromeClient#onCreateWindow,但我找不到新的窗口将打开的新方法?主页url是我唯一能知道的东西?我想根据目标网址更改应用行为.任何帮助表示赞赏,谢谢!

返回
顶部