Android:无法以非静态方法设置getContext()-需要API级别23 [英] Android: Unable to set getContext() in non-static method - requires API Level 23

查看:168
本文介绍了Android:无法以非静态方法设置getContext()-需要API级别23的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一些从非静态方法调用的源代码。我收到错误

Below, is some source code which is being called from a non-static method. I'm getting the error


调用要求API级别23(当前最小值为15);
android.app.Fragment #getContext

"Call requires API level 23 (current min is 15); android.app.Fragment#getContext"



android.content.Context context = (Context) getContext();

如何获取上下文对象?

How can I get the context object?

在对象浏览器> Gradle Scripts> build.gradle下,我看到了。在我看来像23版。我在找正确的位置吗?

Under the object explorer > Gradle Scripts > build.gradle, I see this. Looks like version 23 to me. Am I looking in the correct location?

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.test.test"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:support-v4:23.2.1'
}

看起来像我有Android Studio 1.5.1。我只是在Android Studio中选择了SettingsActivity项目模板。并将一个类添加到该默认代码中。

Looks like I have Android Studio 1.5.1. I just chose the SettingsActivity project template in Android Studio. And added one class to that default code.

推荐答案

您需要导入import android.support.v4.app.Fragment而不是导入android.support.app.Fragment;

You need to import import android.support.v4.app.Fragment instead of import android.support.app.Fragment;

这篇关于Android:无法以非静态方法设置getContext()-需要API级别23的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆