我正在制作Alarm应用程序,我有3个Tap,并且我想在片段中创建自定义列表视图,但是 运行项目时不起作用。 谁能告诉我为什么?
这是myAdapter.java
public class myAdapter extends BaseAdapter {
private Context c;
Fragment fr ;
private int res ;
private ArrayList<Times> test;
public myAdapter(Context c , int res , ArrayList<Times> test)
{
this.c = c;
this.res = res;
this.test = test;
}
@Override
public int getCount() {
return test.size();
}
@Override
public Times getItem(int position) {
return test.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null)
{
LayoutInflater.from(c).inflate(res , null , false);
}
TextView tv_name = v.findViewById(R.id.textView);
TextView tv_name2 = v.findViewById(R.id.textView2);
Times t = getItem(position);
tv_name.setText(t.getTime());
tv_name2.setText(t.getDay());
return v;
}
}
这是Times.java
public class Alarm extends Fragment {
public Alarm() {
// Required empty public constructor
}
ListView list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_alarm, container, false);;
list = v.findViewById(R.id.listview);
ArrayList<Times> names = new ArrayList<>();
names.add(new Times(1 , 3 , "Am"));
names.add(new Times(1 , 3 , "Am"));
names.add(new Times(1 , 3 , "Am"));
names.add(new Times(1 , 3 , "Am"));
names.add(new Times(1 , 3 , "Am"));
myAdapter adapter = new myAdapter(getContext(), R.layout.custom_list_alarm , names);
Log.e("hi", "onCreateView: " + getContext() );
list.setAdapter(adapter);
return v;
}
}
这是Times.java
public class Times {
int id ;
int time ;
String day ;
public Times(int id, int time, String day) {
this.id = id;
this.time = time;
this.day = day;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
}
---------崩溃开始 2019-12-07 23:16:10.065 6781-6781 / com.example.myalarm E / Android运行时:致命异常:主要 流程:com.example.myalarm,PID:6781 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.view.View android.view.View.findViewById(int)” 在com.example.myalarm.myAdapter.getView(myAdapter.java:51) 在android.widget.AbsListView.obtainView(AbsListView.java:2362) 在android.widget.ListView.makeAndAddView(ListView.java:1970) 在android.widget.ListView.fillDown(ListView.java:704) 在android.widget.ListView.fillFromTop(ListView.java:765) 在android.widget.ListView.layoutChildren(ListView.java:1744) 在android.widget.AbsListView.onLayout(AbsListView.java:2161) 在android.view.View.layout(View.java:17523) 在android.view.ViewGroup.layout(ViewGroup.java:5612) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 在android.widget.FrameLayout.onLayout(FrameLayout.java:261) 在android.view.View.layout(View.java:17523) 在android.view.ViewGroup.layout(ViewGroup.java:5612) 在androidx.viewpager.widget.ViewPager.onLayout(ViewPager.java:1775) 在android.view.View.layout(View.java:17523) 在android.view.ViewGroup.layout(ViewGroup.java:5612) 在com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142) 在com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41) 在com.google.android.material.appbar.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556) 在androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888) 在android.view.View.layout(View.java:17523) 在android.view.ViewGroup.layout(ViewGroup.java:5612) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 在android.widget.FrameLayout.onLayout(FrameLayout.java:261) 在android.view.View.layout(View.java:17523) 在android.view.ViewGroup.layout(ViewGroup.java:5612) 在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741) 在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585) 在android.widget.LinearLayout.onLayout(LinearLayout.java:1494) 在android.view.View.layout(View.java:17523) 在android.view.ViewGroup.layout(ViewGroup.java:5612) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 在android.widget.FrameLayout.onLayout(FrameLayout.java:261) 在android.view.View.layout(View.java:17523) 在android.view.ViewGroup.layout(ViewGroup.java:5612) 在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741) 在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585) 在android.widget.LinearLayout.onLayout(LinearLayout.java:1494) 在android.view.View.layout(View.java:17523) 在android.view.ViewGroup.layout(ViewGroup.java:5612) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) 在android.widget.FrameLayout.onLayout(FrameLayout.java:261) 在com.android.internal.policy.DecorView.onLayout(DecorView.java:724) 在android.view.View.layout(View.java:17523) 在android.view.ViewGroup.layout(ViewGroup.java:5612) 在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6301) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:871) 在android.view.Choreographer.doCallbacks(Choreographer.java:683) 在android.view.Choreographer.doFrame(Choreographer.java:619) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:857) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) 在java.lang.reflect.Method.invoke(本机方法)
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866) 2019-12-07 23:16:10.065 6781-6781 / com.example.myalarm E / AndroidRuntime:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 2019-12-07 23:16:10.067 6781-6806 / com.example.myalarm D / EGL_emulation:eglMakeCurrent:0xa7905120:ver 2 0(tinfo 0xa7903230)