Android opOptionsItemSelected菜单不起作用

时间:2014-03-02 19:48:16

标签: android onclick android-menu

我的Android应用中的菜单有问题:当我点击按钮时显示,但是当我尝试点击将启动另一个活动的选项时,它什么也没做。它也不会崩溃。

以下是菜单的代码:

@Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle item selection
                switch (item.getItemId()) {
                    case R.id.bookmark:

                        this.item.setBookmark(!this.item.getBookmarks())  ;

                        if(this.item.getBookmarks()){
                            bookmark.setImageResource(R.drawable.favorito1);
                        }
                        else{
                            bookmark.setImageResource(R.drawable.favorito);
                        }

                        return true;
                    case R.id.settings:
                        Intent settings = new Intent(this,Settings.class);
                        startActivity(settings);  
                        return true;
                    default:
                        return super.onOptionsItemSelected(item);
                }
            }

这是我活动的完整代码:

public class EventList extends Activity implements OnClickListener  {

    ImageView bookmark;
    ImageView palau;
    ImageView noimage;

    TextView nom;
    TextView cognom;
    TextView data;
    TextView descripcio;

    Button map;
    Button join;

    Context ct;
    ItemEvent item;

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_list);

        item = (ItemEvent) getIntent().getSerializableExtra("item");
        ct = this;
        Toast.makeText(this, item.getNom(), Toast.LENGTH_LONG).show();
        element_confi(); //line 63!
                        }

        public void element_confi() {

            bookmark = (ImageView) findViewById(R.id.bookmark);
            nom = (TextView) findViewById(R.id.nom);
            data= (TextView) findViewById(R.id.data);
            descripcio = (TextView) findViewById(R.id.descripcio);
            map = (Button) findViewById(R.id.mapa);
            join = (Button) findViewById(R.id.join);

            nom.setText(item.getNom());
            data.setText(item.getDate()); 
            descripcio.setText(item.getDescription());

            if( item.getBookmarks()){
                bookmark.setImageResource(R.drawable.favorito1); //line 80!
            }
            else{
                bookmark.setImageResource(R.drawable.favorito);
            }

            map.setOnClickListener(this);
            join.setOnClickListener(this);

            ViewPager viewPager= (ViewPager) findViewById(R.id.view_pager);
            ImageAdapter adapter = new ImageAdapter(item.getRutaFoto1(),item.getRutaFoto2());
            viewPager.setAdapter(adapter);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.event, menu);
            return true;
        }

         @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle item selection
                switch (item.getItemId()) {
                    case R.id.bookmark:

                        this.item.setBookmark(!this.item.getBookmarks())  ;

                        if(this.item.getBookmarks()){
                            bookmark.setImageResource(R.drawable.favorito1);
                        }
                        else{
                            bookmark.setImageResource(R.drawable.favorito);
                        }

                        return true;
                    case R.id.settings:
                        Intent settings = new Intent(this,Settings.class);
                        startActivity(settings);  
                        return true;
                    default:
                        return super.onOptionsItemSelected(item);
                }
            }

        @Override
        public void onClick(View v) {

            switch (v.getId()) {
            case R.id.mapa:
                Toast.makeText(ct, "mostrar mapa", Toast.LENGTH_LONG).show();
                break;

            case R.id.join:
                Intent apuntar = new Intent(ct, JoinEvents.class);
                startActivity(apuntar);
                break;
            }
        }

        private class ImageAdapter extends PagerAdapter {

            private int[] mImages;

            public ImageAdapter(int image1Recurso, int image2Recurso){
                mImages = new int[] { image1Recurso,
                        image2Recurso,

                };
            }

            @Override
            public int getCount() {
                return mImages.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == ((ImageView) object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {

                Context context = EventList.this;
                ImageView imageView = new ImageView(context);
                int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
                imageView.setPadding(padding, padding, padding, padding);
                imageView.setScaleType(ImageView.ScaleType.FIT_START);
                imageView.setImageResource(mImages[position]);
                ((ViewPager) container).addView(imageView, 0);
                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                ((ViewPager) container).removeView((ImageView) object);
            }

        }

}

非常感谢你的帮助。

您诚挚的,

莫罗。

编辑:

Settings.java

(有些地方正在等待抛光:不要介意它们)

public class Settings extends Activity implements OnItemSelectedListener, OnClickListener {

    CheckBox view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        //onCheckBoxClicked();
        //onRadioButtonClicked();
        Spinner_Menu();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.settings, menu);
        return true;
    }

    //checkbox
public void onCheckBoxClicked (View v)
    {
    view = (CheckBox)findViewById(R.id.Setting1);
    boolean checking = ((CheckBox) v).isChecked();
switch (view.getId())
    {
case R.id.Setting1:
if (checking)
{
//  //checkbox checked!
}

else
{
    //not checked!  
}

break;

                }
    }

//Menú desplegable  
  public void Spinner_Menu()
  {
      Spinner spinner = (Spinner) findViewById(R.id.Settings2);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.menu, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);  
        spinner.setOnItemSelectedListener(this);
  }

    public void onRadioButtonClicked(View v)
    {
        boolean checked = ((RadioButton) v).isChecked();
        switch (view.getId())
        {
        case R.id.Setting3_1:
        if (checked)
        {
            //Option 1
        }

        break;

case R.id.Setting3_2:
        if (checked)
        {
            //Option 2
        }

        break;
        }
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}

EventList的布局:

<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=".EventList" >

<LinearLayout  
android:id="@+id/linear"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical" 
     android:gravity="center">

 <ListView
     android:id="@+id/list"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical" 
     android:gravity="center"

     >
      </ListView>   

 </LinearLayout>

<TextView
    android:id="@+id/nom"
    android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nom Event" 
        android:layout_below="@+id/linear"
    />

    <ImageView 
        android:id="@+id/bookmark"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/favorito"
        android:layout_below="@+id/nom"
        />

<android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/view_pager"
        android:layout_below="@id/bookmark"
        android:layout_width="match_parent"
        android:layout_height="200dp" 
        />

<TextView
        android:id="@+id/data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/view_pager"
        android:text="TextView" />

<Button
        android:id="@+id/mapa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/data"
        android:text="Com arribar" />

     <Button
        android:id="@+id/join"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/mapa"
        android:text="Join event" />


     <TextView
        android:id="@+id/descripcio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/join"
        android:text="Descripció event" />



 </RelativeLayout>

以防万一,Settings.xml文件也是:

<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=".Settings" >

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="setting1"/>

        <CheckBox
        android:id="@+id/Setting1"
        android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Checkbox1"
    android:layout_below="@+id/linear1"/>

    </LinearLayout>

        <LinearLayout
            android:id="@+id/linear2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/linear1">

           <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="setting2"/>

           <Spinner
        android:id="@+id/Settings2"
        android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Desplegable"
    android:layout_below="@+id/linear2"/>

                    </LinearLayout>

        <LinearLayout
            android:id="@+id/linear3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/linear2">

<TextView
    android:id="@+id/tv3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="setting3"/>

       <RadioButton
        android:id="@+id/Setting3_1"
        android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Opció1"    
    android:layout_below="@+id/linear3"/>

    <RadioButton
        android:id="@+id/Setting3_2"
        android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Opció2"
    android:layout_below="@+id/Setting3_1"/>

</LinearLayout>


</RelativeLayout>

非常感谢。

2 个答案:

答案 0 :(得分:1)

尝试将此添加到EventList.java:

@Override
public boolean onOptionsItemSelected(MenuItem item) {   
    switch(item.getItemId()) {  
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        case R.id.settings:
            Intent settingsIntent = new Intent(this, Settings.class);
            startActivity(settingsIntent);
            return true;
    }
}

答案 1 :(得分:0)

你可以粘贴你的event.xml以及扩展PreferenceActivity的Settings.class吗?

如果您正在使用eclipse,有时也会发生这种情况。只需清理尝试清理并重建您的项目。

如果它不起作用,那么试试:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.createOnOptionsMenu(menu);
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.event, menu);
  return true;
}