我试图简单地将一个ArrayList加载到ListView中,每个项目都有一个ImageButton,我已经创建了一个listview适配器类,但是我收到了这个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.braceytech.jack.spinthebottle, PID: 20713
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at android.widget.ListView.measureScrapChild(ListView.java:1312)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1398)
at android.widget.ListView.onMeasure(ListView.java:1303)
at android.view.View.measure(View.java:21045)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1117)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:642)
at android.view.View.measure(View.java:21045)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21045)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21045)
at android.support.v7.widget.AlertDialogLayout.tryOnMeasure(AlertDialogLayout.java:133)
at android.support.v7.widget.AlertDialogLayout.onMeasure(AlertDialogLayout.java:63)
at android.view.View.measure(View.java:21045)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
at android.view.View.measure(View.java:21045)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21045)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:21045)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:21045)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6459)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:849)
at android.view.View.measure(View.java:21045)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2576)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1608)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1886)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1515)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7091)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
at android.view.Choreographer.doCallbacks(Choreographer.java:702)
at android.view.Choreographer.doFrame(Choreographer.java:638)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
主要活动
package com.braceytech.jack.spinthebottle;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button mBtnPlay;
private TextView mTxtNameDisplay, mTxtTask, mTxtPlayer2;
private RanNumGen rng;
private ArrayList<String> tasks, namesBoy, namesGirl, namesAll;
private ToggleButton mTglSameSex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnPlay = (Button) findViewById(R.id.btnPlay);
mTxtNameDisplay = (TextView) findViewById(R.id.playerName);
mTxtTask = (TextView) findViewById(R.id.playerTask);
mTxtPlayer2 = (TextView) findViewById(R.id.playerName2);
mTglSameSex = (ToggleButton) findViewById(R.id.tglSameSex);
namesAll = new ArrayList<String>();
namesBoy = new ArrayList<String>();
namesGirl = new ArrayList<String>();
tasks = new ArrayList<String>();
mBtnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (namesGirl.size() > 0 && namesBoy.size() > 0 && namesAll.size() > 0 && tasks.size() > 0) {
if (!mTglSameSex.isChecked()) {
//Choose player 1
rng = new RanNumGen();
Integer i1 = rng.getRNG(0, namesBoy.size());
mTxtNameDisplay.setText(namesBoy.get(i1));
mTxtNameDisplay.setVisibility(View.VISIBLE);
//Choose player 2
rng = new RanNumGen();
Integer i3 = rng.getRNG(0, namesGirl.size());
mTxtPlayer2.setText(namesGirl.get(i3));
mTxtPlayer2.setVisibility(View.VISIBLE);
} else {
//Choose player 1
rng = new RanNumGen();
Integer i1 = rng.getRNG(0, namesAll.size());
mTxtNameDisplay.setText(namesAll.get(i1));
mTxtNameDisplay.setVisibility(View.VISIBLE);
ArrayList<String> namesAllTemp = (ArrayList<String>) namesAll.clone();
namesAllTemp.remove(i1);
//Choose player 2
rng = new RanNumGen();
Integer i2 = rng.getRNG(0, namesAllTemp.size());
mTxtPlayer2.setText(namesAllTemp.get(i2));
mTxtPlayer2.setVisibility(View.VISIBLE);
}
//Choose task
rng = new RanNumGen();
Integer i2 = rng.getRNG(0, tasks.size());
mTxtTask.setText(tasks.get(i2));
mTxtTask.setVisibility(View.VISIBLE);
} else if (namesAll.size() < 2) {
Toast.makeText(MainActivity.this, "Need to add more players", Toast.LENGTH_SHORT).show();
} else if (tasks.size() == 0) {
Toast.makeText(MainActivity.this, "Need to add more tasks", Toast.LENGTH_SHORT).show();
} else if (namesBoy.size() < 2) {
Toast.makeText(MainActivity.this, "Need more male players", Toast.LENGTH_SHORT).show();
} else if (namesGirl.size() < 2) {
Toast.makeText(MainActivity.this, "Need more female players", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Error, make sure you have players and tasks", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_player:
//Add player
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.pop_add_player, null);
final EditText mPlayerName = (EditText) mView.findViewById(R.id.etPlayerName);
final RadioGroup mRadioGroup = (RadioGroup) mView.findViewById(R.id.radGroup);
final RadioButton mMale = (RadioButton) mView.findViewById(R.id.radMale);
final RadioButton mFemale = (RadioButton) mView.findViewById(R.id.radFemale);
Button mBtnCancel = (Button) mView.findViewById(R.id.btnCancel);
Button mBtnAdd = (Button) mView.findViewById(R.id.btnAdd);
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
mBtnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mMale.isChecked() && !mFemale.isChecked() && !TextUtils.isEmpty(mPlayerName.getText())) {
//Male
namesBoy.add(mPlayerName.getText().toString());
namesAll.add(mPlayerName.getText().toString());
} else if (!mMale.isChecked() && mFemale.isChecked() && !TextUtils.isEmpty(mPlayerName.getText())) {
//Female
namesGirl.add(mPlayerName.getText().toString());
namesAll.add(mPlayerName.getText().toString());
} else {
Toast.makeText(MainActivity.this, "Enter a player name.", Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
});
mBtnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
return true;
case R.id.add_task:
//Add task
final AlertDialog.Builder mBuilder2 = new AlertDialog.Builder(MainActivity.this);
View mView2 = getLayoutInflater().inflate(R.layout.pop_add_task, null);
final EditText mTaskName = (EditText) mView2.findViewById(R.id.etTaskName);
final Button mBtnCancel2 = (Button) mView2.findViewById(R.id.btnCancel);
final Button mBtnAdd2 = (Button) mView2.findViewById(R.id.btnAdd);
mBuilder2.setView(mView2);
final AlertDialog dialog2 = mBuilder2.create();
dialog2.show();
mBtnAdd2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!TextUtils.isEmpty(mTaskName.getText())) {
tasks.add(mTaskName.getText().toString());
dialog2.dismiss();
} else {
Toast.makeText(MainActivity.this, "Enter a task name", Toast.LENGTH_SHORT).show();
}
}
});
mBtnCancel2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog2.dismiss();
}
});
return true;
case R.id.player_info:
if (tasks.size() > 0) {
//Open player list
final AlertDialog.Builder mBuilder3 = new AlertDialog.Builder(MainActivity.this);
View mView3 = getLayoutInflater().inflate(R.layout.pop_task_list, null);
final ListView mListTasks = (ListView) mView3.findViewById(R.id.mLstTasks);
mBuilder3.setView(mView3);
final AlertDialog dialog3 = mBuilder3.create();
dialog3.show();
LstViewAdapter adapter = new LstViewAdapter(tasks, this);
mListTasks.setAdapter(adapter);
} else {
Toast.makeText(this, "There are no tasks to be displayed", Toast.LENGTH_SHORT).show();
}
return true;
case R.id.task_info:
//Open task list
Toast.makeText(this, "Task list", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
列出视图适配器
package com.braceytech.jack.spinthebottle;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class LstViewAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
public LstViewAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_task_design, null);
}
TextView listItemText = (TextView) view.findViewById(R.id.lstTaskDesignText);
ImageButton listItemDelete = (ImageButton) view.findViewById(R.id.lstTaskDesignDelete);
listItemText.setText(list.get(position));
listItemDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);
notifyDataSetChanged();
}
});
return null;
}
}
如果您需要更多信息,请与我们联系。非常感谢任何帮助,我知道它与适配器有关,只是无法弄清楚是什么。感谢