操作栏项目没有响应在Android中单击

时间:2014-09-10 11:26:14

标签: android android-actionbar

我正面临着一个问题......我在动作栏上创建了一个项目..当我点击那个项目我的项目点击按钮没有响应..对于打印我创建了Toast ..要检查Resopnse 但Toast是NOt显示

这是更新的片段类..

public class Welcome extends SherlockFragment {
    private ProgressDialog pDialog;
    Button editprofile;
    JSONParser jsonParser = new JSONParser();
     ImageView i;
    Editor editor;
    SharedPreferences sharedPreferences;
    ImageView frm;
    ImageLoader imageloader;
    String image_url;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {

        View rootView = inflater.inflate(R.layout.welcome, container, false);
        final UserModel user = (UserModel) getActivity().getIntent().getSerializableExtra("User");
        frm=(ImageView)rootView.findViewById(R.id.image_frm);
        // Button editprofile = (Button)rootView.findViewById(R.id.btn_edit);
        //ImageView image = (ImageView)rootView. findViewById(R.id.imageView1);
        TextView userfullname = (TextView)rootView. findViewById(R.id.userfullname);
        TextView tv_tomeet = (TextView)rootView. findViewById(R.id.tomeet);

        TextView tv_intrseted = (TextView) rootView.findViewById(R.id.intsdin);


        int loader = R.drawable.loader;

        String UserfullName = user.getUser_Full_Name();
        String Tomeet=user.getTo_Meet();
        String Age = user.getUser_Age();
        String Gender = user.getGender();
        String IntrestedIn = user.getIntrest_In();
        Bundle bundle = this.getArguments();
        String image_url=user.getUser_Image();
        userfullname.setText(UserfullName);
        tv_tomeet.setText(Tomeet);
        //image loader
        setHasOptionsMenu(true);
    //imageloader=new ImageLoader();

        //tv_gender.setText(Gender);
    ImageView     iv = (ImageView)rootView.findViewById(R.id.imageView1);
    Picasso.with(getActivity())
    .load(image_url).transform(new RoundedImageView()).into(iv);


    frm.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String image_url=user.getUser_Image();
             Bundle bundle = new Bundle();
             bundle.putString("UserImage", image_url);
        }
    });

        // ImageLoader class instance
        //ImageLoader imgLoader = new ImageLoader(getActivity());



    // whenever you want to load an image from url
    // call DisplayImage function
    // url - image url to load
    // loader - loader image, will be displayed before getting image
    // image - ImageView 
      /*  imgLoader.DisplayImage(image_url, Welcome.this, image);*/
        return rootView;

    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

    }

    @Override
    public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,MenuInflater inflater)
    {
        inflater.inflate(R.menu.action_bar_menu, menu);
        //super.onCreateOptionsMenu(menu, inflater);
    }
@Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                logout();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    ///Logout User And Clear Sharedpref
    public void logout(){
        Toast.makeText(getActivity(), "hi Logout", Toast.LENGTH_LONG).show();
         sharedPreferences = getActivity().getSharedPreferences(MainActivity.MyLOGINDATA
                 ,0);
                   editor = sharedPreferences.edit();
                  editor.clear();
                  editor.commit();
        Intent l=new Intent(getActivity(),MainActivity.class);
        startActivity(l);
        getActivity().finish();

    }



}

这是菜单项

<item 
          android:id="@+id/action_search"
          android:icon="@drawable/setting_iconn"
          android:title="Setting"
          android:showAsAction="always"
/>

我真的不明白我做错了什么? 请帮帮我

编辑

我创造了 在片段类

@Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

onclick Not Working Stil

4 个答案:

答案 0 :(得分:2)

编辑:了解到您使用的是Fragment

onCreate

Fragment方法中添加以下内容
setHasOptionsMenu(true);

答案 1 :(得分:1)

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.action_bar_menu, menu);
    return true;
}

你忘了覆盖,这就是原因。

修改

对于菜单,应该在FragmentActivity上进行,而不是在片段中进行。

但是,如果您想在片段中添加它,请查看该链接: Android Options Menu in Fragment(Marco HC回复)

答案 2 :(得分:0)

Sidd的回答是正确的。您需要覆盖onCreateOptionsMenu(Menu,Inflater)和onOptionsItemSelected(MenuItem)。如果要在片段或活动中添加覆盖方法,请使用这些快捷方式查找可用的覆盖方法。

对于Eclipse IDE - Alt + Shift + S. 对于Android Studio - Alt + Insert

在setContentView()

之后也给出setHasOptionsMenu(true)

答案 3 :(得分:0)

替换此代码

@Override
    public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,MenuInflater inflater)
    {
        inflater.inflate(R.menu.action_bar_menu, menu);
        //super.onCreateOptionsMenu(menu, inflater);
    }

with

    @Override
public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,MenuInflater inflater)
{

      menu.add(0, R.drawable.setting_iconn, R.id.action_search, "Setting").setIcon(R.drawable.setting_iconn).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    //super.onCreateOptionsMenu(menu, inflater);
}

这有效......