我正在尝试制作一个从 .txt 文件读取到哈希图的应用程序,然后在单击按钮时打印一个随机哈希图条目。但是我收到了像“FATAL EXCEPTION:main”这样的错误,我可以找出问题所在。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
register r = new register();
final HashMap<String, quote> quotes = r.getMap();
final Random rand = new Random();
final Button newQuote = (Button) findViewById(R.id.button1);
final Button clear = (Button) findViewById(R.id.button2);
final TextView text = (TextView) findViewById(R.id.Text1);
newQuote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i = rand.nextInt(4);
String kode = ""+i;
// TODO Auto-generated method stub
text.setText(quotes.get(kode).sitat);
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text.setText("Sitat");
// 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.main, menu);
return true;
}
}
class quote{
String kode;
String sitat;
String forfatter;
quote(String kode, String sitat, String forfatter){
this.kode = kode;
this.sitat = sitat;
this.forfatter = forfatter;
}
public String hentSitat(){
return sitat;
}
public String hentForfatter(){
return forfatter;
}
}
class register{
HashMap<String, quote> quotes = new HashMap<String, quote>();
register(){
Scanner sc = new Scanner("sitater");
while(sc.hasNext()){
String linje = sc.nextLine();
String[] split = linje.split("\t");
String kode = split[0];
String sitat = split[1];
String forfatter = split[2];
quote q = new quote(kode, sitat, forfatter);
quotes.put(kode, q);
}
}
HashMap<String, quote> getMap(){
return quotes;
}
}
日志猫:
05-15 16:25:12.906: D/AndroidRuntime(4135): 关闭 VM 05-15 16:25:12.906: W/dalvikvm(4135): threadid=1: 线程退出未捕获异常 (group=0x40a71930) 05 -15 16:25:12.927: E/AndroidRuntime(4135): 致命异常: main 05-15 16:25:12.927: E/AndroidRuntime(4135): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.MorHam .qoutes/com.MorHam.qoutes.MainActivity}:java.lang.ArrayIndexOutOfBoundsException:长度=1;index=1 05-15 16:25:12.927: E/AndroidRuntime(4135): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 05-15 16:25:12.927: E/AndroidRuntime(4135):在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 05-15 16:25:12.927: E/AndroidRuntime(4135): 在 android.app.ActivityThread.access$600(ActivityThread.java:141) 05-15 16:25:12.927: 25:12.927:E/AndroidRuntime(4135):在 dalvik.system.NativeStart.main(本机方法)05-15 16:25:12.927:E/AndroidRuntime(4135):引起:java.lang.ArrayIndexOutOfBoundsException:长度= 1个;index=1 05-15 16:25:12.927: E/AndroidRuntime(4135): at com.MorHam.qoutes.register.(MainActivity.java:120) 05-15 16:25:12.927: E/AndroidRuntime(4135) : 在 com.MorHam.qoutes.MainActivity.onCreate(MainActivity.java:19) 05-15 16:25:12.927: E/AndroidRuntime(4135): 在 android.app.Activity.performCreate(Activity.java:5104) 05 -15 16:25:12.927: E/AndroidRuntime(4135): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 05-15 16:25:12.927: E/AndroidRuntime(4135): 在 android.app .ActivityThread.performLaunchActivity(ActivityThread.java:2144) 05-15 16:25:12.927: E/AndroidRuntime(4135): ... 11 更多 05-15 16:25:12.998: