【问题标题】:RESULT_OK cannot be resolved to a variableRESULT_OK 无法解析为变量
【发布时间】:2015-12-09 17:14:42
【问题描述】:

我的这条线有问题,我正在尝试使用处理创建一个适用于 android 的应用程序,我是 android 新手。 这是我的代码:

    @Override
    public void onActivityResult(int requestCode, int resultCode,Intent data){
    if(requestCode==0){

    if(resultCode == RESULT_OK){
    BACKGND=2; //Set the background to GREEN
    } else {
    BACKGND=1; //Set the background to RED
    }
    }
    }

我收到以下错误:

1. ERROR in /tmp/android6837327122296929778sketch/src/processing/test/sketch_151209a/sketch_151209a.java (at line 52)
if(resultCode == RESULT_OK){
                 ^^^^^^^^^
RESULT_OK cannot be resolved to a variable

//////更新////////

完整代码如下:

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
int BACKGND=0; //Set the background to BLUE

//Get the default Bluetooth adapter
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();


/*The startActivityForResult() launches an Activity which is
used to request the user to turn Bluetooth on. 
 The following onActivityResult() method is called when the 
 Activity exits. */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent   data){
 if(requestCode==0){

 if(resultCode == Activity.RESULT_OK){
 BACKGND=2; //Set the background to GREEN
 } else {
 BACKGND=1; //Set the background to RED
 }
 }
}

void setup(){
 orientation(LANDSCAPE);

 /*IF Bluetooth is NOT enabled, 
 then ask user permission to enable it */
 if (!bluetooth.isEnabled()) {
 Intent requestBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 startActivityForResult(requestBluetooth, 0);
 }
}

void draw(){
 if(BACKGND==0){
 background(10,10,255); //Set background to BLUE
 } else if(BACKGND==1) {
 background(255,10,10); //Set background to RED
 } else {
 background(10,255,10); //Set background to GREEN
 }
}

【问题讨论】:

  • onActivityResult 声明在哪里?片段还是活动?

标签: android bluetooth arduino processing


【解决方案1】:

RESULT_OK 是 Activity 类的常量。如果此代码在 Fragment 中执行,则不会解析变量。

参考如下:

Activity.RESULT_OK

【讨论】:

  • 我已经尝试过了,但出现以下错误 1. /tmp/android1989785468487631807sketch/src/processing/test/sketch_151209a/sketch_151209a.java 中的错误(在第 52 行) if(resultCode == Activity .RESULT_OK){ ^^^^^^^^ Activity 无法解析为变量 ----------
  • 只是覆盖基地。 Activity 是导入的吗?它需要是 android.app.Activity.RESULT_OK
  • 从您添加的代码来看,此代码似乎不在一个类中,我也没有看到 Activity 类的导入。此代码需要在某种活动或片段中。你的类声明应该有SomeActivity extends ActivitySomeFragment extends Fragment 或类似的。你还需要import android.app.Activity;
  • 谢谢!添加“import android.app.Activity;”就可以了,这正是我所需要的。
猜你喜欢
  • 1970-01-01
  • 2012-04-14
  • 2021-12-21
  • 2012-02-29
  • 2020-12-07
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多