2016-03-22 152 views
0

我有這樣的錯誤消息:錯誤無法啓動活動ComponentInfo

com.example.faizirrahmadi.eyeglauapp E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.faizirrahmadi.eyeglauapp, PID: 22114 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.faizirrahmadi.eyeglauapp/com.example.faizirrahmadi.eyeglauapp.PenyakitActivity}: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769) 
    at android.app.ActivityThread.access$900(ActivityThread.java:177) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5910) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
    at java.util.ArrayList.get(ArrayList.java:308) 
    at com.example.faizirrahmadi.eyeglauapp.PenyakitActivity.prepareListData(PenyakitActivity.java:111) 
    at com.example.faizirrahmadi.eyeglauapp.PenyakitActivity.onCreate(PenyakitActivity.java:69) 
    at android.app.Activity.performCreate(Activity.java:6178) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2648) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)  
    at android.app.ActivityThread.access$900(ActivityThread.java:177)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:135)  
    at android.app.ActivityThread.main(ActivityThread.java:5910)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at java.lang.reflect.Method.invoke(Method.java:372)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)  

這裏是我的代碼(Java類)

package com.example.faizirrahmadi.eyeglauapp; 

import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.text.TextUtils; 
import android.widget.ExpandableListView; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.HashMap; 
import java.util.List; 

import sistempakar.dbHelper; 

/** 
* Created by Faizir Rahmadi on 3/19/2016. 
*/ 
public class PenyakitActivity extends ActionBarActivity { 
ExpandableListAdapter listAdapter; 
ExpandableListView expListView; 
List<String> listDataHeader; 
HashMap<String, List<String>> listDataChild; 

Cursor cursor_gejala; 
dbHelper myDb; 
SQLiteDatabase sqliteDatabase; 
TextView nama_penyakit; 


public String bundleId; 
public String bundleKode; 
private String bundleNamaPenyakit; 
private String bundleDeskripsi; 
private String bundleGejala; 
private String bundlePenanganan; 
String a; 

List<String> bundleNamaGejala; 
List<String> namaGejalaList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_penyakit); 

    Bundle takeData = getIntent().getExtras(); 
    bundleId = takeData.getString("clickedId"); 
    bundleKode = takeData.getString("clickedKode").toLowerCase(); 
    bundleNamaPenyakit = takeData.getString("clickedNamaPenyakit"); 
    bundleDeskripsi = takeData.getString("clickedDeskripsi"); 
    bundleGejala = takeData.getString("clickedGejala"); 
    bundlePenanganan = takeData.getString("clickedPenanganan"); 

    //mengubah kode gejala menjadi nama gejala yang ditampilkan 
    bundleNamaGejala = Arrays.asList(bundleGejala.split("\\s*,\\s*")); 

    nama_penyakit = (TextView) findViewById(R.id.textView5); 
    nama_penyakit.setText(bundleNamaPenyakit); 
    nama_penyakit.setAllCaps(true); 


    expListView = (ExpandableListView) findViewById(R.id.expandableListView); 
    namagejala(bundleNamaGejala); 
    a = new String(TextUtils.join(", ", namaGejalaList)); 
    prepareListData(); 
    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); 
    expListView.setAdapter(listAdapter); 
    Toast.makeText(this, a, Toast.LENGTH_SHORT).show(); 
} 

//mengubah kode gejala dalam tabel RELASI menjadi nama gejala dari tabel GEJALA 
public List<String> namagejala(List<String> _nama) { 
    myDb = new dbHelper(this); 
    sqliteDatabase = myDb.getReadableDatabase(); 
    String where = ""; 
    namaGejalaList = new ArrayList<String>(); 
    for (int i = 0; i < _nama.size(); i++) { 
     where = where + dbHelper.COLUMN_ID_GEJALA + " IN('" + _nama.get(i).toString() + "')"; 
     if (i != (_nama.size() - 1)) { 
      where = where + " OR "; 
     } 
    } 
    cursor_gejala = sqliteDatabase.query(dbHelper.TABLE_GEJALA, new String[]{dbHelper.COLUMN_NM_GEJALA}, where, null, null, null, null); 
    while (cursor_gejala.moveToNext()) { 
     namaGejalaList.add(cursor_gejala.getString(cursor_gejala.getColumnIndex(dbHelper.COLUMN_NM_GEJALA))); 
    } 
    return namaGejalaList; 
} 

private void prepareListData() { 
    listDataHeader = new ArrayList<String>(); 
    listDataChild = new HashMap<String, List<String>>(); 

    listDataHeader.add("Gejala"); 


    List<String> deskripsi = new ArrayList<String>(); 
    deskripsi.add(bundleDeskripsi); 

    List<String> gejala = new ArrayList<String>(); 
    gejala.add(a); 

    List<String> penanganan = new ArrayList<String>(); 
    penanganan.add(bundlePenanganan); 

    listDataChild.put(listDataHeader.get(0), deskripsi); 
    listDataChild.put(listDataHeader.get(1), gejala); 
    listDataChild.put(listDataHeader.get(2), penanganan); 
} 

} 

我不知道這到底是怎麼happend ..我有檢查,總是有錯誤prepareListData();listDataChild.put(listDataHeader.get(1), gejala); 我一直在尋找所有的答案,沒有什麼可以幫助我 謝謝你的kindess幫助我,併爲將來解決這個問題

回答

0

列表索引從0開始,所以如果你想要得到第一個元素,你應該寫

listDataHeader.get(0) 

如果listDataHeader的大小爲1,當您嘗試使用索引1獲得元素,你將有一個例外。

0

在你的代碼放在一個項目在

listDataHeader.add("Gejala"); 

prepareListData()方法。這是在索引。而您試圖訪問listDataHeader列表,它是不可用的

listDataChild.put(listDataHeader.get(1), gejala); 

指數。所以,你會得到錯誤。

相關問題