java.lang.Runtimeexception: Unable to instantiate Activity Componentinfo

Asked

Viewed 2,503 times

0

I am running the project on my mobile phone, however it presents 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' }
  • Why The nullpointerexception Tag?

1 answer

2


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

It doesn’t work. Before the setContentView, those elements even exist (were not "inflated").

If you do so, you must solve:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.