gpt4 book ai didi

Android - 通过广播 Intent 传递长数组

转载 作者:太空狗 更新时间:2023-10-29 12:41:27 27 4
gpt4 key购买 nike

我写了一个小的 android 程序,有一个带有广播监听器的主 Activity ,我创建了另一个线程。该线程搜索素数,并将它们加载到一个长数组列表中,每隔 3 秒,将填充的数组通过广播发送到主 Activity 。一切正常,直到我试图从 Intent 中获得额外的长数组。每次都会导致空指针异常。我尝试使用字符串数组列表,它起作用了,但我很好奇,因为 Intent 有一个“getlongarrayextra”方法。

这是我的代码:

公共(public)类 MainActivity 扩展 Activity {

public static String BROADCAST_THREAD_KEY = "broadcast_key";
public static String EXTRAARRAYID = "primes";
private static long MAXNUM = 2000000;
private PrimeCalculatorThread thread;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.numberstext);
}

private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(android.content.Context context,
android.content.Intent intent) {
String origitext = textView.getText().toString();
long[] primes = intent.getExtras().getLongArray(EXTRAARRAYID);
Log.d("ASD", "broadcast received" + primes.toString());
StringBuilder builder = new StringBuilder();
if (primes != null) {
for (long prime : primes) {
builder.append(prime + " - ");
}
textView.setText(origitext + "\n" + builder.toString());
}

};
};

@Override
protected void onResume() {
Log.d("ASD", "ONRESUME");
initReceiverAndStartThread();
super.onResume();
}

private void initReceiverAndStartThread() {
IntentFilter filter = new IntentFilter(BROADCAST_THREAD_KEY);
registerReceiver(receiver, filter);
thread = new PrimeCalculatorThread(getBaseContext(), MAXNUM);
thread.start();
Log.d("ASD", "THREAD STARTED");
}

和第二个线程:

公共(public)类 PrimeCalculatorThread 扩展线程 {

private Context context;
private long maxnum;
List<Long> primes;
boolean isrunning;

public void setIsrunning(boolean isrunning) {
this.isrunning = isrunning;
}

private long counter = 0;
private long DELAYBETWEENBROADCAST = 3000;

public PrimeCalculatorThread(Context c, long maxnum) {
this.context = c;
this.maxnum = maxnum;
primes = new ArrayList<Long>();
}

@Override
public void run() {
long startTime = System.currentTimeMillis();
long estimatedTime;
isrunning = true;
for (long i = 0; i < maxnum; i++) {
Log.d("ASD", Boolean.toString(isrunning));
if (!isrunning)
break;
Log.d("ASD", i + "");
estimatedTime = System.currentTimeMillis() - startTime;
if (isPrime(i)) {
primes.add(i);
Log.d("ASD", i + "is a prime");
} else {
Log.d("ASD", i + "is not a prime");
}
if (estimatedTime > counter * DELAYBETWEENBROADCAST
+ DELAYBETWEENBROADCAST) { // elapsed another period
Log.d("ASD", primes.toString() + " will be sending.");
sendBroadCast();
primes.clear();
counter++;
}
try { //for debug purposes
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

private void sendBroadCast() {
Intent intent = new Intent(MainActivity.BROADCAST_THREAD_KEY);
intent.putExtra(MainActivity.EXTRAARRAYID, primes.toArray());
context.sendBroadcast(intent);
Log.d("ASD", "BROADCAST SENT" + primes.toString());
}

boolean isPrime(long n) {
if (n < 2)
return false;
if (n == 2 || n == 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
long sqrtN = (long) Math.sqrt(n) + 1;
for (long i = 6L; i <= sqrtN; i += 6) {
if (n % (i - 1) == 0 || n % (i + 1) == 0)
return false;
}
return true;
}

最佳答案

问题是您正在管理 Long 对象 列表并将其传递到 putExtra 中,这意味着您正在调用 putExtra(String name, Serializable value)。然后您尝试使用 getLongArray() 获取该值,但是您没有放置任何额外的长数组,您看!要解决这个问题,请替换

intent.putExtra(MainActivity.EXTRAARRAYID, primes.toArray());

long[] primesArray = new long[primes.size()];
for (int i = 0; i < primes.size(); i++) {
primesArray[i] = primes.get(i);
}
intent.putExtra(MainActivity.EXTRAARRAYID, primesArray);

这将调用正确的 putExtra(String name, long[] value) 方法。

关于Android - 通过广播 Intent 传递长数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25032112/

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