gpt4 book ai didi

java - Android 由 : java. lang.NullPointerException 引起

转载 作者:行者123 更新时间:2023-12-01 12:52:41 25 4
gpt4 key购买 nike

实际上,我正在开发一个应用程序,它可以根据用户对手机说的话打开另一个应用程序。例如,当我说“Facebook”时,应用程序将自动打开 Facebook。但我遇到了一个问题,调试器显示Caused by: java.lang.NullPointerException

这是我的源代码。

测试.java

package com.example.Voice_Recognizer;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.EditText;

public class Test extends Activity
{
private EditText editText;

private List<String> label = new ArrayList<String>();
private List<Drawable> icon = new ArrayList<Drawable>();
private List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
private List<Class> clsName = new ArrayList<Class>();

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.edittext);
getApp();
}

public void getApp() {
PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
for(ApplicationInfo app : apps) {
//checks for flags; if flagged, check if updated system app
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
installedApps.add(app);
clsName.add(pm.getClass());
label.add((String)pm.getApplicationLabel(app));
icon.add(pm.getApplicationIcon(app));
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
//Discard this one
//in this case, it should be a user-installed app
} else {
installedApps.add(app);
clsName.add(pm.getClass());
label.add((String)pm.getApplicationLabel(app));
icon.add(pm.getApplicationIcon(app));
}
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1)
{
if (resultCode == Activity.RESULT_OK)
{
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

Calculation cal=new Calculation(label, matches, installedApps, clsName);
/*if (matches.size() > 0)
{
editText.setText(matches.get(0));
}*/
}
}
}

public void onClick_Voice_Recognizer(View view)
{

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "");
startActivityForResult(intent, 1);
}
}

计算.java

package com.example.Voice_Recognizer;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class Calculation extends Activity {
private List<String> label;
private List<String> input;
private List<ApplicationInfo> pkgName;
private List<Class> clsName;
private String outputCls;
private String outputPkg;

public Calculation (List<String> label, List<String> input, List<ApplicationInfo>pkgName, List<Class> clsName) {
this.label = new ArrayList<String>(label);
this.input = new ArrayList<String>(input);
this.pkgName = new ArrayList<ApplicationInfo>(pkgName);
this.clsName=new ArrayList<Class>(clsName);
outputCls="";
outputPkg="";
comparison();
}

private void comparison() {
int count = 0;
for(int i = 0; i < input.size(); i++)
for(int k = 0; k < label.size(); k++){
if(input.get(i).equalsIgnoreCase(label.get(k))) {
try {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(new ComponentName(pkgName.get(k).packageName, clsName.get(k).getSimpleName()));
startActivity(intent);
} catch (NullPointerException e) {
Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
}

}
}
//this.count.add(count);
}

private static ComponentName unflattenFromString(String str) {
int sep = str.indexOf('/');
if (sep < 0 || (sep+1) >= str.length()) {
return null;
}
String pkg = str.substring(0, sep);
String cls = str.substring(sep+1);
if (cls.length() > 0 && cls.charAt(0) == '.') {
cls = pkg + cls;
}
return new ComponentName(pkg, cls);
}
}

错误消息

Caused by: java.lang.NullPointerException
at com.example.Voice_Recognizer.Calculation.comparison(Calculation.java:46)
at com.example.Voice_Recognizer.Calculation.<init>(Calculation.java:31)
at com.example.Voice_Recognizer.Test.onActivityResult(Test.java:68)

最佳答案

Calculation.java 是一个 Activity 类,您有

  Calculation cal=new Calculation(label, matches, installedApps, clsName)

这是错误的。 Activity 不是一个普通的 java 类,它有生命周期,并且在 list 中声明。

您永远不应该实例化 Activity 类。您可能应该将其设为普通的 java 类并将 Activity 上下文传递给构造函数。

注意:startActivity() 是 Activity 的一种方法。您将需要上下文。

关于java - Android 由 : java. lang.NullPointerException 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24105406/

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