新活动出现意外错误?

时间:2015-11-16 01:37:37

标签: java android android-activity

我正在Android中实施算法,并且在尝试调试时仍然遇到问题。当我按下按钮将我的数组传递给新活动以显示它并且我很难解密错误代码时,应用程序崩溃。谁能告诉我我能做些什么来解决这个问题?

主要活动

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
public final static String SORTING = "com.example.junkyardstar.SORTING";

int [] sortedInt;

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

public void insertionSort(View view)
{
    Intent intent = new Intent(this, InsertionSort.class);
    Bundle mBundle = new Bundle();
    EditText unsorted = (EditText) findViewById(R.id.editText);
    String unsortedString = unsorted.getText().toString();
    sortedInt = new int[unsortedString.length()];

    for(int i = 0; i < unsortedString.length(); i++)
    {
        sortedInt[i] = Integer.parseInt(unsortedString.substring(i, i+1));
    }

    for(int i = 1; i < sortedInt.length; i++)
    {
        int key = sortedInt[i];

        int temp = i - 1;
        while(i > 0 && sortedInt[temp] > key)
        {
            sortedInt[temp + 1] = sortedInt[temp];
            temp = temp - 1;
        }
        sortedInt[temp + 1] = key;
    }

    mBundle.putSerializable("sorted", sortedInt);
    intent.putExtras(mBundle);
    startActivity(intent);
}

}

插入排序

 public class InsertionSort extends AppCompatActivity {

TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    Bundle bundle = intent.getBundleExtra("sorted");
    int [] sortedList = (int []) bundle.getSerializable("sorted");

    String sortedListString = String.valueOf(sortedList);
    textView.setText(sortedListString);
}

@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_insertion_sort, 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);
}

public void onClick()
{
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}
}

Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.junkyardstar.insertionsort" >

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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=".InsertionSort"
        android:label="@string/title_activity_insertion_sort" >
    </activity>
</application>

2 个答案:

答案 0 :(得分:0)

您无法通过int[]通过序列化,int原始类型,因此将您的int array转换为String array 并通过,因为 String扩展Object ,可用于序列化。

答案 1 :(得分:-1)

   mBundle.putIntgerArray("sorted", sortedInt);
    intent.putExtras(mBundle);
    startActivity(intent);


Intent intent = getIntent();
Bundle bundle = intent.getExtras("sorted");
int [] sortedList = (int []) bundle.getIntgerArray("sorted");
try{
   String sortedListString = String.valueOf(sortedList);
}catch(Throwable e){
}