尽管图像按钮被初始化,但没有被调用

时间:2015-01-07 13:59:28

标签: android android-intent onclicklistener imagebutton

创建了一个imagebutton:facebook图标和twitter图标。因此,当用户点击任一图标时,它将分别调用新意图;因此,如果用户点击了facebook图标,它将调用facebook.class的意图。

但是,当用户点击facebook图标或推特图标时,它没有响应,并且在同一方法中调用不同的意图。有人可以帮忙吗?感谢

XML代码:

 <!-- The navigation drawer:right drawer-->
<FrameLayout 
    android:id="@+id/preview_layout"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:background="#D8D8D8"
    android:typeface="serif" >


    <LinearLayout 
        android:id="@+id/preview_header"
        android:layout_width="match_parent"
        android:layout_height="50dp"              
        android:adjustViewBounds="true"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:textStyle="bold"
            android:text="Product Preview"
            android:textColor="#000000"
            android:typeface="serif" 
            android:textSize="20sp"/>  

 <ImageButton
     android:id= "@+id/facebook_icon"
     android:src= "@drawable/facebook"
     android:paddingRight="10dp" 
     android:paddingLeft="10dp"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:clickable="true"
     android:background="@color/transparent" 
     android:layout_toLeftOf="@+id/twitter_icon"/>
  <ImageButton 
     android:id= "@+id/twitter_icon"
     android:src= "@drawable/twitter_icon" 
     android:paddingLeft="10dp"
     android:paddingRight="10dp"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@color/transparent" 
     android:layout_marginRight="10dp" 
     android:clickable="true"
     android:layout_alignParentRight="true"/>


    </LinearLayout>
...

代码:

listView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

            //DAPO-DEV02:20150107: Social Media Sharing: Twitter

                ImageView TwitterBtn = (ImageView) mContex.findViewById(R.id.twitter_icon);
                TwitterBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContex.getApplicationContext(),
                                ShareTwitter.class);
                        startActivity(intent);
                    }
                });



                //DAPO-DEV02:20150107: Social Media Sharing: Facebook
                ImageButton FacebookBtn = (ImageButton) mContex.findViewById(R.id.facebook_icon);
                FacebookBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContex.getApplicationContext(),
                                ShareFacebook.class);
                        startActivity(intent);
                    }
                });

                ProductImage =(ImageView) RDrawerView.findViewById(R.id.productimage);
                String imgFilePath = url + imageLargename;
                imageLoader.displayImage(imgFilePath, ProductImage, options);


                ProductImage.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Toast.makeText(mContex,
                                "Pinch & Zoom To See Large Photo.",
                                Toast.LENGTH_SHORT).show();
                        String imgFilePath = url + imageLargename;
                        Intent intent = new Intent(
                                mContex.getApplicationContext(),
                                FullScreenViewActivity.class);
                        intent.putExtra("path", imgFilePath);
                        startActivity(intent);
                    }

                });

                //END OF EDITED VERSION TO DISPLAY VIEW FOR SELECTED FRAGMENT ITEM-19/11/2014
            }
        });

2 个答案:

答案 0 :(得分:0)

设法破译了问题,并提出了一个可以调用次要意图的解决方案:

代码:

//DAPO-DEV02:20150107: Social Media Sharing: Twitter

final ImageButton TwitterBtn = (ImageButton) mContex.findViewById(R.id.twitter_icon);
TwitterBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        String cKey = "MNjFvsNstiMXcddTLc4J0IsHa";
                        String cSecret = "NgziPWnykalqAeMZhDUygqAYS89qJtVsehv9aQhLSlZW2myZXp";
                        final String tw="";



                        if (shareTwitter == null)
                            shareTwitter = new ShareTwitter(cKey, cSecret, ((android.app.Activity) getActivity()),
                                    getActivity().getApplicationContext(), R.drawable.twitter_icon);

                        if (socialBgAsyncTask == null) {
                            socialBgAsyncTask = (SocialBgAsyncTask) new SocialBgAsyncTask();
                            socialBgAsyncTask.execute();
                        }
                        if (twResponse == null) {
                            twResponse = "";
                            try {

                                String twitterURL = "https://twitter.com/intent/tweet?text=";
                                try {
                                    Intent i = new Intent(Intent.ACTION_VIEW);
                                    i.setData(Uri.parse(twitterURL+Uri.encode(tw)));
                                    startActivity(i);
                                } catch (android.content.ActivityNotFoundException e){
                                    Toast.makeText(getActivity(), "Can't tweet", Toast.LENGTH_SHORT).show();
                                }
                                Toast.makeText(getActivity(), "Posting Twitter data...", Toast.LENGTH_SHORT).show();
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        } else {
                            Toast.makeText(getActivity(), "Processing post data...", Toast.LENGTH_SHORT).show();
                        }
                    }
});

//DAPO-DEV02:20150108: Edited version for Social Media Sharing: facebook and twitter
private class SocialBgAsyncTask extends AsyncTask<Void, Integer, Long> {
    protected Long doInBackground(Void... params) {
        Log.i("----------SocialBgAsyncTask----------", "doInBackground");
        long result = 0;
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            publishProgress(0);
        } while (!exitAsync);
        return result;
    }

    protected void onProgressUpdate(Integer... progress) {

        // ====================TWITTER
        if (twResponse != null && !twResponse.equals("")) {
            if (twResponse.equals("success"))
                Toast.makeText(getActivity(), "Message has been successfully Tweeted!", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(getActivity(), "Failed tweeting message, please try again later.", Toast.LENGTH_SHORT).show();
            twResponse = null;
        }
    }

    protected void onPostExecute(Long result) {
        Log.i("----------SocialBgAsyncTask ----------", "onPostExecute");
    }
}

答案 1 :(得分:0)

简单地替换

ImageView TwitterBtn = (ImageView) mContex.findViewById(R.id.twitter_icon);

ImageButton TwitterBtn = (ImageButton) mContex.findViewById(R.id.twitter_icon);