2016-02-22 108 views
0

我是一個新手android編程,我寫了一個程序,它沒有錯誤地構建它,但是當我運行它時,它給了我錯誤消息,不幸的是我的應用程序已停止工作。這是我的程序。不幸的是我發現我的應用程序已經停止工作

listview_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<EditText 
    android:id="@+id/search" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:inputType="text" 
    android:hint="@string/hint_message"/> 

<ListView 
    android:id="@+id/listview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@+id/search"/> 

</RelativeLayout> 

listview_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<TextView 
    android:id="@+id/universitylabel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/universitylabel" /> 

<TextView 
    android:id="@+id/university" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/universitylabel" /> 

<TextView 
    android:id="@+id/Statelabel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/universitylabel" 
    android:text="@string/Statelabel" /> 

<TextView 
    android:id="@+id/State" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/university" 
    android:layout_toRightOf="@+id/Statelabel" /> 

<TextView 
    android:id="@+id/vicechancellorlabel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/Statelabel" 
    android:text="@string/vicechancellorlabel" /> 

<TextView 
    android:id="@+id/vicechancellor" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/State" 
    android:layout_toRightOf="@+id/vicechancellorlabel" /> 

<TextView 
    android:id="@+id/websitelabel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/vicechancellorlabel" 
    android:text="@string/websitelabel"/> 

<TextView 
    android:id="@+id/website" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/vicechancellor" 
    android:layout_toRightOf="@+id/websitelabel" /> 


</RelativeLayout> 

singleitemview.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<TextView 
    android:id="@+id/universitylabel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/universitylabel" /> 

<TextView 
    android:id="@+id/university" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/universitylabel" /> 

<TextView 
    android:id="@+id/Statelabel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/universitylabel" 
    android:text="@string/Statelabel" /> 

<TextView 
    android:id="@+id/State" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/university" 
    android:layout_toRightOf="@+id/Statelabel" /> 

<TextView 
    android:id="@+id/vicechancellorlabel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/Statelabel" 
    android:text="@string/vicechancellorlabel" /> 

<TextView 
    android:id="@+id/vicechancellor" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/State" 
    android:layout_toRightOf="@+id/vicechancellorlabel" /> 

<TextView 
    android:id="@+id/websitelabel" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/vicechancellorlabel" 
    android:text="@string/websitelabel" /> 

<TextView 
    android:id="@+id/website" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/vicechancellor" 
    android:layout_toRightOf="@+id/websitelabel" /> 


</RelativeLayout> 

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.spookyapp.findme" 
android:versionCode="1" 
android:versionName="1.0"> 

<uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="21" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name="com.spookyapp.findme.MainActivity" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name="com.spookyapp.findme.SingleItemView" 
     android:label="@string/app_name"> 
    </activity> 
</application> 

</manifest> 

MainActivity.java

package com.spookyapp.findme; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.text.Editable; 
    import android.text.TextWatcher; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.widget.EditText; 
    import android.widget.ListView; 

    import java.util.ArrayList; 
    import java.util.Locale; 

public class MainActivity extends Activity { 

// Declare Variables 
private ListView list; 
private ListViewAdapter adapter; 
private EditText editsearch; 
private String[] university; 
private String[] State; 
private String[] vicechancellor; 
private String[] website; 

private final ArrayList<UniversityDetails> arraylist = new ArrayList<>(); 

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

    // Generate sample data 
    university = new String[]{"Abubakar Tafawa Balewa University","Adeyemi University Education","Ahmadu Bello University","Alvan Ikoku University of Education","Bayero University","Federal University Gashua","Federal University of Education kano","Federal University of Education Zaria","Federal University of Health Science Otukpo","Federal University of Petroleum Resources Effurun","Federal University of Technology Akure","Federal University of Technology Minna","Federal University of Technology Owerri","Federal University Dutse","Federal University Dutsin-Ma","Federal University Kashere","Federal University Lafia","Federal University, Lokoja","Federal University, Ndufu-Alike","Federal University, Otuoke","Federal University, Oyo-Ekiti","Federal University, Wukari","Federal University,Birnin Kebbi","Federal University,Gusau","Michael Okpara Uni. of Agric., Umudike","Modibbo Adama University of Technology","National Open University of Nigeria","Nigeria Maritime University, Okerenkoko","Nigeria Police Academy Wudil","Nigerian Defence Academy","Nnamdi Azikiwe University, Awka","Obafemi Awolowo University,Ile-Ife","University of Abuja, Gwagwalada","University of Agriculture, Abeokuta","University of Agriculture, Makurdi","University of Benin","University of Calabar","University of Ibadan","University of Ilorin","University of Jos","University of Lagos","University of Maiduguri","University of Nigeria, Nsukka","University of Port-Harcourt","University of Uyo","Usumanu Danfodiyo University" 
    }; 

    State = new String[]{" Bauchi"," Ondo"," Zaria","Imo","Kano","Yobe","Kano","Zaria","Benue","Delta","Ondo","Niger","Imo","Jigawa","Kastina","Gombe","Nasarawa","Kogi","Ebonyi","Bayelsa","Ekiti","Taraba","Kebbi","Zamfara","Abia","Adamawa","Lagos","Delta","Kano","Kaduna","Anambra","Oyo","Abuja","Ogun","Benue","Edo","Cross,Rivers","Oyo","Kwara","Plateau","Lagos","Borno","Enugu","Rivers" ,"Akwa,Ibom" ,"Sokoto"}; 

    vicechancellor=new String[]{"Prof. Saminu Ibrahim","Prof. Richard King","Prof.Ibrahim Garba","Prof.Sadiq Abubakar" ,"Prof.Abubakar Adamu Rasheed mni","Prof.Shehu Abdulrahman","Prof.Victor Awonusi" ,"Prof.Ibrahim Kolo","Prof. Martins Aghaji","Prof. Akaehomen O.A Ibhadode","Prof. Adebiyi Gregory Daramola","Prof. Musibau Adewunmi Akanji","Prof. Chigozie C. Asiabaka","Prof. Jibril Dahiru Amin","Prof. James O. I. Ayatse","Prof. Mohammed Kabiru Farouk","Prof. Ekanem Ikpi Braide","Prof. Abdulmumini Rafindadi","Prof. Oye Ibidapo-Obe","Prof. Mobolaji E. Aluko","Prof. Isaac Asuzu","Prof. Geoffrey Okogbaa","Prof.Lawal Suleiman Bilbis","Prof. Ben Chuks Okeke","Prof. Hillary Odo Edoga","Prof. B. H. Usman","Prof. Vincent A. Tenebe","Prof. Ongoebi Maureen O. Etebu","Prof. Munzali Jibril","Prof. S.O Okeniyi","Prof. Joseph E. Ehaneku","Prof. Bamitale Omole","Prof. Michael Umale Adikwu","Prof. O. B. Oyewole","Prof. Emmanuel I. Kucha","Prof. Faraday Orumwense","Prof. James Epoke","Prof. Isaac Folorunso Adewole","Prof. Abdulganiyu Ambali","Prof. Hayward B. Mafuyai","Prof. Rahman A. Bello","Prof. Ibrahim A. Njodi","Prof. Benjamin C. Ozumba","Prof. Joseph Ajienka" ,"Prof. Comfort M. Ekpo","Prof. Riskuwa A. Shehu "}; 

    website=new String[]{"http://www.atbu.edu.ng","","http://www.abu.edu.ng","","http://www.buk.edu.ng","www.fugashua.edu.ng","","","http://www.fupre.edu.ng","http://www.futa.edu.ng","http://www.futminna.edu.ng","http://www.futo.edu.ng","http://www.fud.edu.ng","http://www.fudutsinma.edu.ng","www.fukashere.edu.ng","http://www.fulafia.edu.ng","http://www.fulokoja.edu.ng","http://www.funai.edu.ng","http://www.fuotuoke.edu.ng","http://www.fuoye.edu.ng","http://www.fuwukari.edu.ng","www.fubk.edu.ng","www.fugus.edu.ng","http://www.moua.org","http://www.futy.edu.ng","http://www.nou.edu.ng","","www.npf.gov.ng","www.nda.edu.ng","http://www.unizik.edu.ng","http://www.oauife.edu.ng","http://www.uniabuja.edu.ng","http://www.unaab.edu.ng","http://www.uam.edu.ng","http://www.uniben.edu.ng","http://www.unical.edu.ng","http://www.ui.edu.ng","http://www.unilorin.edu.ng","http://www.unijos.edu.ng","http://www.unilag.edu.ng","http://www.unimaid.edu.ng","http://www.unn.edu.ng","http://www.uniport.edu.ng","http://www.uniuyo.edu.ng","http://www.udusok.edu.ng"}; 

    // Locate the ListView in listview_main.xml 
    list = (ListView) findViewById(R.id.listview); 

    for (int i = 0; i < university.length; i++) 
    { 
     UniversityDetails ud = new UniversityDetails(university[i], State[i], 
       vicechancellor[i],website[i]); 

     // Binds all strings into an array 
     arraylist.add(ud); 
    } 

    // Pass results to ListViewAdapter Class 
    adapter = new ListViewAdapter(this, arraylist); 

    // Binds the Adapter to the ListView 
    list.setAdapter(adapter); 

    // Locate the EditText in listview_main.xml 
    editsearch = (EditText) findViewById(R.id.search); 

    // Capture Text in EditText 
    editsearch.addTextChangedListener(new TextWatcher(){ 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 
      String text = editsearch.getText().toString().toLowerCase(Locale.getDefault()); 
      adapter.filter(text); 
    } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, 
             int arg2, int arg3) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, 
            int arg3) { 
      // TODO Auto-generated method stub 
     } 
    }); 

    } 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
    } 
} 

ListViewAdapter.java

package com.spookyapp.findme; 

import android.content.Context; 
import android.content.Intent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 
import android.view.View.OnClickListener; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Locale; 

/** 
* Created by ${hudds} on ${21/02/2016}. 
*/ 
class ListViewAdapter extends BaseAdapter { 

// Declare Variables 
private final Context mContext; 
private final LayoutInflater inflater; 
private List<UniversityDetails> universityDetailsList = null; 
private final ArrayList<UniversityDetails> arraylist; 

public ListViewAdapter(Context context, List<UniversityDetails> universityDetailsList) { 
    mContext = context; 
    this.universityDetailsList = universityDetailsList; 
    inflater = LayoutInflater.from(mContext); 
    this.arraylist = new ArrayList<>(); 
    this.arraylist.addAll(universityDetailsList); 

} 

public class ViewHolder { 
    TextView university; 
    TextView State; 
    TextView vicechancellor; 
    TextView website; 
} 

@Override 
public int getCount(){ 
    return universityDetailsList.size(); 
} 

@Override 
public UniversityDetails getItem(int position){ 
    return universityDetailsList.get(position); 
} 

@Override 
public long getItemId(int position){ 
    return position; 
} 

public View getView(final int position, View view, ViewGroup parent){ 
    final ViewHolder holder; 
    if (view == null){ 

     holder = new ViewHolder(); 
     view = inflater.inflate(R.layout.listview_item, null); 

     // Locate the TextViews in listview_item.xml 
     holder.university = (TextView) view.findViewById(R.id.university); 
     holder.State = (TextView) view.findViewById(R.id.State); 
     holder.vicechancellor = (TextView) view.findViewById(R.id.vicechancellor); 
     holder.website = (TextView) view.findViewById(R.id.website); 
     view.setTag(holder); 
    }else { 
     holder = (ViewHolder) view.getTag(); 
    } 

    // Set the results into TextViews 
    holder.university.setText(universityDetailsList.get(position).getUniversity()); 
    holder.State.setText(universityDetailsList.get(position).getState()); 
    holder.vicechancellor.setText(universityDetailsList.get(position).getVicechancellor()); 
    holder.website.setText(universityDetailsList.get(position).getWebsite()); 

    // Listen for ListView Item Click 
    view.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0){ 
      // Send single item click data to SingleItemView Class 
      Intent intent = new Intent(mContext, SingleItemView.class); 
      // Pass all data university 
      intent.putExtra("university",(universityDetailsList.get(position).getUniversity())); 
      // Pass all data State 
      intent.putExtra("State",(universityDetailsList.get(position).getState())); 
      // Pass all data vicechancellor 
      intent.putExtra("vicechancellor",(universityDetailsList.get(position).getVicechancellor())); 
      // Pass all data website 
      intent.putExtra ("website",(universityDetailsList.get(position).getWebsite())); 
      // Pass all data flag 
      // Start SingleItemView Class 
      mContext.startActivity(intent); 
     } 


    }); 

    return view; 
} 

// Filter Class 
public void filter(String charText) { 
    charText = charText.toLowerCase(Locale.getDefault()); 
    universityDetailsList.clear(); 
    if (charText.length() == 0) { 
     universityDetailsList.addAll(arraylist); 
    } else { 

     for (UniversityDetails ud : arraylist) { 
      if (ud.getState().toLowerCase(Locale.getDefault()).contains(charText)) { 
       universityDetailsList.add(ud); 
      } 
     } 
    } 
    notifyDataSetChanged(); 

    } 

    } 

SingleViewItem.java

package com.spookyapp.findme; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 

/** 
* Created by hudds on 21/02/2016. 
*/ 
public class SingleItemView extends Activity { 

// Declare Variables 
private TextView txtuniversity; 
private TextView txtState; 
private TextView txtvicechancellor; 
private TextView txtwebsite; 
private String university; 
private String State; 
private String vicechancellor; 
private String website; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.singleitemview); 

    // Retrieve data from MainActivity on item click event 
    Intent i = getIntent(); 
    // Get the results of university 
    university = i.getStringExtra("university"); 
    // Get the results of State 
    State = i.getStringExtra("State"); 
    // Get the results of vice chancellor 
    vicechancellor = i.getStringExtra("vicechancellor"); 
    // Get the results of website 
    website = i.getStringExtra("website"); 

    // Locate the TextViews in singleitemview.xml 
    txtuniversity = (TextView) findViewById(R.id.university); 
    txtState = (TextView) findViewById(R.id.State); 
    txtvicechancellor = (TextView) findViewById(R.id.vicechancellor); 
    txtwebsite = (TextView) findViewById(R.id.website); 

    // Load the results into the TextViews 
    txtuniversity.setText(university); 
    txtState.setText(State); 
    txtvicechancellor.setText(vicechancellor); 
    txtwebsite.setText(website); 

    } 
} 

UniversityDetail.java

package com.spookyapp.findme; 

/** 
* Created by hudds on 21/02/2016. 
*/ 
public class UniversityDetails { 

private final String university; 
private final String State; 
private final String vicechancellor; 
private final String website; 

public UniversityDetails(String university, String State, String vicechancellor, String website) { 
    this.university = university; 
    this.State = State; 
    this.vicechancellor = vicechancellor; 
    this.website = website; 
} 
public String getUniversity() { 
    return this.university; 
} 
public String getState(){ 
    return this.State; 
} 

public String getVicechancellor(){ 
    return this.vicechancellor; 
} 

public String getWebsite(){ 
    return this.website; 
    } 
} 

日誌貓

2-22 09:24:21.254 3618-3618/? I/art: Not late-enabling -Xcheck:jni (already on) 
02-22 09:24:25.861 3618-3618/com.spookyapp.findme D/AndroidRuntime: Shutting down VM 
02-22 09:24:25.954 3618-3618/com.spookyapp.findme E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.spookyapp.findme, PID: 3618 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spookyapp.findme/com.spookyapp.findme.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=45; index=45 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
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:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=45; index=45 
at com.spookyapp.findme.MainActivity.onCreate(MainActivity.java:48) 
at android.app.Activity.performCreate(Activity.java:5937) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
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:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
02-22 09:24:31.691 3618-3618/com.spookyapp.findme I/Process: Sending signal. PID: 3618 SIG: 9 
+0

可能是你沒有在字符串aray ..所有相同的數據..! – Nils

+0

你可以嘗試打印所有數組的長度嗎? –

+0

檢查您正在使用的陣列中的元素數量。您正在循環「university」數組,因此請確保您用於獲取索引的其他數組的數組元素的數組元素的數量不能少於「university」數組。 – Mann

回答

2

堆棧跟蹤說,錯誤是在MainActivity第45行,我懷疑它會指向這個:

for (int i = 0; i < university.length; i++) 
{ 
    UniversityDetails ud = new UniversityDetails(university[i], State[i], 
      vicechancellor[i],website[i]); 

    // Binds all strings into an array 
    arraylist.add(ud); 
} 

很可能university,State,vicechancellorwebsite數組不包含相同數量的項目。導致你的數組越界。

+0

感謝它的工作 – Spooky

0

你的logcat說你有一個ARRY其在行少了一個元素,以便其出現ArrayIndexOutOfBoundsException異常45.

所以, 打印在日誌中的所有數組的長度,它會告訴你在數組你有較少的元素。

0

這是因爲java.lang.ArrayIndexOutOfBoundsException而發生的。

如果您注意,變量「website」有45個元素,而其他變量(「大學」,「州」和「副校長」)有46個元素。 因此,在「MainActivity」類的for循環中,當您使用以下代碼創建對象時,它會發出此異常。

UniversityDetails ud = new UniversityDetails(university[i], State[i], 
      vicechancellor[i],website[i]); 

要解決該問題,只需在「網站」變量中添加一個元素即可。

+0

感謝它的工作 – Spooky

相關問題