调用需要API级别11(目前最小为8)android.app.Activity#onCreateView最小、级别、目前、API

2023-09-05 01:58:43 作者:弃我者〃后会无期

我是一个新手,Android和我开发一个Android应用程序。但我的包线给出MainActivity.java类此错误。 谁能告诉我这是为什么? 这是我的类和包线给出了这样的错误。

 包com.example.eventgyaam;


进口android.os.Bundle;
进口android.support.v7.app.AppCompatActivity;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TextView;

公共类MainActivity扩展AppCompatActivity {

INT计数器;

按钮添加,子;
TextView的显示;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    计数器= 0;
    添加=(按钮)findViewById(R.id.bAdd);
    分=(按钮)findViewById(R.id.bSub);
    显示器=(按钮)findViewById(R.id.tvDisplay);
    add.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            反++;
            display.setText(你总为+专柜);
        }
    });
    sub.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            计数器 - ;
            display.setText(你总为+专柜);
        }
    });

}
}
 

解决方案

首先,一些介绍性的细节。

有Android的API中的含量的,并在每个级别,他们可能会引入新的功能,按的这里。

Android App Bundle 最新动态及 2021 年目标 API 级别要求

当你创建一个Android应用程序,您可以指定您的应用程序将运行在最低水平。这是在清单使用-SDK 节做:看的此处了解详细信息。

所以,如果你使用的功能,是只适用于11级,你的code的不会的运行在一个8级的API,就像你指定的。

您要么只使用什么是可在您的电流最小,涨幅达最低。

至于为什么你似乎越来越在包装线上的错误,并为呼叫似乎并不在源$ C ​​$ C,你已经证明了我们,我不能说。由于它在抱怨 android.app.Activity#onCreateView 按照你的头衔,还有的可以的是一个问题的观点 R.layout.activity_main 就是被夸大了的setContentView 。或者,它可能是一个问题兼容性库本身。

不过,而不是疲于应付这个太难了,我会建议只是加大了最低至API-11和共下跌了 appcompat 的东西。按此处,只有约5%的Andr​​oid用户仍然回到之前版本的Andr​​oid的3.0 / API-11(2015年8月时)。这是只可能变得越来越小,随着时间的推移。

I am a newbie to android, and I am developing an android application. But my package line gives this error in MainActivity.java class. Could anyone please tell me the reason of this?. This is my class and the package line gives this error.

package com.example.eventgyaam;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity  {

int counter;

Button add,sub;
TextView display;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    counter = 0;
    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);
    display = (Button) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter++;
            display.setText("Your total is "+counter);
        }
    });
    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter--;
            display.setText("Your total is "+counter);
        }
    });

}
}

解决方案

First, some introductory details.

There are levels of Android API and, at each level, they may introduce new functionality , as per here.

When you create an Android application, you specify the minimum level that your application will run on. This is done in the manifest with the uses-sdk stanza: see here for details.

So, if you use functionality that's only available at level 11, your code won't run on a level 8 API like you've specified.

You either have to use only what's available at your current minimum, or up the minimum.

As to why you appear to be getting the error on the package line, and for a call that doesn't seem to be in the source code you've shown us, I couldn't say. Since it's complaining about android.app.Activity#onCreateView as per your title, there may be an issue with the view R.layout.activity_main that's being inflated by setContentView. Or it may be an issue with the compatibility library itself.

However, rather than struggling too hard with this, I would suggest simply upping the minimum to API-11 and dropping the appcompat stuff altogether. As per here, only about 5% of Android users are still back on releases earlier than Android-3.0/API-11 (as of August 2015). That's only likely to get smaller as time goes by.