gpt4 book ai didi

android - NullPointer 异常将 Intent 字符串保存为实例状态 Android

转载 作者:行者123 更新时间:2023-11-29 17:48:20 24 4
gpt4 key购买 nike

我遇到了一些 saveInstanceState 问题。

一个 Activity 通过 Intent 向另一个 Activity 发送一个字符串值,这非常有效。现在在第二个 Activity 中,我选择一个行项目以进入第三个 Activity ,这也有效。但是如果我使用操作栏后退按钮(到第二个 Activity ),我会得到一个 NullPointerException。

原因当然是无法加载第一个 Activity 的 Intent 字符串。因此,在第二个 Activity 中,我尝试将此 Intent 字符串值保存为实例状态,并再次在 oncreate 方法中获取此值。我谷歌搜索了很多,我尝试了很多。但是空指针不会消失。

如果有一点提示就太好了。

我的第二个activity代码:

    package de.cityknight.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class RouteChooseActivity extends ActionBarActivity implements OnItemClickListener {

String citySave;
String[] titelRoute;
String[] descRoute;
Integer[] bildRoute;

ListView listView;
List<RowItem> rowItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_choose);
Bundle b = getIntent().getExtras();
if (savedInstanceState != null) {
savedInstanceState.getString("stadt");
}
else {
b.getString("stadt");
}
TextView headingRoute = (TextView) findViewById(R.id.routeTitel);
headingRoute.setText(String.format(getResources().getString(R.string.route_text)) + " " + b.getString("stadt") + " aus:");
if (b.getString("stadt").equals("Passau")) {
titelRoute = new String[]{"Die Altstadt", "Universität", "Essen und Trinken"};
descRoute = new String[]{"Die Altstadt Passaus bietet viele Attraktion: Rathaus, Dom und die vielen kleinen Gassen.",
"Deutschlands schönster Campus.", "Kulinarische Köstlichkeiten in Passau"};
bildRoute = new Integer[]{R.drawable.augsburg, R.drawable.bamberg, R.drawable.passau};
} else if (b.getString("stadt").equals("Augsburg")) {
titelRoute = new String[]{"Augsburg1", "Augsburg2", "Augsburg3"};
descRoute = new String[]{"Bla bla", "Mehr Bla", "Blubb"};
bildRoute = new Integer[]{R.drawable.augsburg, R.drawable.bamberg, R.drawable.passau};
} else if (b.getString("stadt").equals("Bamberg")) {
titelRoute = new String[]{"Bamberg", "Bamberg2", "Bamberg3"};
descRoute = new String[]{"Bla bla", "Mehr Bla", "Blubb"};
bildRoute = new Integer[]{R.drawable.augsburg, R.drawable.bamberg, R.drawable.passau};
} else {
try {
TextView errorMessage = (TextView) findViewById(R.id.routeTitel);
errorMessage.setText(R.string.errorroute);
} catch (NullPointerException e) {
Context context = getApplicationContext();
CharSequence text = getResources().getString(R.string.errorroute);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}

}

try {
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titelRoute.length; i++) {
RowItem item = new RowItem(bildRoute[i], titelRoute[i], descRoute[i]);
rowItems.add(item);
}
Comparator<RowItem> sortRoutes = new Comparator<RowItem>() {
@Override
public int compare(RowItem rowItem1, RowItem rowItem2) {
return rowItem1.getTitle().compareTo(rowItem2.getTitle());
}
};
Collections.sort(rowItems, sortRoutes);
listView = (ListView) findViewById(R.id.listRoute);
CityListViewAdapter adapter = new CityListViewAdapter(this, R.layout.row_items, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
} catch (ArrayIndexOutOfBoundsException rowItems) {
Context context = getApplicationContext();
CharSequence text = getResources().getString(R.string.ressourcefailed);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent();
intent.setClass(RouteChooseActivity.this, RouteView.class);
RowItem item = (RowItem) parent.getItemAtPosition(position);
Bundle b = new Bundle();
b.putString("route", item.getTitle().toString());
intent.putExtras(b);
startActivity(intent);
}

protected void onSaveInstanceState(Bundle savedInstanceState) {
Bundle b = getIntent().getExtras();
b.getString("stadt");
savedInstanceState.putString("stadt", citySave);
super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
savedInstanceState.getString(citySave);
Log.i("debug", "saved data: " + citySave);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.route, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_stadt:
setContentView(R.layout.activity_stadt);
Intent stadt = new Intent(RouteChooseActivity.this, StadtActivity.class);
startActivity(stadt);
return true;
case R.id.action_help:
setContentView(R.layout.activity_help);
Intent help = new Intent(RouteChooseActivity.this, Help.class);
startActivity(help);
return true;
case R.id.action_exit:
moveTaskToBack(true);
System.exit(0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

最佳答案

  1. 这将引发 NullPointerException :

    savedInstanceState.getString(citySave);
    Log.i("debug", "saved data: " + citySave);

    应该是:

    citySave = savedInstanceState.getString("stadt");
    Log.i("debug", "saved data: " + citySave);
  2. onSaveInstanceState 中,您应该在向 savedInstanceState 添加字符串之前调用 super.onSaveInstanceState(savedInstanceState);(否则它将被删除出...)

  3. 整个 savedInstanceState 并不真正适合这个目的。 Android 可以在您进行第 3 个 Activity 时将其删除! (这取决于系统是否认为它需要内存并因此完全销毁第二个 Activity 及其 savedInstanceState)。

    做事的好方法是覆盖你的第三个 Activity 的 onBackPressed() 方法来触发一个 Intent 来启动你的第二个 Activity ,其值放在 Intent 的“额外”包中.

    通过这种方法,您的问题应该得到解决。

关于android - NullPointer 异常将 Intent 字符串保存为实例状态 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24820719/

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