我有2或3个孩子的列表视图。我从数据库中取出孩子。我得到了孩子们,但是当我想调用updateOdgovor()方法时,我得到了空指针异常,这个方法是由updateOdgovor()中的nullpointer异常引起的。如果我在单击其中一个按钮后调用方法updateOdgovor(),则没有空指针异常 这是我的主要代码
public class PitanjeActivity extends Activity implements OnClickListener {
private Animator mCurrentAnimator;
private int mShortAnimationDuration;
private ArrayList<Pitanja> getAllPitanja;
private ArrayList<Pitanje_has_Slika> getAllImages;
private ArrayList<Odgovor> odgovorList;
private HashMap<Integer, List<Integer>> odgBrojPitanja;
private List<Integer> kliknuti;
private DBTools db = new DBTools(this);
private Intent intent;
int broj = 0;
private ImageView previous, informacije, odgovori, next, textSlike;
private TextView brojPitanja, kategorijaPitanja, textPitanja;
private String info;
private ListView listView;
boolean odgovoreno;
private ListView mDrawerList;
private RelativeLayout rightRL;
private DrawerLayout drawerLayout;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavigationItem> navDrawerItems;
private NavigationListAdapter adapte;
private Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_pitanje);
informacije = (ImageView) findViewById(R.id.informacijeButton);
odgovori = (ImageView) findViewById(R.id.odgovoriButton);
next = (ImageView) findViewById(R.id.nextButton);
textSlike = (ImageView) findViewById(R.id.imageSlikaImageView);
brojPitanja = (TextView) findViewById(R.id.brojPitanjaTextViewPitanjeActivity);
textPitanja = (TextView) findViewById(R.id.textPitanjaTextViewPitanjaActivity);
previous.setOnClickListener(this);
informacije.setOnClickListener(this);
odgovori.setOnClickListener(this);
next.setOnClickListener(this);
getAllPitanja = db
.getAllPitanja(intent.getStringExtra("id_kategorije"));
getAllImages = db.getAllPitanjaImages();
updateDisplay(broj);
}
@SuppressLint("SdCardPath")
private void updateDisplay(int z) {
odgovorList = db.getAllOdgovore(Integer.toString(getAllPitanja.get(z)
.getIdPitanja()));
Collections.shuffle(odgovorList);
PitanjaAdapter adapter = new PitanjaAdapter(this,
R.layout.pitanja_entry, odgovorList);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
Log.i("Pitanje je sljedece", getAllPitanja.get(z).getTextPitanja());
Log.i("Odgovori su sljedeci", odgovorList.get(0).getContents() + "; ;" + odgovorList.get(1).getContents());
updateOdgovor(z);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.previousButton:
odgovoreno = check();
if (odgovoreno) {
updateOdgovor(broj);
set();
} else {
broj--;
if (broj < 0) {
broj = getAllPitanja.size() - 1;
}
updateDisplay(broj);
if (checkList(broj)){
checList(broj);
} else {
Log.i("Vrijednosti ne postoji", "Ne postoji");
}
}
break;
case R.id.informacijeButton:
Intent intent = new Intent(getApplicationContext(),
InformacijeActivity.class);
intent.putExtra("Informacije", info);
intent.putExtra("naslov",
String.valueOf(getAllPitanja.get(broj).getTextPitanja()));
startActivity(intent);
break;
case R.id.odgovoriButton:
updateOdgovor(broj);
set();
break;
case R.id.nextButton:
odgovoreno = check();
if (odgovoreno) {
updateOdgovor(broj);
set();
} else {
broj++;
if (broj > (getAllPitanja.size() - 1)) {
broj = 0;
}
updateDisplay(broj);
if (checkList(broj)){
checList(broj);
} else {
Log.i("Vrijednosti ne postoji", "Ne postoji");
}
}
break;
}
}
private void updateOdgovor(int z) {
kliknuti = new ArrayList<>();
Log.i("Ulazim u updateOdgovor", "UpdateOdgovor");
for (int i = 0; i < odgovorList.size(); i++) {
Log.i("Ulazim u for petlju", "UpdateOdgovor");
Odgovor odgovor = odgovorList.get(i);
Log.i("Definiram odgovorr", "UpdateOdgovor");
if (Integer.parseInt(odgovor.getIs_correct()) == 1) {
Log.i("Ulazim u prvi if", "UpdateOdgovor");
listView.getChildAt(i)
.findViewById(R.id.code)
.setBackgroundResource(
R.drawable.tocan_odgovor_drawable);
Log.i("Izlazim iz prvog if-a", "UpdateOdgovor");
} else {
Log.i("Ulazim u prvi else", "UpdateOdgovor");
listView.getChildAt(i)
.findViewById(R.id.code)
.setBackgroundResource(
R.drawable.netocan_odgovor_drawable);
Log.i("Izlzim iz elsa", "UpdateOdgovor");
}
if (Integer.parseInt(odgovor.getIs_correct()) == 1
&& odgovor.isSelected()) {
Log.i("Ulazim u drugi if", "UpdateOdgovor");
listView.getChildAt(i).findViewById(R.id.checkBox1)
.setBackgroundResource(R.drawable.ikona_box2_tocno);
Log.i("Zavrsavam drugi if", "UpdateOdgovor");
} else if (Integer.parseInt(odgovor.getIs_correct()) != 1
&& odgovor.isSelected()) {
Log.i("Ulazim u else if", "UpdateOdgovor");
listView.getChildAt(i).findViewById(R.id.checkBox1)
.setBackgroundResource(R.drawable.ikona_box2_netocno);
Log.i("Zavrsavam else if", "UpdateOdgovor");
}
if (odgovor.isSelected()){
Log.i("Ulazim u treci if", "UpdateOdgovor");
kliknuti.add(odgovor.getId());
Log.i("Zavrsavam treci if", "UpdateOdgovor");
}
odgovoreno = false;
Log.i("Zavrsavam s forom", "UpdateOdgovor");
}
odgBrojPitanja.put(z, kliknuti);
Log.i("Pitanje na koje ste odgovorili", odgBrojPitanja.toString());
}
因为有人问我布局,我会在这里发布我的布局 我的主要布局:
<RelativeLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#878787" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="10dp"
android:background="#535351"
android:weightSum="100" >
<TextView
android:id="@+id/kategorijaTextViewPitanjeActivity"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="70"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#A7A7A5" />
<TextView
android:id="@+id/brojPitanjaTextViewPitanjeActivity"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="15"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFAD00" />
<ImageView
android:id="@+id/glavniIzbornikPitanjaImageView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="15"
android:clickable="true"
android:contentDescription="@string/glavni_izbornik_image_view"
android:onClick="onRight"
android:src="@drawable/ikona_glavni_izbornik_01" />
</LinearLayout>
<TextView
android:id="@+id/textPitanjaTextViewPitanjaActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<ImageView
android:id="@+id/expanded_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/imageview_pitanja_ispiti"
android:contentDescription="@string/autoskola_imageView"
android:focusable="true"
android:foregroundGravity="center"
android:visibility="gone" />
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/textPitanjaTextViewPitanjaActivity"
android:layout_alignStart="@+id/textPitanjaTextViewPitanjaActivity"
android:layout_below="@+id/textPitanjaTextViewPitanjaActivity"
android:layout_marginEnd="20dp"
android:layout_above="@+id/linearLayout4"
android:layout_marginRight="20dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageSlikaImageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="?android:attr/actionBarSize"
android:layout_weight="1"
android:contentDescription="@string/autoskola_imageView"
android:paddingEnd="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingStart="15dp"
android:visibility="gone" />
<ListView
android:id="@+id/listView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:divider="@null" >
</ListView>
</LinearLayout>
<RelativeLayout
android:id="@+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="@+id/previousButton"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginBottom="-5dp"
android:layout_marginEnd="-7dp"
android:layout_marginLeft="-7dp"
android:layout_marginRight="-7dp"
android:layout_marginStart="-7dp"
android:contentDescription="@string/autoskola_imageView"
android:src="@drawable/ikona_lijevo" />
<ImageView
android:id="@+id/nextButton"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="-5dp"
android:layout_marginEnd="-7dp"
android:layout_marginLeft="-7dp"
android:layout_marginRight="-7dp"
android:layout_marginStart="-7dp"
android:contentDescription="@string/autoskola_imageView"
android:src="@drawable/ikona_desno" />
<ImageView
android:id="@+id/informacijeButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="85dp"
android:layout_marginStart="85dp"
android:layout_toEndOf="@+id/previousButton"
android:layout_toRightOf="@+id/previousButton"
android:contentDescription="@string/autoskola_imageView"
android:paddingTop="8dp"
android:src="@drawable/ikona_informacije" />
<ImageView
android:id="@+id/odgovoriButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginEnd="65dp"
android:layout_marginLeft="54dp"
android:layout_marginRight="65dp"
android:layout_marginStart="54dp"
android:layout_toEndOf="@+id/informacijeButton"
android:layout_toLeftOf="@+id/nextButton"
android:layout_toRightOf="@+id/informacijeButton"
android:layout_toStartOf="@+id/nextButton"
android:contentDescription="@string/autoskola_imageView"
android:paddingTop="8dp"
android:src="@drawable/ikona_odgovori" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/whatYouWantInRightDrawerPitanja"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layout_marginTop="?android:attr/actionBarSize" >
<ListView
android:id="@+id/listaDesnoPitanja"
android:layout_width="200dp"
android:layout_height="match_parent"
android:background="#535351" />
</RelativeLayout>
这是我在listview中的儿童布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:layout_gravity="center"
android:button="@null"
android:background="@drawable/selector_check" />
<TextView
android:id="@+id/code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_drawable"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
</LinearLayout>
这是我的logcat
01-12 10:51:14.148: E/AndroidRuntime(1471): FATAL EXCEPTION: main 01-12 10:51:14.148: E/AndroidRuntime(1471): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prolink.autoskola/com.prolink.autoskola.PitanjeActivity}: java.lang.NullPointerException 01-12 10:51:14.148: E/AndroidRuntime(1471): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 01-12 10:51:14.148: E/AndroidRuntime(1471): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135) 01-12 10:51:14.148: E/AndroidRuntime(1471): at android.app.ActivityThread.access$700(ActivityThread.java:140) 01-12 10:51:14.148: E/AndroidRuntime(1471): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237) 01-12 10:51:14.148: E/AndroidRuntime(1471): at android.os.Handler.dispatchMessage(Handler.java:99) 01-12 10:51:14.148: E/AndroidRuntime(1471): at android.os.Looper.loop(Looper.java:137) 01-12 10:51:14.148: E/AndroidRuntime(1471): at android.app.ActivityThread.main(ActivityThread.java:4921) 01-12 10:51:14.148: E/AndroidRuntime(1471): at java.lang.reflect.Method.invokeNative(Native Method) 01-12 10:51:14.148: E/AndroidRuntime(1471): at java.lang.reflect.Method.invoke(Method.java:511) 01-12 10:51:14.148: E/AndroidRuntime(1471): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 01-12 10:51:14.148: E/AndroidRuntime(1471): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 01-12 10:51:14.148: E/AndroidRuntime(1471): at dalvik.system.NativeStart.main(Native Method) 01-12 10:51:14.148: E/AndroidRuntime(1471): Caused by: java.lang.NullPointerException 01-12 10:51:14.148: E/AndroidRuntime(1471): at com.prolink.autoskola.PitanjeActivity.updateOdgovor(PitanjeActivity.java:361) 01-12 10:51:14.148: E/AndroidRuntime(1471): at com.prolink.autoskola.PitanjeActivity.updateDisplay(PitanjeActivity.java:219) 01-12 10:51:14.148: E/AndroidRuntime(1471): at com.prolink.autoskola.PitanjeActivity.onCreate(PitanjeActivity.java:143) 01-12 10:51:14.148: E/AndroidRuntime(1471): at android.app.Activity.performCreate(Activity.java:5206) 01-12 10:51:14.148: E/AndroidRuntime(1471): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 01-12 10:51:14.148: E/AndroidRuntime(1471): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
问题是当我尝试在listview中更改孩子的背景时。问题不在arrayList
中答案 0 :(得分:0)
可能的错误:您的Hashmap
HashMap<Integer, List<Integer>> odgBrojPitanja
未初始化。如果这不是问题,请向我们展示logcat输出。但是HashMap肯定无效。
这里的问题是,你试图在listView中找到不可见的子节点,所以如果getChildAt()是一个隐藏的子节点,它将返回null。我不知道这是否解决了你的问题,但有一个技巧。尝试:
listView.getChildAt(i -listView.getFirstVisiblePosition()).findViewById(R.id.code).setBackgroundResource(
R.drawable.tocan_odgovor_drawable);
在任何地方执行此操作您想对listViews子进行操作。