我收到错误NullPointerException我已经尝试了方法提及
Count Total Number of List Items in a ListView
但没有运气我想获得列表中的项目
这就是我正在做的事情
int count = listView.getAdapter().getCount();
自定义适配器代码
package com.example.wildstone.cv_maker;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by wildstone on 08/02/2018.
*/
public class Custom_Adapter_for_Language extends ArrayAdapter<Language_data_comes_here>
{
Context context ;;
public Custom_Adapter_for_Language(Context context, ArrayList<Language_data_comes_here> array) {
super(context,0 , array);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View v = convertView ;
if (v == null){
v = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_view ,parent ,false);
}
Language_data_comes_here language_data_comes_here = getItem(position);
ImageView imageView = (ImageView) v.findViewById(R.id.custom_image_display_list_item);
TextView textView = (TextView) v.findViewById(R.id.custom_text_display_list_item);
textView.setText(language_data_comes_here.getLanguage());
imageView.setImageResource(R.mipmap.language);
return v ;
}
}
活动主体
package com.example.wildstone.cv_maker;
import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class Language_Activity extends AppCompatActivity {
ListView listView ;
FloatingActionButton floatingActionButton ;
Custom_Adapter_for_Language arrayAdapter ;
ArrayList<Language_data_comes_here> arr ;
Toolbar toolbar ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_language_);
floatingActionButton = (FloatingActionButton) findViewById(R.id.floating_action_button);
floatingActionButton.setImageResource(R.mipmap.plus);
floatingActionButton.setBackgroundColor(Color.WHITE);
listView = (ListView) findViewById(R.id.list_view);
arr = new ArrayList<>();
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(Language_Activity.this);
LayoutInflater layoutInflater = getLayoutInflater() ;
View view = layoutInflater.inflate(R.layout.enter_language , null);
final EditText language = (EditText) view.findViewById(R.id.language_entered);
toolbar = (Toolbar) view.findViewById(R.id.toolbar);
toolbar.setTitle(" Add Language !");
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setLogo(R.mipmap.language);
Button add_language = (Button) view.findViewById(R.id.enter_language);
add_language.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arr.add(new Language_data_comes_here( R.mipmap.language , language.getText().toString()));
arrayAdapter = new Custom_Adapter_for_Language(Language_Activity.this , arr);
listView.setAdapter(arrayAdapter);
builder.setCancelable(true);
language.setText("");
}
});
builder.setCancelable(true);
builder.setView(view);
AlertDialog alertDialog = builder.create() ;
alertDialog.show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
arr.remove(position);
arrayAdapter.notifyDataSetChanged();
return false;
}
}) ;
getListCount();
}
public void getListCount () {
int count = listView.getAdapter().getCount();
Button bb = (Button) findViewById(R.id.b);
switch(count){
case 1 :
bb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Language_Activity.this, "There is 1 item", Toast.LENGTH_SHORT).show();
}
});
break ;
case 2 :
bb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Language_Activity.this, "There is 2 items", Toast.LENGTH_SHORT).show();
}
});
break ;
case 3 :
bb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Language_Activity.this, "There is 3 items", Toast.LENGTH_SHORT).show();
}
});
case 4 :
bb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Language_Activity.this, "There is 4 items", Toast.LENGTH_SHORT).show();
}
});
break ;
case 5 :
bb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Language_Activity.this, "There is 5 items", Toast.LENGTH_SHORT).show();
}
});
break ;
case 6 :
bb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Language_Activity.this, "There is 6 items", Toast.LENGTH_SHORT).show();
}
});
break ;
default:
Toast.makeText(Language_Activity.this, "There list is empty", Toast.LENGTH_SHORT).show();
break ;
}
}
}
logcat的
02-14 22:03:13.736 25664-25664/com.example.wildstone.cv_maker D/AndroidRuntime: Shutting down VM
02-14 22:03:13.739 25664-25664/com.example.wildstone.cv_maker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.wildstone.cv_maker, PID: 25664
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wildstone.cv_maker/com.example.wildstone.cv_maker.Language_Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.wildstone.cv_maker.Custom_Adapter_for_Language.getCount()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.wildstone.cv_maker.Custom_Adapter_for_Language.getCount()' on a null object reference
at com.example.wildstone.cv_maker.Language_Activity.getListCount(Language_Activity.java:117)
at com.example.wildstone.cv_maker.Language_Activity.onCreate(Language_Activity.java:106)
at android.app.Activity.performCreate(Activity.java:6955)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
答案 0 :(得分:0)
在单击add_language按钮之前,数组适配器为null。但是getListCount()方法是从onCreate调用的。
您可能需要在getListCount方法之前更改onCreate中的initialize arrayAdapter。以下行应位于onClickListener之外。
arrayAdapter = new Custom_Adapter_for_Language(Language_Activity.this , arr);
listView.setAdapter(arrayAdapter);
像这样的onCreate方法: -
arr = new ArrayList<>();
arrayAdapter = new Custom_Adapter_for_Language(Language_Activity.this , arr);
listView.setAdapter(arrayAdapter);
在点击监听器中,您只需要通知数据集
add_language.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
.
.
arrayAdapter.notifyDataSetChanged();
}
答案 1 :(得分:0)
add_language.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arr.add(new Language_data_comes_here( R.mipmap.language , language.getText().toString()));
arrayAdapter = new Custom_Adapter_for_Language(Language_Activity.this , arr);
listView.setAdapter(arrayAdapter);
builder.setCancelable(true);
language.setText("");
}
});
每次单击按钮时,都会创建一个新适配器。如果用户在尝试计算适配器中的元素数量之前没有单击按钮,您将获得NPE。
要解决此问题,您应该在onCreate()
中创建一个适配器。然后,当您向ArrayList
添加元素时,只需致电notifyDataSetChanged()
。