java Android Studio。NullPointerException没有指向我的项目的任何链接
我使用jsoup从网站上获取一些数据,并对其进行过滤,然后将其显示在ListView上,但无论我对代码做什么,每次都会收到一个奇怪的错误。它与我的项目没有任何链接,而是与Android库和一些奇怪的东西(如zygote等)有链接。 这是我的代码:
package com.shapedhorizon.teymur.epiknovel;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.os.AsyncTask;
import 安卓.os.Bundle;
import 安卓.support.design.widget.FloatingActionButton;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.support.v7.widget.Toolbar;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.AdapterView;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.ListView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class SeriInfo extends AppCompatActivity {
ListView listView ;
Context ncontext;
String[] tts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seri_info);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//something
}
});
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
ncontext = this;
listView = findViewById(R.id.seri_bolum);
Log.i("Message", message);
AsyncInternet task = new AsyncInternet();
task.execute(message);
Log.i("OMG", "You got to be kidding with me");
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
return;
}
private class AsyncInternet extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... strings) {
Elements els;
Document doc= null;
Log.e("try",strings[0]);
try {
int ttsi = 0;
doc = Jsoup.connect(strings[0]).get();
els = doc.getElementsByAttributeValueStarting("href","https://www.epiknovel.com/seri");
Integer size = 0;
for (Element e: els
) {
if(!(e==null)) {
if(!e.parent().tagName("span").text().contains("Premium")){
size++;
Log.i("e.text",e.text());
}
}
}
Log.e("size", size.toString());
tts = new String[size];
for (Element e: els
) {
if(!(e==null)) {
if(!e.parent().tagName("span").text().contains("Premium")){
Log.i("e.text",e.text());
tts[ttsi] = e.text();
}
}
}
} catch (IOException c) {
c.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if(!(tts ==null)) {
Log.i("tts","IS NOT NULL");
ArrayAdapter adapter = new ArrayAdapter<String>(ncontext, R.layout.seri_view, tts);
Log.i("Adapter", "Check if we set up the adapter");
if(!(adapter==null)) {
listView.setAdapter(adapter);
Log.i("LW", "Weird that we passed that part");
}else{
Log.e("ADAPTER", "IS NULL");
}
}else{
Log.e("tts","IS NULL");
}
Log.i("OMG","Still?! What the hell is wrong with you?!");
return;
}
}
}
我的错误是:我
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.shapedhorizon.teymur.epiknovel, PID: 27846
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at 安卓.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
at 安卓.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at 安卓.widget.AbsListView.obtainView(AbsListView.java:2474)
at 安卓.widget.ListView.measureHeightOfChildren(ListView.java:1301)
at 安卓.widget.ListView.onMeasure(ListView.java:1203)
at 安卓.view.View.measure(View.java:19162)
at 安卓.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at 安卓.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at 安卓.view.View.measure(View.java:19162)
at 安卓.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6115)
at 安卓.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
at 安卓.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:91)
at 安卓.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1361)
at 安卓.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
at 安卓.view.View.measure(View.java:19162)
at 安卓.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6115)
at 安卓.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at 安卓.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at 安卓.view.View.measure(View.java:19162)
at 安卓.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6115)
at 安卓.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723)
at 安卓.widget.LinearLayout.measureVertical(LinearLayout.java:788)
at 安卓.widget.LinearLayout.onMeasure(LinearLayout.java:648)
at 安卓.view.View.measure(View.java:19162)
at 安卓.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6115)
at 安卓.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at 安卓.view.View.measure(View.java:19162)
at 安卓.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6115)
at 安卓.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723)
at 安卓.widget.LinearLayout.measureVertical(LinearLayout.java:788)
at 安卓.widget.LinearLayout.onMeasure(LinearLayout.java:648)
at 安卓.view.View.measure(View.java:19162)
at 安卓.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6115)
at 安卓.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at com.安卓.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2690)
at 安卓.view.View.measure(View.java:19162)
at 安卓.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2530)
at 安卓.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1490)
at 安卓.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
at 安卓.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1371)
at 安卓.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6773)
at 安卓.view.Choreographer$CallbackRecord.run(Choreographer.java:896)
at 安卓.view.Choreographer.doCallbacks(Choreographer.java:698)
at 安卓.view.Choreographer.doFrame(Choreographer.java:633)
at 安卓.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:882)
at 安卓.os.Handler.handleCallback(Handler.java:815)
at 安卓.os.Handler.dispatchMessage(Handler.java:104)
at 安卓.os.Looper.loop(Looper.java:207)
at 安卓.app.ActivityThread.main(ActivityThread.java:5811)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:768)
编辑1:我添加了
for (Object obj: tts
) {
if (obj.equals(null)){
Log.e("BIG ERROR", "There is null in tts");
}
}
在doInBackground函数中,我发现了一个新错误:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: com.shapedhorizon.teymur.epiknovel, PID: 30862
java.lang.RuntimeException: An error occurred while executing doInBackground()
at 安卓.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at 安卓.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Object.equals(java.lang.Object)' on a null object reference
at com.shapedhorizon.teymur.epiknovel.SeriInfo$AsyncInternet.doInBackground(SeriInfo.java:102)
at com.shapedhorizon.teymur.epiknovel.SeriInfo$AsyncInternet.doInBackground(SeriInfo.java:62)
at 安卓.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at 安卓.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818) `
和(serinfo.java:102)指if (obj.equals(null)){
编辑2:我将数组更改为列表。以下是在异步互联网功能中所做的更改:
private class AsyncInternet extends AsyncTask<String, Void, List<String>>{
@Override
protected List<String> doInBackground(String... strings) {
Elements els;
Document doc= null;
List<String> adapterData = new ArrayList<>();
Log.e("try",strings[0]);
try {
int ttsi = 0;
doc = Jsoup.connect(strings[0]).get();
els = doc.getElementsByAttributeValueStarting("href","https://www.epiknovel.com/seri");
for (Element e: els
) {
if(!e.parent().tagName("span").text().contains("Premium")){
if(e.text() != null) {
if(e.parent().parent().html().contains("td")) {
Log.i("e.text", e.text());
adapterData.add(e.text());
}
}
}
}
} catch (IOException c) {
c.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<String> result) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ncontext, R.layout.seri_view, result);
Log.i("Adapter", "set up the adapter");
listView.setAdapter(adapter);
Log.i("LW", "Weird that we passed that part");
}
}
}
现在我得到了这样的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.shapedhorizon.teymur.epiknovel, PID: 5914
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at 安卓.widget.ArrayAdapter.getCount(ArrayAdapter.java:337)
at 安卓.widget.ListView.setAdapter(ListView.java:491)
at com.shapedhorizon.teymur.epiknovel.SeriInfo$AsyncInternet.onPostExecute(SeriInfo.java:97)
at com.shapedhorizon.teymur.epiknovel.SeriInfo$AsyncInternet.onPostExecute(SeriInfo.java:63)
at 安卓.os.AsyncTask.finish(AsyncTask.java:651)
at 安卓.os.AsyncTask.-wrap1(AsyncTask.java)
at 安卓.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at 安卓.os.Handler.dispatchMessage(Handler.java:102)
at 安卓.os.Looper.loop(Looper.java:148)
at 安卓.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:616)
(serinfo.java:97)是listView.setAdapter(adapter);
# 1 楼答案
首先
== null
是一个正确的空检查。你可以用!= null
来否定它但无论如何,我建议不要使用数组,而是为适配器使用列表
从
然后
现在在onPostExecute中有了一个非空的列表对象。如果在这里初始化,适配器就不是空的
您还应该真正地将适配器移动到活动中的一个字段,并在onCreate中用一个空列表初始化它