menuinflator 和 onclicklistener 的问题

发布于 2024-11-10 09:00:17 字数 2634 浏览 5 评论 0原文

所以我有一个菜单充气器,当我在其中选择某些内容时,我的程序会强制关闭,我相信这是因为该类还为我必须添加的一系列按钮实现了 onclicklistener。以下是一些相关代码:

    package com.riley.howmany;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class howMany extends Activity implements View.OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//I have to use a dynamic layout because it changes based on user options.
//As of right now it is just in a for loop because the settings menu won't open
//because of this issue. I hope this makes sense.
        ScrollView sv = new ScrollView(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);
        ll.setPadding(1,1,1,1);

        TextView tv = new TextView(this);
        tv.setText("Dynamic layouts ftw!");
        ll.addView(tv);
//Each button press actually performs the same code for that individual button
        for (int c=0; c<=10; c++) {
            Button b = new Button (this);
            b.setText("Button:"+" "+"0");
            b.setTextSize(10.0f);
            b.setOnClickListener(this);
            ll.addView(b);
        }

        this.setContentView(sv);
    }

    public void onClick(View view) {
        //handle each button click
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent(this, Setting.class);
        startActivity(intent);

        return true;

    }
}

非常感谢您提供的任何建议!

编辑/更新!

好的,我找到了修复方法。就是这样:

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

@Override
public boolean onOptionsItemSelected (MenuItem item) {
            startActivity(new Intent(this, Setting.class));
            return true;
    }

现在效果很好!谢谢你的帮助。

So I have a menu inflator and when I select something in it, my program force closes and I believe it is because the class also implements onclicklistener for an array of buttons I have to add. Here is some of the relevant code:

    package com.riley.howmany;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class howMany extends Activity implements View.OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//I have to use a dynamic layout because it changes based on user options.
//As of right now it is just in a for loop because the settings menu won't open
//because of this issue. I hope this makes sense.
        ScrollView sv = new ScrollView(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);
        ll.setPadding(1,1,1,1);

        TextView tv = new TextView(this);
        tv.setText("Dynamic layouts ftw!");
        ll.addView(tv);
//Each button press actually performs the same code for that individual button
        for (int c=0; c<=10; c++) {
            Button b = new Button (this);
            b.setText("Button:"+" "+"0");
            b.setTextSize(10.0f);
            b.setOnClickListener(this);
            ll.addView(b);
        }

        this.setContentView(sv);
    }

    public void onClick(View view) {
        //handle each button click
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent(this, Setting.class);
        startActivity(intent);

        return true;

    }
}

Thank you very much for any advice you can provide!

EDIT/UPDATE!

Ok I found the fix. Here it is:

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

@Override
public boolean onOptionsItemSelected (MenuItem item) {
            startActivity(new Intent(this, Setting.class));
            return true;
    }

Now it works great! Thank you for the help.

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

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

发布评论

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

评论(1

笑叹一世浮沉 2024-11-17 09:00:17

您的 onClick 侦听器不应该是问题。
强制关闭可以来自您的 startActivity() 调用。
您确定已在清单中声明了设置活动吗?

你能发布你的整个清单吗?

Your onClick listener shouldn't be the problem.
The force close can come from your startActivity() call.
Are you sure you have declared the Setting activity in your manifest?

Could you post your whole manifest?

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