简体   繁体   English

方法未定义类型对象

[英]method is undefined for the type object

I am using the tutorial from the link http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/ and try to get the screen on and off data in android phone. 我正在使用http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/链接中的教程,并尝试在android中启用和关闭屏幕数据电话。 But I come up with the The method onpause is undefined for the type object , the same error happens in the OnResume method. 但是我想出了The method onpause is undefined for the type object ,在OnResume方法中发生了同样的错误。 I set the android level from 10-17. 我将android级别设置为10-17。

Here is the whole code for that: 这是完整的代码:

package com.example.myfirstapp;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;

public class ExampleActivity {
    protected void onCreate() {
        // INITIALIZE RECEIVER
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        ScreenReceiver mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
        // YOUR CODE
    }

    private void registerReceiver(ScreenReceiver mReceiver, IntentFilter filter) {
        // TODO Auto-generated method stub

    }

    protected void onPause() {
        // WHEN THE SCREEN IS ABOUT TO TURN OFF
        if (ScreenReceiver.wasScreenOn) {
            // THIS IS THE CASE WHEN ONPAUSE() IS CALLED BY THE SYSTEM DUE TO A SCREEN STATE CHANGE
            System.out.println("SCREEN TURNED OFF");
        } else {
            // THIS IS WHEN ONPAUSE() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
        }
        super.onPause();
    }

    protected void onResume() {
        // ONLY WHEN SCREEN TURNS ON
        if (!ScreenReceiver.wasScreenOn) {
            // THIS IS WHEN ONRESUME() IS CALLED DUE TO A SCREEN STATE CHANGE
            System.out.println("SCREEN TURNED ON");
        } else {
            // THIS IS WHEN ONRESUME() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
        }
        super.onResume();
    }
}

First off, 首先,

public class ExampleActivity {
...

For any activities you define in Android, you must extend from the Activity class 对于您在Android中定义的任何活动,您必须从Activity类扩展

public class ExampleActivity extends Activity {

OnCreate, OnResume and OnPause are override methods, so change them accordingly OnCreate,OnResume和OnPause是覆盖方法,因此请相应地更改它们

@Override
protected void onCreate() {
...

@Override
protected void onPause() {
...

@Override
protected void onResume() {
...

The issues is that you have to extend Activity (check the article to confirm this), you also have to Override each of the inherited methods (using the @Override notation). 问题是您必须扩展Activity(检查文章以确认这一点),您还必须覆盖每个继承的方法(使用@Override表示法)。

Also, you have to remove "registerReciever" as this call is inherited from (and handled by) the Activity. 此外,您必须删除“registerReciever”,因为此调用是从Activity继承(并由其处理)。

I have posted what I believe is the correct coding. 我发布了我认为正确编码的内容。

package com.example.myfirstapp;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;

public class ExampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // INITIALIZE RECEIVER
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    ScreenReceiver mReceiver = new ScreenReceiver();
    registerReceiver(mReceiver, filter);
    // YOUR CODE
}


@Override
protected void onPause() {
    // WHEN THE SCREEN IS ABOUT TO TURN OFF
    if (ScreenReceiver.wasScreenOn) {
        // THIS IS THE CASE WHEN ONPAUSE() IS CALLED BY THE SYSTEM DUE TO A SCREEN STATE CHANGE
        System.out.println("SCREEN TURNED OFF");
    } else {
        // THIS IS WHEN ONPAUSE() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
    }
    super.onPause();
}

@Override
protected void onResume() {
    // ONLY WHEN SCREEN TURNS ON
    if (!ScreenReceiver.wasScreenOn) {
        // THIS IS WHEN ONRESUME() IS CALLED DUE TO A SCREEN STATE CHANGE
        System.out.println("SCREEN TURNED ON");
    } else {
        // THIS IS WHEN ONRESUME() IS CALLED WHEN THE SCREEN STATE HAS NOT CHANGED
    }
    super.onResume();
}
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM