我在我的应用中遇到错误“无法实例化活动ComponentInfo”。我已经在AndroidManifest.xml中声明了Activity,但我仍然得到错误。我不知道它是什么。有人可以帮忙吗?
感谢
错误:
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
我的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>
活动:
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;
}
}
}
}
答案 0 :(得分:13)
我们没有任何代码可以确认,但错误似乎表明您正试图在getString
的构造函数中使用NovaAtividadeActivity
。在Activity
调用之前不会创建onCreate
,因此您必须将代码从构造函数移动到该函数。
更新现在您已添加活动代码,我们可以看到您正在调用getString
来初始化成员DIAS
,这将在课程中发生'构造函数。而是将DIAS
的初始化移至onCreate
。