user3718258
user3718258

Reputation: 9

My app keeps crashing on me and cant seem to find any errors

When I start my application it works fine but as soon as I hit the button to take me to MakeRap.activity it crashes. I checked the activity of MakeRap and there seems to be no errors.

Here is what log cat says:

 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

Heres 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);
    }

}

Here is activity for the button:

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;

        }
    }
}

Upvotes: 0

Views: 84

Answers (1)

Bobbake4
Bobbake4

Reputation: 24847

Your problem is with the following lines

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

The crash that occurs is an index out of bounds exception and indicates that the array has a size of 1 and you are trying to access the second element in the array which only has 1 element. When you use a split command it will always have a first entry but is not guaranteed to have more. The problem is at some point you hit a line variable that does not contain a "," and you get an out of bounds error.

Change to something like this

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("");
}

That will make sure you do not have the crash and will insert an empty string if it doesn't have a comma.

This could also be solved by fixing the input file to make sure that every line has at least one comma in it. Depends on what outcome you are looking for.

Upvotes: 1

Related Questions