2015-11-16 89 views
1

我正在練習在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> 

+0

「AndroidManifest.xml」的內容是什麼? –

回答

0

你不能通過你的int[]通過序列化,int是原始類型,所以將您int arrayString array而過,因爲String延伸Object與序列化的工作。

-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){ 
} 
+0

這應該是mBundle.putIntArray(「sorted」,sortedInt)?我試過了,但我仍然有同樣的問題 –