Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to instantiate activity ComponentInfo... java.lang.NullPointerException

I'm having the error "Unable to instantiate activity ComponentInfo" in my app. I had already declared the Activity in AndroidManifest.xml, but I still get the error. I don't know what it is. Does anyone can help?

thanks

The error:

    FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.com.controladoratividades/br.com.controladoratividades.activities.NovaAtividadeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:80)
at android.content.Context.getString(Context.java:182)
at br.com.controladoratividades.activities.NovaAtividadeActivity.<init>(NovaAtividadeActivity.java:15)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1429)
at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
... 11 more

My AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.controladoratividades"
    android:versionCode="1"
    android:versionName="1.0" >



    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".activities.ControladorAtividadesActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name=".activities.NovaAtividadeActivity"
            android:label="@string/nova_atividade" >            
        </activity>

           <activity
            android:name=".activities.PlanejadorActivity"
            android:label="@string/novo_plano" >            
        </activity>         


    </application>

    <uses-sdk android:minSdkVersion="7" />

</manifest>

The activity:

package br.com.controladoratividades.activities;

import br.com.controladoratividades.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class NovaAtividadeActivity extends Activity {

    private String[] DIAS = new String[] {getString(R.string.domingo), 
            getString(R.string.segunda),
            getString(R.string.terca),
            getString(R.string.quarta),
            getString(R.string.quinta),
            getString(R.string.sexta),
            getString(R.string.sabado)};
    protected boolean[] DIAS_SELECIONADOS =  new boolean[ DIAS.length ];

    static final int DIAS_DIALOG_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.nova_atividade);

        Button btAlterarDias = (Button)findViewById(R.id.btSalvarPlano);
        btAlterarDias.setOnClickListener(new AlterarDiasClickHandler());

        for (int i = 1;  i < DIAS_SELECIONADOS.length; i++) {
            DIAS_SELECIONADOS[i] = true;
        }
    }

    public class AlterarDiasClickHandler implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            showDialog(DIAS_DIALOG_ID);

        }       
    }

    @Override
    protected Dialog onCreateDialog( int id ) 
    {
        return 
        new AlertDialog.Builder( this )
            .setTitle( "Planets" )
            .setMultiChoiceItems( 
                    DIAS,
                    DIAS_SELECIONADOS, 
                    new DialogSelectionClickHandler() )
            .setPositiveButton( "OK", new DialogButtonClickHandler() )
            .create();
    }

    public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener
    {
        public void onClick( DialogInterface dialog, int clicked, boolean selected )
        {
            Log.i( "ME", DIAS[ clicked ] + " selected: " + selected );
        }
    }

    public class DialogButtonClickHandler implements DialogInterface.OnClickListener
    {
        public void onClick( DialogInterface dialog, int clicked )
        {
            switch( clicked )
            {
                case DialogInterface.BUTTON_POSITIVE:
                    //printSelectedPlanets();
                    break;
            }
        }
    }
}
like image 795
VanessaBR Avatar asked Dec 13 '22 01:12

VanessaBR


1 Answers

We don't have any code to confirm, but the error seems to suggest that you are trying to use getString from within NovaAtividadeActivity's constructor. An Activity is not created until the onCreate call, so you would have to move your code from the constructor to that function.

Update: Now that you added the code for the activity, we can see that you are calling getString to initialize the member DIAS, which will happen at the class' constructor. Instead, move the initialization of DIAS to onCreate.

like image 71
K-ballo Avatar answered Jan 04 '23 23:01

K-ballo