当我想继续其他活动时,我有这个例外:
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)
清单:
<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>
主要活动:
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);
}
}
接收活动:
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("");
}
}
答案 0 :(得分:1)
您正在从初始化程序调用{{1}},这将无效。一般情况下,除非有指示,否则请勿在{{1}}返回后调用getResources()
上的方法。
在这种情况下,请替换:
Activity
使用:
super.onCreate()
并添加:
String [] advices = getResources().getStringArray(R.array.advices);
在String [] advices;
方法中advices = getResources().getStringArray(R.array.advices);
之后。