java - 我的应用程序总是崩溃,并且似乎找不到任何错误

标签 java android eclipse

当我启动我的应用程序时,它工作正常,但一旦我点击按钮将我带到MakeRap.activity,它就会崩溃。我检查了 MakeRap 的 Activity ,似乎没有错误。

这是日志猫所说的:

 06-07 11:57:09.173: I/Adreno200-EGLSUB(14785): <ConfigWindowMatch:2087>: Format      RGBA_8888.
 06-07 11:57:09.203: D/memalloc(14785): ion: Mapped buffer base:0x52531000 size:2088960    offset:0 fd:60
 06-07 11:57:09.203: D/OpenGLRenderer(14785): Enabling debug mode 0
 06-07 11:57:09.503: D/memalloc(14785): ion: Mapped buffer base:0x52ea4000 size:2088960 offset:0 fd:64
 06-07 11:57:10.955: D/AndroidRuntime(14785): Shutting down VM
 06-07 11:57:10.955: W/dalvikvm(14785): threadid=1: thread exiting with uncaught exception (group=0x40aaea08)
 06-07 11:57:10.975: E/AndroidRuntime(14785): FATAL EXCEPTION: main
 06-07 11:57:10.975: E/AndroidRuntime(14785): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.soloinc.meip/com.soloinc.meip.MakeRapActivity}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at android.app.ActivityThread.access$600(ActivityThread.java:139)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at android.os.Handler.dispatchMessage(Handler.java:99)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at android.os.Looper.loop(Looper.java:156)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at android.app.ActivityThread.main(ActivityThread.java:5045)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at java.lang.reflect.Method.invokeNative(Native Method)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at java.lang.reflect.Method.invoke(Method.java:511)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at dalvik.system.NativeStart.main(Native Method)
 06-07 11:57:10.975: E/AndroidRuntime(14785): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at com.soloinc.meip.MakeRapActivity.onCreate(MakeRapActivity.java:59)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at android.app.Activity.performCreate(Activity.java:4543)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
 06-07 11:57:10.975: E/AndroidRuntime(14785):   ... 11 more

这里MakeRapActivity:

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MakeRapActivity extends Activity 
{
    ListView instruments_list;
    protected List<String> instrument_title_list;
    protected List<String> instrument_audio_file_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_rap);
         // Get ListView object from xml
        instruments_list = (ListView) findViewById(R.id.instruments_list);

        //Getting list of all files From name_mapping.txt
        instrument_title_list = new ArrayList<String>();
        instrument_audio_file_list = new ArrayList<String>();

        InputStream inputStream = getResources().openRawResource(R.raw.name_mapping);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line = null;
        try 
        {
            line = reader.readLine();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        while (line != null) 
        {
            String[] temp = line.split(",");
            instrument_audio_file_list.add(temp[0]);
            instrument_title_list.add(temp[1]);

            try 
            {
                line = reader.readLine();
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
        try {
            reader.close();
            inputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Define a new Adapter
        // First parameter - Context
        // Second parameter - Layout for the row
        // Third parameter - ID of the TextView to which the data is written
        // Forth - the Array of data
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, android.R.id.text1, instrument_title_list);


        // Assign adapter to ListView
        instruments_list.setAdapter(adapter); 
        // ListView Item Click Listener
        instruments_list.setOnItemClickListener(new OnItemClickListener() 
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,long id) 
            {           
                   // ListView Clicked item value
                   String  instrument_title_clicked    = (String) instruments_list.getItemAtPosition(position);
                   Intent intent = new Intent(MakeRapActivity.this,RecordRap.class);
                   intent.putExtra("instrument_title",instrument_title_clicked);
                   intent.putExtra("instrument_file_name", get_Instrument_file_name(instrument_title_clicked));
                   startActivity(intent);
            }

         }); 
    }

    @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;
    }

    public String get_Instrument_file_name(String instrument_title)
    {
        int position = 0;
        for(int i = 0; i < instrument_title_list.size(); i++)
        {
            if(instrument_title_list.get(i).equals(instrument_title))
            {
                position = i;
            }
        }

        return instrument_audio_file_list.get(position);
    }

}

这是按钮的 Activity :

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {

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

    @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;
    }

    @Override
    public void onClick(View view) 
    {

        switch(view.getId())
        {

            case R.id.button1:
                //creating an intent to goto activity to add a new record
                Intent it=new Intent(MainActivity.this,MakeRapActivity.class);
                //starting new activity
                startActivity(it);
            break;

        }
    }
}

最佳答案

您的问题出在以下几行

String[] temp = line.split(",");
instrument_audio_file_list.add(temp[0]);
instrument_title_list.add(temp[1]);

发生的崩溃是索引越界异常,表明数组的大小为 1,并且您正在尝试访问数组中只有 1 个元素的第二个元素。当您使用 split 命令时,它将始终有第一个条目,但不保证有更多条目。问题是在某些时候您遇到一个不包含“,”的行变量,并且您会收到越界错误。

改成这样

String[] temp = line.split(",");
instrument_audio_file_list.add(temp[0]);

if (temp.length > 1) {
    instrument_title_list.add(temp[1]);
} else {
    instrument_title_list.add("");
}

这将确保您不会发生崩溃,并且如果没有逗号,则会插入一个空字符串。

这也可以通过修复输入文件以确保每一行至少有一个逗号来解决。取决于您想要的结果。

关于java - 我的应用程序总是崩溃,并且似乎找不到任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24099695/

相关文章:

java - Maven 从环境变量构建 : set settings. xml 路径

android - 哪个启动器正在运行?

java - addParentStack 中的 NameNotFoundException

java - src 文件夹导致 Eclipse 中的包声明错误

Eclipse IDE - Open Call Hierarchy 为空/损坏

java - 从底层 InputStream 解析时,Jackson 如何处理流结束读取(例如 -1、EOF)

java - Hyperledger Fabric V1.0

使用鼠标按钮的 Eclipse 后退/前进导航

java - 使用java邮件将图像嵌入到html电子邮件中

java - 如何从firestore数据库获取文档id