java - Unable to start activityon a null object reference -
this question has answer here:
- null pointer exception - findviewbyid() 9 answers
- what nullpointerexception, , how fix it? 12 answers
the problem is:
java.lang.runtimeexception: unable start activity componentinfo{com.example.android.miwok/com.example.android.miwok.report}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.listview.setadapter(android.widget.listadapter)' on null object reference
so there problem listview adapter in report.java, can't figure out how fix it. point create listview arrayadapter double data type , display screen.
activity_report.xml:
<?xml version="1.0" encoding="utf-8"?> <listview 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" tools:context="com.example.android.miwok.family_members_activity" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margintop="16dp" android:layout_marginleft="16dp" android:id="@+id/result"> </listview>
listpazymiai.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_margintop="8dp"> <textview android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/ltrez" android:gravity="center" android:layout_weight="1" tools:text="first mark" /> <textview android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/anglrez" android:layout_weight="1" android:gravity="center" tools:text="second mark" /> <textview android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/itrez" android:layout_weight="1" android:gravity="center" tools:text="third mark" /> </linearlayout>
report.java code
package com.example.android.miwok; import android.app.actionbar; import android.graphics.drawable.colordrawable; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.widget.arrayadapter; import android.widget.gridview; import android.widget.linearlayout; import android.widget.listview; import android.widget.textview; import com.example.android.miwok.reports; import java.lang.reflect.array; import java.util.arraylist; import com.example.android.miwok.reportocard; import static android.graphics.color.black; public class report extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_numbers_activity); arraylist<reportocard> pzm = new arraylist<reportocard>(); pzm.add(new reportocard(98.5, 89.6, 88.8)); pzm.add(new reportocard(88.7, 69.3, 58.2)); pzm.add(new reportocard(88.9, 59.4, 48.3)); pzm.add(new reportocard(92.8, 81.2, 87.4)); pzm.add(new reportocard(98.2, 89.1, 88.1)); pzm.add(new reportocard(23.5, 19.4, 88.7)); pzm.add(new reportocard(92.3, 59.3, 81.1)); pzm.add(new reportocard(56.5, 15.6, 18.8)); pzm.add(new reportocard(48.4, 95.6, 99.8)); pzm.add(new reportocard(45.7, 100, 10.8)); reports reportss = new reports(this, pzm); listview listelis =(listview)findviewbyid(r.id.result); listelis.setadapter(reportss); } }
reportocard.java:
package com.example.android.miwok; import android.text.layout; /** *created justas on 9/10/2017. */ public class reportocard { double manglu; double mlietuviu; double mit; public reportocard (double anglu, double lietuviu, double it) { manglu = anglu; mlietuviu = lietuviu; mit = it; } public double getit () { return mit; } public double getanglu () { return manglu; } public double getlietuviu () { return mlietuviu; } }
reports.java:
package com.example.android.miwok; import android.app.activity; 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 com.example.android.miwok.adaptoreditor; import com.example.android.miwok.r; import com.example.android.miwok.reportocard; import java.util.arraylist; public class reports extends arrayadapter<reportocard> { private static final string log_tag = reports.class.getsimplename(); public reports(activity context, arraylist<reportocard> pzm) { super(context, 0, pzm); } @override public view getview(int position, view convertview, viewgroup parent) { // check if existing view being reused, otherwise inflate view view linear = convertview; if(linear == null) { linear = layoutinflater.from(getcontext()).inflate(r.layout.listpazymiai, parent, false); } reportocard pzm = getitem(position); textview anglu = (textview) linear.findviewbyid(r.id.ltrez); anglu.settext(""+pzm.getanglu()); textview lietuviu = (textview) linear.findviewbyid(r.id.anglrez); lietuviu.settext(""+pzm.getlietuviu()); textview = (textview)linear.findviewbyid(r.id.itrez); it.settext(""+pzm.getit()); return linear; } }
your problem listview null. need focus on 2 lines of code in report.java code:
setcontentview(r.layout.activity_numbers_activity);
and
listview listelis =(listview)findviewbyid(r.id.result);
when you're calling findviewbyid
, implicitly using inflated activity_numbers_activity
view first line of code try , find listview. problem not have listview inside activity_numbers_activity.xml!
you need move listview inside r.layout.activity_numbers_activity
or call findviewbyid
view listview inside of it.
Comments
Post a Comment