实际上,我正在开发一个应用程序,它可以根据用户对手机的说法打开另一个应用程序。例如,当我说“Facebook”时,该应用程序将自动打开Facebook。
但我遇到调试器显示Caused by: java.lang.NullPointerException
的问题。
这是我的源代码。
Test.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);
}
}
Calculation.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)
答案 0 :(得分:1)
Calculation.java是一个Activity类,你有
Calculation cal=new Calculation(label, matches, installedApps, clsName)
这是错误的。 Activity不是一个普通的java类,它有一个生命周期,它在manifest中声明。
您永远不应该实例化Activity类。您应该将其设为普通的java类,并将Activity上下文传递给构造函数。
注意:startActivity()
是Activity的一种方法。你需要上下文。