简体   繁体   English

我将如何使这个Handler类成为静态的?

[英]How would I go about making this Handler class static?

Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        Log.i(tag, "in handler");
        super.handleMessage(msg);
        switch (msg.what) {
            case SUCCESS_CONNECT:
                // DO something
                ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
                Toast.makeText(getApplicationContext(), "CONNECT", Toast.LENGTH_SHORT).show();
                String s = "successfully connected";
                connectedThread.write(s.getBytes());
                Log.i(tag, "connected");
                break;
            case MESSAGE_READ:
                byte[] readBuf = (byte[]) msg.obj;
                String string = new String(readBuf);
                Toast.makeText(getApplicationContext(), string, Toast.LENGTH_LONG).show();
                break;
        }
    }
};

I need some help making this Handler class static, but I don't really know how. 我需要一些帮助使此Handler类变为静态,但我真的不知道如何。

Message I get: 我收到的消息:

This Handler class should be static or leaks might occur (null) 该处理程序类应该是静态的,否则可能会发生泄漏(空) 图片

Here's Logcat complaining: 这是Logcat的抱怨:

Part 1: pastebin.com/xwy2zEPh 第1部分:pastebin.com/xwy2zEPh

Part 2: pastebin.com/9UBSWyr2 第2部分:pastebin.com/9UBSWyr2

If I get what you mean with static (which is probably not the right term) you want to create a different file, eg MyHandler.java, where you put 如果我对static的理解(这可能不是正确的术语)是什么意思,那么您想创建一个不同的文件,例如MyHandler.java,在其中放置

public class MyHandler implements Handler {

 private final ApplicationContext context;

 public MyHandler(final ApplicationContext context) {
    // Add Pre Condition for context not null
    this.context = context;
 }

 @Override
 public void handleMessage(Message msg) {
    // TODO Auto-generated method stub
    Log.i(tag, "in handler");
    super.handleMessage(msg);
    switch (msg.what) {
        case SUCCESS_CONNECT:
            // DO something
            ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket) msg.obj);
            Toast.makeText(context, "CONNECT", Toast.LENGTH_SHORT).show();
            String s = "successfully connected";
            connectedThread.write(s.getBytes());
            Log.i(tag, "connected");
            break;
        case MESSAGE_READ:
            byte[] readBuf = (byte[]) msg.obj;
            String string = new String(readBuf);
            Toast.makeText(context, string, Toast.LENGTH_LONG).show();
            break;
    }
 }
}

and then 接着

 final ApplicationContext context = getApplicationContext();
 Handler mHandler = new MyHandler(context);

NB I made the assumption that Handler is an interface... 注意我假设Handler是一个接口...

Edited Due to Andy's comment 由于安迪的评论而编辑

I fixed the issue. 我解决了这个问题。 The reason it was crashing allot was because I forgot to add 分配失败的原因是我忘记添加

@Override
    protected void onResume() {
        super.onResume();
        registerReceiver(receiver, filter);

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将如何 go 关于使用原始数组整数制作 class ? - How would I go about making a class with a primitve array integers? 我将如何为这样的算法建立递归关系? - How would I go about making a recurrence relation for an algorithm like this? 如何保存带有说明和价格的班级中的物品? 还是我会怎么做? 与addItem方法? - How would I save items in my class with description and price? Or how would I go about it? with the addItem method? 我该如何使我的JFrame的油漆自动更新? - How would I go about making my paint for my JFrame automatically update? 我将如何 go 关于制作程序以单击谷歌表单上的按钮? - How would I go about making a program to click a button on a google form? 我将如何进行高效的键值存储(例如memcache)/简单的数据库? - How would I go about making an efficient key value store (e.g. memcache) / simple database? 我将如何解析 Java 类文件常量池? - How would I go about parsing the Java class file constant pool? 我将如何创建供paintComponent使用的自定义图形类? - How would I go about creating a custom graphics class for paintComponent to use? 我将如何创建一个字段,其中存储另一个类的对象? - How would I go about in creating a field in which store object of another class? 我将如何调用从单独的包中运行另一个类? - How would I go about calling in to run another class from a separate package?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM