我通过Jsoup从网站上获取了大约60-70行表的数据,并使用它来创建带有团队成员子名的ListView。当我进行调试时,一切正常,但NullPointerException
却从无处出现。我认为我的代码是好的,这不应该发生。在错误描述中,我有关于跳过帧的声明。
错误:
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.adam.rozpiskimeczowe.offical.GetDataFromBeachPzps$1.onClick(GetDataFromBeachPzps.java:104)
W/System.err: at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24779)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:180)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:853)
I/Choreographer: Skipped 110 frames! The application may be doing too much work on its main thread.
和我的代码:
public class GetDataFromBeachPzps extends AppCompatActivity {
AlertDialog dialog;
Button btnGetData;
Document doc;
String teams;
//add
int quantityOfTeam=0;
ArrayList<String> names;
CustomAdapter customAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_data_from_beach_pzps);
//ADD LIST
final ListView list = findViewById(R.id.getDataListView);
list.setItemsCanFocus(true);
//GET DATA FROM WEBSITE
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
btnGetData = findViewById(R.id.getDataBtn);
btnGetData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Selecja turnieju za pomocą numeru na końcu URL
try {
Connection.Response res = Jsoup.connect("http://beach.pzps.pl/pl/site/login")
.data("LoginForm[email]", "adamandrys7@gmail.com", "LoginForm[password]", "66romek6")
.method(Connection.Method.POST)
.execute();
Map<String, String> loginCookies = res.cookies();
doc = Jsoup.connect("http://beach.pzps.pl/pl/tournament/793")
.cookies(loginCookies)
.get();
Element innerTable = doc.select(".items tbody").first();
Elements rows = innerTable.select("tr");
for(Element row:rows) {
Elements cells = row.select("td:eq(4)");
/*for (Element cell : cells) {*/
//MOŻLIWA ZMIANA DO TURNIEJU TO JEST ZROBIONE PO ZAKOŃCZENIU TURNIEJU
teams =cells.text();
/*}*/
quantityOfTeam++;
//convert
teams = teams.replaceAll("\\(.*?\\)","");
String team1 =teams.split("/")[0];
String team2 =teams.split("/")[1];
team1 = team1.substring(indexOfFirstCapitalLetter(team1));
team2 = team2.substring(indexOfFirstCapitalLetter(team2));
team1 = team1.split(" ")[0];
team2 = team2.split(" ")[0];
names.add(team1);
names.add(team2);
}
customAdapter = new CustomAdapter(getApplicationContext());
list.setAdapter(customAdapter);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
class CustomAdapter extends BaseAdapter {
private Context context;
private String[] rValues = new String[quantityOfTeam];
CustomAdapter(Context context) {
this.context = context;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getCount() {
return quantityOfTeam;
}
@Override
public String getItem(int position) {
return names.get(position) +"\n" + names.get(position + quantityOfTeam);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
for(int i =0;i<quantityOfTeam;i++){
rValues[i]="R"+(i+1);
}
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.brazylian_item_list, null, true);
holder.editText = convertView.findViewById(R.id.brazItemListEditTextName1);
holder.editText2 = convertView.findViewById(R.id.brazItemListEditTextName2);
holder.textView = convertView.findViewById(R.id.brazItemListtextViewR);
holder.editText.setText(names.get(position));
holder.editText2.setText(names.get(position+quantityOfTeam));
convertView.setTag(holder);
} else {
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(rValues[position]);
holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (!b) {
names.set(position, holder.editText.getText().toString()) ;
}
}
});
holder.editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (!b) {
names.set(position + quantityOfTeam, holder.editText2.getText().toString());
}
}
});
return convertView;
}
private class ViewHolder {
protected EditText editText;
protected EditText editText2;
protected TextView textView;
}
}
int indexOfFirstCapitalLetter(String str){
for(int i=0;i<str.length();i++){
if(Character.isUpperCase(str.charAt(i))) return i;
}
return -1;
}
}
我不知道该怎么办?我在上网,但没有发现任何有意义的东西:(
答案 0 :(得分:0)
您没有初始化names
。您需要添加names = new ArrayList<>();
作为声明的一部分,或者在代码中使用它之前。如果您只希望包含检索到的两个团队的名称,请在names.add(team1);
之前添加该语句。
答案 1 :(得分:0)
跳过的帧与您的问题无关。未初始化null object reference
导致的names
错误。
所以首先:
AlertDialog dialog;
Button btnGetData;
Document doc;
List<String> names = new ArrayList<>();
然后:
names.clear();
for(Element row:rows) {