2015-09-25 84 views
1

您好,我正在打开一个应用程序,打开具有语音识别功能的社交网络,该应用程序运行良好,但是如果语音识别对话框弹出并且我没有说任何应用程序强制关闭按回按钮,该logcat的部分代码指向:关闭语音识别对话框时的空对象

if (firstWord.equals("open")) { 
      PackageManager packageManager = getPackageManager(); 
      List<PackageInfo> packs = packageManager 
        .getInstalledPackages(0); 
      int size = packs.size(); 
      boolean uninstallApp = false; 
      boolean exceptFlg = false; 
      for (int v = 0; v < size; v++) { 
       PackageInfo p = packs.get(v); 
       String tmpAppName = p.applicationInfo.loadLabel(
         packageManager).toString(); 
       String pname = p.packageName; 
       tmpAppName = tmpAppName.toLowerCase(); 
       if (tmpAppName.trim().toLowerCase(). 
         equals(secondWord.trim().toLowerCase())) { 

        PackageManager pm = this.getPackageManager(); 
        Intent appStartIntent = pm.getLaunchIntentForPackage(pname); 


        if (null != appStartIntent) { 
         try { 
          Toast.makeText(getApplicationContext(), "That App is not a Social One ;-))", 
            Toast.LENGTH_LONG).show(); 
         } catch (Exception e) { 
         } 
        } 
       } 
      } 
     } // end of open app code 

我试图添加}else{,但它没有工作,

logcat的

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference 
      at mrad4tech.com.test.MainActivity.onActivityResult(MainActivity.java:94) 
      at android.app.Activity.dispatchActivityResult(Activity.java:6140) 
      at android.app.ActivityThread.deliverResults(ActivityThread.java:3535) 
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) 
            at android.app.ActivityThread.access$1300(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5223) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 
09-25 20:12:26.265 7041-7041/mrad4tech.com.test E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: mrad4tech.com.test, PID: 7041 
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1234, result=0, data=null} to activity {mrad4tech.com.test/mrad4tech.com.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference 
      at android.app.ActivityThread.deliverResults(ActivityThread.java:3539) 
      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) 
      at android.app.ActivityThread.access$1300(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5223) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference 
      at mrad4tech.com.test.MainActivity.onActivityResult(MainActivity.java:94) 
      at android.app.Activity.dispatchActivityResult(Activity.java:6140) 
      at android.app.ActivityThread.deliverResults(ActivityThread.java:3535) 
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) 
            at android.app.ActivityThread.access$1300(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5223) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 

完整的代码

import android.content.Intent; 
import android.content.pm.ApplicationInfo; 
import android.content.pm.PackageInfo; 
import android.content.pm.PackageManager; 
import android.os.Bundle; 
import android.app.Activity; 
import android.os.Handler; 
import android.speech.RecognizerIntent; 
import android.speech.SpeechRecognizer; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

import java.lang.ref.WeakReference; 
import java.util.ArrayList; 
import java.util.List; 

public class MainActivity extends Activity { 

    private 
    static 
    final 
    int VOICE_RECOGNITION_REQUEST_CODE = 1234; 
    private static final String TAG = String.valueOf(1); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button button1 = (Button) findViewById(R.id.button1); 
     button1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       startVoiceRecognitionActivity(); 
      } 
     }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return 
       true; 
    } 

    public void onClick(View v) { 

     switch (v.getId()) { 

      case R.id.button1: 
       startVoiceRecognitionActivity(); 

       break; 

     } 


    } 


    public void startVoiceRecognitionActivity() { 

     Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass() 
       .getPackage().getName()); 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
       RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5); 
     startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     String wordStr = null; 
     String[] words = null; 
     String firstWord = null; 
     String secondWord = null; 
     if (requestCode == VOICE_RECOGNITION_REQUEST_CODE 
       && resultCode == RESULT_OK) { 
      ArrayList<String> matches = data 
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      wordStr = matches.get(0); 
      words = wordStr.split(" "); 
      firstWord = words[0]; 
      secondWord = words[1]; 
     } 

     if (firstWord.equals("open")) { 
      PackageManager packageManager = getPackageManager(); 
      List<PackageInfo> packs = packageManager 
        .getInstalledPackages(0); 
      int size = packs.size(); 
      boolean uninstallApp = false; 
      boolean exceptFlg = false; 
      for (int v = 0; v < size; v++) { 
       PackageInfo p = packs.get(v); 
       String tmpAppName = p.applicationInfo.loadLabel(
         packageManager).toString(); 
       String pname = p.packageName; 
       tmpAppName = tmpAppName.toLowerCase(); 
       if (tmpAppName.trim().toLowerCase(). 
         equals(secondWord.trim().toLowerCase())) { 

        PackageManager pm = this.getPackageManager(); 
        Intent appStartIntent = pm.getLaunchIntentForPackage(pname); 


        if (null != appStartIntent) { 
         try { 
          Toast.makeText(getApplicationContext(), "That App is not a Social One ;-))", 
            Toast.LENGTH_LONG).show(); 
         } catch (Exception e) { 
         } 
        } 
       } 
      } 
     } // end of open app code 
     else { 

     } 


    } 





} 

我试图添加其他} {Toast.makeText .....},但没有工作

+0

'如果(空= appStartIntent!)'不应该这是'如果(appStartIntent!= NULL)'可能并不重要IDK的。还有什么行是94行? – 3kings

+0

谢谢下面的家伙帮我欣赏你的尝试 –

回答

2
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE 
       && resultCode == RESULT_OK) { 
      ArrayList<String> matches = data 
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      wordStr = matches.get(0); 
      words = wordStr.split(" "); 
      firstWord = words[0]; 
      secondWord = words[1]; 
     } // This ends above if 

     if (firstWord.equals("open")) { 

在我看来,你继续if (firstWord.equals...即使结果代码WASN 't RESULT_OK。因此,您总是检查firstWord是否等于某个东西,即使您没有实际分配任何东西。

编辑:为了避免混乱和过度缩进代码,你可以简单地做

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode != VOICE_RECOGNITION_REQUEST_CODE || resultCode != RESULT_OK) { 
      return; // don't do anything 
     } 
     // Assume you have all data 
     // ... 
    } 
+0

OMG !!!!!!!!!你真的懂了!非常感谢你的摇滚! –

+0

我删除了“}//这结束了上面,如果”并把它放在最后 –

+0

我很高兴我可以帮助。如果你喜欢这个答案,也可以考虑提高它 – wasyl