gpt4 book ai didi

java - 新 Activity 出现意外错误?

转载 作者:行者123 更新时间:2023-12-01 06:11:33 26 4
gpt4 key购买 nike

我正在练习在 Android 中实现算法,但在尝试调试时一直遇到问题。当我按下按钮将数组传递给新 Activity 以显示它时,应用程序崩溃,并且我很难破译错误代码。谁能告诉我如何解决这个问题?

主要 Activity

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 list

<?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>

最佳答案

您不能通过序列化传递您的int[]int是原始类型,因此将您的int数组转换为String array 并通过,因为 String 扩展了可序列化的 Object

关于java - 新 Activity 出现意外错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33727290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com