java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

0

I'm running the project on my phone, but it throws the following exception error: java.lang.RuntimeException: Unable to Instantiate Activity ComponentInfo

MainActivity:

public class MainActivity extends AppCompatActivity {


Spinner dropdown = (Spinner)findViewById(R.id.livrosbiblia);
Button btlido = (Button)findViewById(R.id.btLido);
Button btLivrosLidos = (Button) findViewById(R.id.btLivrosLidos);

AlertDialog alert = new AlertDialog.Builder(this).create();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    String[] itens = new String[]{"Gênesis", "Exôdo", "Levítico", "Números", "Deuteronômio", "Josúe", "Juízes", "Rute", "1 Samuel",
            "2 Samuel", "1 Reis", "2 Reis", "1 Crônicas", "2 Crônicas", "Esdras", "Neemias", "Ester", "Jó", "Salmos", "Provérbios", "Eclesiastes",
            "Cantares", "Isaís", "Jeremias", "Lamentações", "Ezequiel", "Daniel", "Oséias", "Joel", "Obadias", "Jonas", "Miquéias", "Naum", "Habacuque",
            "Sofonias", "Ageu", "Zacarias", "Malaquias", "Mateus", "Marcos", "Lucas", "João", "Atos", "Romanos", "1 Coríntios", "2 Coríntios", "Gálatas",
            "Efésios", "Filipenses", "Colossenses", "1 Tessalonicenses", "2 Tessalonicenses", "1 Timóteo", "2 Timóteo", "Tito", "Filemom", "Hebreus",
            "Tiago", "1 Pedro", "2 Pedro", "1 João", "2 João", "3 João", "Judas", "Apocalipse"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, itens);
    dropdown.setAdapter(adapter);

AndroidManifest:

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".LivrosActivity" >
    </activity>
</application>

Build.Gradle:

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "com.estudosmobileoreia.leuabiblia"
    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.4.0' }
    
asked by anonymous 19.07.2016 / 04:31

1 answer

1

You are assigning the View s of your Activity to the variables ( findViewById ...) before onCreate (by placing them at the top of the class). Consequently, you are doing this before setContentView .

This does not work. Before setContentView , those elements do not even exist (they were not "inflated").

If you do this, you should resolve:

public class MainActivity extends AppCompatActivity {

    Spinner dropdown;
    Button btlido;
    Button btLivrosLidos;

    AlertDialog alert = new AlertDialog.Builder(this).create();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dropdown = (Spinner)findViewById(R.id.livrosbiblia);
        btlido = (Button)findViewById(R.id.btLido);
        btLivrosLidos = (Button) findViewById(R.id.btLivrosLidos);


        String[] itens = new String[]{"Gênesis", "Exôdo", "Levítico", "Números", "Deuteronômio", "Josúe", "Juízes", "Rute", "1 Samuel",
                "2 Samuel", "1 Reis", "2 Reis", "1 Crônicas", "2 Crônicas", "Esdras", "Neemias", "Ester", "Jó", "Salmos", "Provérbios", "Eclesiastes",
                "Cantares", "Isaís", "Jeremias", "Lamentações", "Ezequiel", "Daniel", "Oséias", "Joel", "Obadias", "Jonas", "Miquéias", "Naum", "Habacuque",
                "Sofonias", "Ageu", "Zacarias", "Malaquias", "Mateus", "Marcos", "Lucas", "João", "Atos", "Romanos", "1 Coríntios", "2 Coríntios", "Gálatas",
                "Efésios", "Filipenses", "Colossenses", "1 Tessalonicenses", "2 Tessalonicenses", "1 Timóteo", "2 Timóteo", "Tito", "Filemom", "Hebreus",
                "Tiago", "1 Pedro", "2 Pedro", "1 João", "2 João", "3 João", "Judas", "Apocalipse"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, itens);
        dropdown.setAdapter(adapter);
    
19.07.2016 / 06:28