Manoj Majumdar Manoj Majumdar - 7 months ago 109
Java Question

Call requires API level 11(current min is 8) android.app.Activity#onCreateView

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);
}
});

}
}

Answer

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.