简体   繁体   English

Android由:java.lang.NullPointerException

[英]Android Caused by: java.lang.NullPointerException

Actually, I am developing an app which can open another app according what user said to the cell phone. 实际上,我正在开发一个可以根据用户对手机说的内容打开另一个应用程序的应用程序。 For example, when I said 'Facebook', the app will open Facebook automatically. 例如,当我说“ Facebook”时,该应用程序将自动打开Facebook。 But I face a problem that the debugger showed Caused by: java.lang.NullPointerException . 但是我遇到了调试器显示的问题, Caused by: java.lang.NullPointerException

Here's my source code. 这是我的源代码。

Test.java 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 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);
    }
}

The Error Message 错误讯息

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 is a Activity class and you have Calculation.java是一个Activity类,您拥有

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

which is wrong. 这是错误的。 Activity is not a normal java class it has a lifecycle and it is declared in manifest. Activity不是普通的Java类,它具有生命周期,并在清单中声明。

You should never instantiate Activity class. 您永远不要实例化Activity类。 You should probably make it normal java class and pass the Activity context to the constructor. 您可能应该使它成为普通的Java类,并将Activity上下文传递给构造函数。

Note : startActivity() is a method of Activity. 注意: startActivity()是Activity的一种方法。 You will need context. 您将需要上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM