Val
Val

Reputation: 4366

onCreateOptionMenu android Issue

It's the usual activity. The problem is in onCreateOptionMenu. When I click the menu button nothing is done. I don't see where the problem is.

I also try to comment all code without menu but It's still don't work. It is either a very strange problem or I don't see some simple things..

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
    import android.content.Intent;
    import android.content.res.AssetManager;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.widget.LinearLayout;
public class Izu4aikaActivity extends Activity implements OnClickListener {

    public final int INFO = 101;
    public final int BLOCK = 102;
    public final int CLOSE = 103;
    final String sdDir = Environment.getExternalStorageDirectory()+"/izuchaika/";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        // Thread to write files to SD

        final String file = "Files"; 
        File dir = new File(sdDir);
        dir.mkdir();
        dir.mkdirs();

        new Thread(new Runnable() {
            public void run() {
                copyFileOrDir(file);
            }
        }).start();

        LinearLayout ll = (LinearLayout) findViewById(R.id.main_layout);
        ll.setOnClickListener(this);
    }

    //Menu
    public boolean onCreateOptionMenu(Menu menu) {
        menu.add(Menu.NONE, INFO, Menu.NONE, "О программе").setIcon(
                R.drawable.info);
        menu.add(Menu.NONE, BLOCK, Menu.NONE, "Блокировать").setIcon(
                R.drawable.block);
        menu.add(Menu.NONE, CLOSE, Menu.NONE, "Выход").setIcon(R.drawable.exit);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onClick(View v) {
        Intent i = new Intent(this, mScr.class);
        startActivity(i);
    }

    private void copyFileOrDir(String path) {
        AssetManager assetManager = this.getAssets();
        String assets[] = null;
        try {
            assets = assetManager.list(path);
            if (assets.length == 0) {
                copyFile(path);
            } else {
                String fullPath = sdDir + path;
                File dir = new File(fullPath);
                if (!dir.exists())
                    dir.mkdir();
                for (int i = 0; i < assets.length; ++i) {
                    copyFileOrDir(path + "/" + assets[i]);
                }
            }
        } catch (IOException ex) {
            Log.e("tag", "I/O Exception", ex);
        }
    }

    private void copyFile(String filename) {
        AssetManager assetManager = this.getAssets();

        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            String newFileName = sdDir + filename;
            out = new FileOutputStream(newFileName);

            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e) {
            Log.e("tag", e.getMessage());
        }

    }

}

Help if you see a solution.

Upvotes: 1

Views: 1056

Answers (3)

jcxavier
jcxavier

Reputation: 2232

Change your onCreateOptionsMenu to:

//Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, INFO, Menu.NONE, "О программе").setIcon(
            R.drawable.info);
    menu.add(Menu.NONE, BLOCK, Menu.NONE, "Блокировать").setIcon(
            R.drawable.block);
    menu.add(Menu.NONE, CLOSE, Menu.NONE, "Выход").setIcon(R.drawable.exit);
    return true;
}

It should work now!

Upvotes: 1

Vladimir
Vladimir

Reputation: 9743

You've misspelled method name - it should be onCreateOptionsMenu(), not onCreateOptionMenu(). It's preferable to use @Override annotation to avoid such mistakes:

@Override   
public boolean onCreateOptionsMenu(Menu menu) {

This way it will give you compile time error if you misspell method name or use wrong parameters.

Upvotes: 4

Vladimir
Vladimir

Reputation: 3812

try to add @Override to public boolean onCreateOptionMenu
And do you try to use MenuInflater and inflate menu from xml?

Upvotes: 0

Related Questions