Android:应用程序崩溃在意图

时间:2017-09-07 09:12:21

标签: java android android-intent

我想在活动之间传输大数组,但是当我单击MainActivity中的按钮时,当我使用putExtra在Intent中添加数组时,应用程序崩溃了。但是如果我没有向Intent添加数据,那么第二个活动(display_alpha)会正确显示,点击button1。当我调试时,Intent和Bundle变量在MainActivity中定义良好。您对崩溃的起因有所了解吗?

MainActivity

public class MainActivity extends AppCompatActivity {
private static final String HOME_ACTIVITY_TAG = MainActivity.class.getSimpleName();
private static final String TAG = "MyActivity";
public int[] indexAlpha = new int[27];
public String[] infoCompo = new String[3567];
public String[] refCompo = new String[3567];
public int clickAlpha=0; //clicked letter
public int clickPays=0; //clicked country
public Bundle b =new Bundle();
int i = 0;
private void showLog(String text) {
    Log.d(HOME_ACTIVITY_TAG, text);
}

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

    setInfoCompo(); //defines the int array indexAlpha
    setIndexAlpha(); //defines the String arrays infoCompo and refCompo

    b.putIntArray("INDEXALPHA", indexAlpha);
    b.putStringArray("INFOCOMPO", infoCompo);
    b.putStringArray("REFCOMPO", refCompo);
    b.putInt("CLICKALPHA", clickAlpha);
    b.putInt("CLICPAYS", clickPays);
    //Log.v(TAG,"b");
    //showLog("DEBUT3");
    // Locate the button in activity_main.xml
    //button = (Button) findViewById(R.id.button);
}

public void onButtonClick(View v)
{
    if(v.getId() == R.id.button) {
        Intent i = new Intent(MainActivity.this, Display_alpha.class);
        i.putExtras(b);
        startActivity(i);
    }
    else if(v.getId() == R.id.button1) {
        Intent i = new Intent(MainActivity.this, Display_alpha.class);
        startActivity(i);
    }
}

public void setIndexAlpha() {
    BufferedReader reader = null;
    String line;
    String[] RowData;
    //Log.v(TAG,"DEBUG...setIndexAlpha");
    //Toast.makeText(getBaseContext(),"DebSetIndexAlpha",Toast.LENGTH_LONG).show();
    try {
        reader = new BufferedReader(
                new InputStreamReader(getAssets().open("indexYouTube_indexAlpha.txt"), "US-ASCII"));
        for (int i = 0; i < 27; i++) {
            line = reader.readLine();
            indexAlpha[i] = Integer.parseInt(line);
            //Log.v(TAG,"DEBUG...indexAlpha "+i+":"+indexAlpha[i]);
        }
    } catch (IOException e) {
    }
}

public void setInfoCompo() {
    Log.v(TAG,"DEBUG...setInfoCompo");
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(
                new InputStreamReader(getAssets().open("indexYouTube_compo.txt"), "US-ASCII"));
        String line;
        int i=0;
        while ((line = reader.readLine()) != null) {
            //Log.v(TAG,"infoCompo "+line);
            String[] RowData = line.split("\\*");
            String nom = RowData[0];
            refCompo[i] = RowData[1];
            String dateNaissance = RowData[2];
            String dateDeces = RowData[3];
            String pays = RowData[3];
            infoCompo[i] = nom + ", born in " + dateNaissance + ", dead in " + dateDeces + " " + pays;
            //Log.v(TAG,infoCompo[i]);
            i++;
        }
    } catch (IOException e) {
        Log.v(TAG,"IOException");
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                //log the exception
            }
        }
    }
}
}

DisplayAlpha(第二项活动):

public class Display_alpha extends Activity {
private static final String HOME_ACTIVITY_TAG = Display_alpha.class.getSimpleName();
private static final String TAG = "Display_alpha";
GridView gridView;

static final String[] alpha = new String[] {
        "A", "B", "C", "D", "E",
        "F", "G", "H", "I", "J",
        "K", "L", "M", "N", "O",
        "P", "Q", "R", "S", "T",
        "U", "V", "W", "X", "Y", "Z"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_alpha);
    Toast.makeText(getBaseContext()," Display_alpha",Toast.LENGTH_LONG).show();
    gridView = (GridView) findViewById(R.id.grid_view_alpha);
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(Display_alpha.this,
            android.R.layout.simple_list_item_1, alpha);
    gridView.setAdapter(adapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            Bundle b= getIntent().getExtras();
            b.putInt("CLICKALPHA", position);
            Intent i = new Intent(Display_alpha.this, Display1.class);
            i.putExtras(b);
            startActivity(i);
        }
    });

  }
}

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码:

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("APPLE",value);  //value is a string.
startActivity(intent);

在第二项活动中:

Intent intent = getIntent();
String str_apple = intent.getStringExtra("APPLE");

如果您的值是int,则可以使用

intent.getIntExtra(..)   //according.