这是堆栈跟踪......您将在代码中看到几次“已删除”,因为我希望将我的包保密。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.*removed*.*removed*/com.*removed*.*removed*.List}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
at android.app.ActivityThread.access$1500(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at java.util.regex.Matcher.appendEvaluated(Matcher.java:135)
at java.util.regex.Matcher.appendReplacement(Matcher.java:115)
at java.util.regex.Matcher.replaceAll(Matcher.java:322)
at java.lang.String.replaceAll(String.java:1963)
at com.*removed*.*removed*.List.fillData(NoteList.java:100)
at com.*removed*.*removed*.List.onCreate(NoteList.java:31)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
... 11 more
......这是相关代码。当用户启动“列表” Activity 时,我希望他们看到基于他们从上一个列表中单击的项目的结果。主页是一个列表,因此“列表” Activity 基本上是一个查询,我在数据库中搜索图书列名称与他们在主页中单击的名称相匹配的所有条目。此方法用查询结果“填充”List ListView 。
@SuppressWarnings("deprecation")
private void fillData() {
String[] args = {"woo"};
for(int i = 0; i < args.length ; i++){
args[i] = args[i].replaceAll("woo", getBook());}
String[] projection = ............removed
Cursor cursor = managedQuery(Provider.CONTENT_URI, projection,
Database.COLUMN_BOOK + "=" + "?",
args, null);
String[] from = ............removed
int[] to = ...........removed
adapter = new SimpleCursorAdapter(List.this, R.layout.note_row, cursor, from,
to);
setListAdapter(adapter); }
static String book;
public static String getBook() {return book;}
public static void setBook(String book) {List.book = book;}
这里是主页的代码
public void onItemClick(AdapterView<?> adapter, View v,
int position, long id) {
final String[] choice = getResources().getStringArray(R.array.booknames);
String book = choice[position];
List.setBook(book);
Intent x = new Intent(Home.this, List.class);
Home.this.startActivity(x);
}
此应用程序已上市,大约 3% 的设备出现此空指针异常。所以这种方法并没有像我希望的那样100%有效。
我的第一个猜测是我没有正确初始化变量 args,这导致 ReplaceAll 方法在极少数情况下出现故障。 我的第二个猜测是我应该避免 getter 和 setter 并使用 bundle 。
如何重写此查询才能 100% 成功?
最佳答案
查看堆栈跟踪,在问题情况下 getBook()
似乎更有可能返回 null。
您使用静态变量将信息传递到 List Activity ,这通常不是一个非常好的主意。这就是传递额外内容( bundle )的目的。
在极少数情况下,您的所有应用程序可能会被终止并重新创建。任何额外内容都会再次传递给 Activity,但在这种情况下您的 book
仍为 null。
关于java - Matcher.appendEvaluated() 上出现 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14045354/