onCreateOptionMenu android 问题

发布于 2024-12-18 15:33:44 字数 3543 浏览 0 评论 0原文

这是平常的活动。问题出在onCreateOptionMenu 上。当我单击菜单按钮时,什么也没做。我不明白问题出在哪里。

我也尝试在没有菜单的情况下注释所有代码,但它仍然不起作用。 这要么是一个非常奇怪的问题,要么我没有看到一些简单的事情。

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());
        }

    }

}

如果您看到解决方案,请提供帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以可爱出名 2024-12-25 15:33:44

您拼错了方法名称 - 它应该是 onCreateOptionsMenu(),而不是 onCreateOptionMenu()
最好使用 @Override 注解来避免此类错误:

@Override   
public boolean onCreateOptionsMenu(Menu menu) {

这样,如果您拼错方法名称或使用错误的参数,则会出现编译时错误。

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.

回眸一笑 2024-12-25 15:33:44

将您的 onCreateOptionsMenu 更改为:

//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;
}

它现在应该可以工作了!

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!

千鲤 2024-12-25 15:33:44

尝试添加
@Override to public boolean onCreateOptionMenu
您是否尝试使用 MenuInflater 并从 xml 中扩充菜单?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文