这是完整的logcat信息:
引起:java.lang.NullPointerException:尝试调用虚拟 方法'无效 android.widget.ListView.setAdapter(android.widget.ListAdapter)'上的一个 null对象引用 在cmput301.subbook.MainActivity.onCreate(MainActivity.java:49) 在android.app.Activity.performCreate(Activity.java:6999) 在android.app.Activity.performCreate(Activity.java:6990) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 在android.app.ActivityThread.-wrap11(未知来源:0) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1589) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这些是相关文件:
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cmput301.subbook.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit App"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/mainText"
android:background="#e6a1a1"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<!-- <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="144dp"
android:layout_marginTop="504dp"
android:text="Add New Subscription"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" /> -->
</android.support.constraint.ConstraintLayout>
MainActivity.java
package cmput301.subbook;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;
import android.widget.ArrayAdapter;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView text;
private List<String> subListVal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.mainText);
ArrayList<String> subListVal = new ArrayList<String>();
// subListVal = new ArrayList<String>();
subListVal.add("Netflix");
subListVal.add("Github");
subListVal.add("Spotify");
subListVal.add("Gym");
/* ArrayAdapter<String> adapter;
adapter = new ArrayAdapter <String>(this, R.layout.list_rows, R.id.listText, subListVal);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter); */
CustomAdapter adapter = new CustomAdapter(subListVal, this);
//handle listview and assign adapter
ListView lView = (ListView)findViewById(android.R.id.list);
lView.setAdapter(adapter);
/* Button add_btn = new Button(this);
add_btn.setText("Add New");
lView.addFooterView(add_btn); */
}
}
list_rows.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/listText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp"
android:textColor="#ffe600" />
<Button
android:id="@+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="Delete" />
<Button
android:id="@+id/edit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/delete_btn"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="Edit" />
</LinearLayout>
CustomerAdapter.java
package cmput301.subbook;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by thesh on 2/3/2018.
*/
public class CustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list= new ArrayList<String>();
private Context context;
public CustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
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_rows, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.listText);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
Button editBtn = (Button)view.findViewById(R.id.edit_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
editBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
notifyDataSetChanged();
}
});
return view;
}
}
从字面上看,我的应用程序正在运行,突然间我得到了这个错误,没有太大变化。我做的最后一次更改是尝试在列表的footer_view中添加一个按钮。我试着谷歌搜索logchat错误无济于事。
非常感谢帮助。感谢。
答案 0 :(得分:1)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at cmput301.subbook.MainActivity.onCreate(MainActivity.java:49)
这告诉您MainActivity
中的第49行在setAdapter()
来电中遇到了问题,您在null
的某个地方调用它。
这似乎是这一行:
lView.setAdapter(adapter);
这意味着lView
为null
。
您尝试在前一行中为lView
分配值:
ListView lView = (ListView)findViewById(android.R.id.list);
如果lView
为null
,则findViewById(android.R.id.list)
将返回null
。这意味着Android无法在布局中找到android:id
值为@android:id/list
的小部件。
这是因为您的ListView
具有不同的ID值:
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/mainText"
android:background="#e6a1a1"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
这些需要排队。所以,要么:
将android:id
中的<ListView>
更改为@android:id/list
,或
更改findViewById()
来电以查找R.id.listView
答案 1 :(得分:0)
更改此ListView lView = (ListView)findViewById(android.R.id.list);
到ListView lView = (ListView)findViewById(R.id.listView);
答案 2 :(得分:0)
更改ListView lView =(ListView)findViewById(android.R.id.list);使用ListView lView =(ListView)findViewById(R.id.listview);