在我开始解释发生了什么之前,我只是想说我已经阅读了类似的问题,但没有一个答案可以解决我的问题。当我使用上一个问题中讨论的方法时,我的应用程序仍然崩溃。
我有一个文本文件,其中每一行都有一个由百分号分隔的首字母缩写词,术语和定义。当用户搜索特定的首字母缩略词或术语时,它会按百分号分隔首字母缩写词,术语和定义,并在屏幕上显示数组的每个索引。我想只显示listview中的首字母缩略词,它始终是myArray [0]。我会弄清楚我自己的点击部分但是现在我只是遇到了显示首字母缩略词的问题。在上一个答案中,最后,用户说放:ArrayAdapter adapter = new ArrayAdapter(this,R.id.yourListView,lines)但这确实给我造成了错误,因为中间的东西应该是布局。这是我的代码:**注意:给我一个错误的代码片段被星星包围。
package com.redacted.CSRenA.acronymlookup;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Browse extends AppCompatActivity {
ListView listView= (ListView) findViewById(R.id.acrolist);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browse);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
try{
InputStream is = this.getResources().openRawResource(R.raw.acronyms2);
InputStreamReader inputreader = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
ArrayList<String> lines = new ArrayList<String>();
boolean hasNextLine =true;
String[] str;
while (hasNextLine){
String line = reader.readLine();
str = line.split("%", 5);
lines.add(str[0]);
hasNextLine = line != null;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,**R.id.acrolist**,lines);
listView.setAdapter(adapter);
is.close();
}
catch(java.io.FileNotFoundException e){
}catch(java.io.IOException e){
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
日志(向右滚动):
07-12 15:57:23.804 13752-13752/com.nesassociates.laurenanderson.acronymlookup E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nesassociates.laurenanderson.acronymlookup, PID: 13752
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nesassociates.laurenanderson.acronymlookup/com.nesassociates.laurenanderson.acronymlookup.Browse}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193)
at com.nesassociates.laurenanderson.acronymlookup.Browse.<init>(Browse.java:18)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:0)
您正在尝试取消引用listView时出现NullPointerException。
为了让findViewById正常工作,首先需要为您的Activity设置内容视图。
这一行:
ListView listView= (ListView) findViewById(R.id.acrolist);
必须在设置内容视图后完成。否则,它将始终返回null。
只需将其移动到您调用setContentView的下方的onCreate。