gpt4 book ai didi

java.lang.RuntimeException : Unable to instantiate activity ComponentInfo: java. lang.NullPointerException 异常

转载 作者:行者123 更新时间:2023-11-30 11:07:57 24 4
gpt4 key购买 nike

当我想继续其他 Activity 时,我遇到了这个异常:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ua.kbv.adviser/ua.kbv.adviser.AdviceWithText}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
at ua.kbv.adviser.AdviceWithText.<init>(AdviceWithText.java:18)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)

list :

<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
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=".AdviceWithText"/>
<activity android:name=".AdviceWithoutText"/>
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"
/>
</application>

主要 Activity :

public class MainActivity extends Activity{

private static long back_pressed;

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

public void clickOnGetAdvice(View view){


AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setTitle(R.string.builderTitle)
.setMessage(R.string.builderMessage)
.setCancelable(true)
.setIcon(R.mipmap.ic_action)
.setNeutralButton(R.string.neutralButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this, AdviceWithText.class);
startActivity(intent);
dialog.dismiss();
}
})
.setPositiveButton(R.string.positivebutton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this, AdviceWithoutText.class);
startActivity(intent);
dialog.dismiss();
}
});

AlertDialog alert = builder.create();
alert.show();
}

@Override
public void onBackPressed() {
if (back_pressed + 2000 > System.currentTimeMillis()) {
super.onBackPressed();
} else {
Toast.makeText(getBaseContext(), R.string.exitToast, Toast.LENGTH_SHORT).show();
}

back_pressed = System.currentTimeMillis();

}

public void clickOnAbout(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.builderAboutTitle)
.setMessage(R.string.builderMessageAbout)
.setCancelable(true)
.setIcon(R.mipmap.ic_action_info)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

AlertDialog alertDialog = builder.create();
alertDialog.show();

}

@Override
protected void onDestroy () {
super.onDestroy();
System.exit(0);
}
}

接收 Activity :

public class AdviceWithText extends Activity{

TextView answer;
private EditText question;
String [] advices = getResources().getStringArray(R.array.advices);

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

public void getAdvice(View view){
answer = (TextView) findViewById(R.id.answerText);
question = (EditText) findViewById(R.id.questionWith);
String text = question.getText().toString();
int rand = (int) Math.floor(Math.random()* advices.length);
if(text.equals("")){
Toast.makeText(getApplicationContext(),R.string.toastWith, Toast.LENGTH_LONG).show();
} else if(text.matches("[0-9]+")) {
Toast.makeText(getApplicationContext(),R.string.toastWithSecond, Toast.LENGTH_LONG).show();
} else {
answer.setText(advices[rand]);
}
}

@Override
public void onBackPressed() {
finish();
}

public void reset(View view){
question = (EditText) findViewById(R.id.questionWith);
question.setText("");
answer.setText("");

}

}

最佳答案

您正在从初始化器调用 getResources(),这将不起作用。通常,在 super.onCreate() 返回之前不要调用 Activity 上的方法,除非有指示这样做。

在这种情况下,替换:

String [] advices = getResources().getStringArray(R.array.advices);

与:

String [] advices;

并添加:

advices = getResources().getStringArray(R.array.advices);

onCreate() 方法中的 super.onCreate() 之后。

关于java.lang.RuntimeException : Unable to instantiate activity ComponentInfo: java. lang.NullPointerException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28685130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com