不幸的是,我的应用程序在使用Phonegap后已停止

时间:2015-12-08 09:00:17

标签: android cordova

我正在开发一种用于语音识别的混合移动应用程序。当我在我的应用程序中使用手机间隙时,我的日志猫中出现以下错误。请有人帮我解决这个错误。

主要活动:

package com.example.anjaneya;
import java.util.ArrayList;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ToggleButton;
import java.util.Date;
import java.util.List;
import java.util.Random;
import com.phonegap.DroidGap;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.*;

public class MainActivity extends DroidGap implementsRecognitionListener 

{

public TextView returnedText;
public Button search;
public ToggleButton toggleButton;
public ProgressBar progressBar;
public SpeechRecognizer speech = null;
public Intent recognizerIntent;
public String LOG_TAG = "MainActivity";
public String text = "";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.loadUrl("file:///android_asset/www/index.html");
returnedText = (TextView) findViewById(R.id.textView1);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);

progressBar.setVisibility(View.INVISIBLE);
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
"en");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);

/*search = (Button) findViewById(R.id.Button1);

search.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) 
{   
Intent i = new Intent(MainActivity.this, Main1Activity.class);
startActivity(i);
}  
});*/
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
progressBar.setVisibility(View.VISIBLE);
progressBar.setIndeterminate(true);
speech.startListening(recognizerIntent);
} else {
progressBar.setIndeterminate(false);
progressBar.setVisibility(View.INVISIBLE);
speech.stopListening();
}
}
});

}

@Override
public void onResume() {
super.onResume();
}

@Override
protected void onPause() {
super.onPause();
if (speech != null) {
speech.destroy();
Log.i(LOG_TAG, "destroy");
}

}

@Override
public void onBeginningOfSpeech() {
Log.i(LOG_TAG, "onBeginningOfSpeech");
progressBar.setIndeterminate(false);
progressBar.setMax(10);
}

@Override
public void onBufferReceived(byte[] buffer) {
Log.i(LOG_TAG, "onBufferReceived: " + buffer);
}

@Override
public void onEndOfSpeech() {
Log.i(LOG_TAG, "onEndOfSpeech");
progressBar.setIndeterminate(true);
toggleButton.setChecked(false);
}

@Override
public void onError(int errorCode) {
String errorMessage = getErrorText(errorCode);
Log.d(LOG_TAG, "FAILED " + errorMessage);
returnedText.setText(errorMessage);
toggleButton.setChecked(false);
}

@Override
public void onEvent(int arg0, Bundle arg1) {
Log.i(LOG_TAG, "onEvent");
}

@Override
public void onPartialResults(Bundle arg0) {
Log.i(LOG_TAG, "onPartialResults");
}

@Override
public void onReadyForSpeech(Bundle arg0) {
Log.i(LOG_TAG, "onReadyForSpeech");
}

@Override
public void onResults(Bundle results) {
Log.i(LOG_TAG, "onResults");
ArrayList<String> matches = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

for (String result : matches)
text += result + "\n";

returnedText.setText(text);
/*String dtStart = returnedText.getText().toString();  
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
try {  
Date date = format.parse(dtStart);  

} catch (ParseException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
}*/

}

@Override
public void onRmsChanged(float rmsdB) {
Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
progressBar.setProgress((int) rmsdB);
}

public static String getErrorText(int errorCode) {
String message;
switch (errorCode) {
case SpeechRecognizer.ERROR_AUDIO:
message = "Audio recording error";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "Client side error";
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "Insufficient permissions";
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "Network error";
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "Network timeout";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "No match";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "RecognitionService busy";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "error from server";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "No speech input";
break;
default:
message = "Didn't understand, please try again.";
break;
}
return message;
}

/* public void converttext(View view)
{
Intent intent = new Intent(this, activity_main1.class);
ImageView search = (ImageView) findViewById(R.id.imageView1);
String message = Image.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}*/
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_COMMENTS = "comments";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_DATE = "date";
public static final String COLUMN_COMMENT = "comment";
private static final String DATABASE_NAME = "comments.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_DATE
+ " date not null, " + COLUMN_COMMENT + "text not null" +  ")";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{     
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
onCreate(db);
}
}
public class Comment {
public long id;
public Date date;
public String comment;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public void getDate()
{
String dtStart = returnedText.getText().toString();  
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
try {  
date = format.parse(dtStart);  
} catch (ParseException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
} 
}
public void setDate(Date date)
{
this.date = date;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return comment;
}
} 
public class CommentsDataSource {
// Database fields
public SQLiteDatabase database;
public MySQLiteHelper dbHelper;
public String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_DATE, MySQLiteHelper.COLUMN_COMMENT };
public CommentsDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Comment createComment(String comment) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_COMMENT, comment);
long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
Comment newComment = cursorToComment(cursor);
cursor.close();
return newComment;
}
/*public void deleteComment(Comment comment) {
long id = comment.getId();
System.out.println("Comment deleted with id: " + id);
database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
+ " = " + id, null);
}*/
public List<Comment> getAllComments() {
List<Comment> comments = new ArrayList<Comment>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Comment comment = cursorToComment(cursor);
comments.add(comment);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return comments;
}
private Comment cursorToComment(Cursor cursor) {
Comment comment = new Comment();
comment.setId(cursor.getLong(0));
comment.setComment(cursor.getString(1));
return comment;
}
} 
public class TestDatabaseActivity extends ListActivity {
private CommentsDataSource datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> values = datasource.getAllComments();
// use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
@SuppressWarnings("unchecked")
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
Comment comment = null;
switch (view.getId()) {
case R.id.Button1:
String[] comments = text.split("");
int nextInt = new Random().nextInt(3);
// save the new comment to the database
comment = datasource.createComment(comments[nextInt]);
adapter.add(comment);
search = (Button) findViewById(R.id.Button1);
search.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view) 
{   Intent i = new Intent(MainActivity.this, Main1Activity.class);
startActivity(i);
}  
});
break;
}
adapter.notifyDataSetChanged();
}
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
} 
}

Xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar1"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp" />
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="ToggleButton" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/toggleButton1"
android:layout_marginTop="109dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:onClick="onClick"
android:text="OK" />
</RelativeLayout>

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.anjaneya"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main1Activity"
android:label="@string/title_activity_main1" >
</activity>
<activity
android:name=".Main2Activity"
android:label="@string/title_activity_main2" >
</activity>
<activity android:name="com.phonegap.DroidGap" android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
</activity>
</application>
</manifest>

Log cat:

12-08 12:14:24.344: I/art(1959): Not late-enabling -Xcheck:jni (already on)
12-08 12:14:24.344: I/art(1959): Late-enabling JIT
12-08 12:14:24.346: I/art(1959): JIT created with code_cache_capacity=2MB compile_threshold=1000
12-08 12:14:24.387: W/System(1959): ClassLoader referenced unknown path: /data/app/com.example.anjaneya-1/lib/x86
12-08 12:14:24.437: I/WebViewFactory(1959): Loading com.android.webview version 44.0.2403.119 (code 246011910)
12-08 12:14:24.554: W/System(1959): ClassLoader referenced unknown path: /system/app/webview/lib/x86
12-08 12:14:26.151: I/LibraryLoader(1959): Time to load native libraries: 367 ms (timestamps 7422-7789)
12-08 12:14:26.169: I/LibraryLoader(1959): Expected native library version number "",actual native library version number ""
12-08 12:14:26.287: V/WebViewChromiumFactoryProvider(1959): Binding Chromium to main looper Looper (main, tid 1) {3dfdbd5}
12-08 12:14:26.287: I/LibraryLoader(1959): Expected native library version number "",actual native library version number ""
12-08 12:14:26.430: I/chromium(1959): [INFO:library_loader_hooks.cc(120)] Chromium logging enabled: level = 0, default verbosity = 0
12-08 12:14:26.509: I/BrowserStartupController(1959): Initializing chromium process, singleProcess=true
12-08 12:14:26.529: W/art(1959): Attempt to remove non-JNI local reference, dumping thread
12-08 12:14:26.585: E/SysUtils(1959): ApplicationContext is null in ApplicationStatus
12-08 12:14:27.590: W/chromium(1959): [WARNING:resource_bundle.cc(285)] locale_file_path.empty()
12-08 12:14:27.652: E/libEGL(1959): validate_display:255 error 3008 (EGL_BAD_DISPLAY)
12-08 12:14:27.652: E/libEGL(1959): validate_display:255 error 3008 (EGL_BAD_DISPLAY)
12-08 12:14:27.652: E/chromium(1959): [ERROR:gl_surface_egl.cc(327)] No suitable EGL configs found.
12-08 12:14:27.652: E/chromium(1959): [ERROR:gl_surface_android.cc(23)] GLSurfaceEGL::InitializeOneOff failed.
12-08 12:14:27.652: E/chromium(1959): [ERROR:browser_main_loop.cc(698)] GLSurface::InitializeOneOff failed
12-08 12:14:28.034: W/art(1959): Suspending all threads took: 200.571ms
12-08 12:14:28.078: I/art(1959): Background sticky concurrent mark sweep GC freed 14772(688KB) AllocSpace objects, 3(60KB) LOS objects, 61% free, 939KB/2MB, paused 202.016ms total 258.692ms
12-08 12:14:28.111: W/AudioManagerAndroid(1959): Requires BLUETOOTH permission
12-08 12:14:28.474: E/DataReductionProxySettingListener(1959): No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp
12-08 12:14:29.566: W/art(1959): Attempt to remove non-JNI local reference, dumping thread
12-08 12:14:29.767: W/AwContents(1959): onDetachedFromWindow called when already detached. Ignoring
12-08 12:14:30.488: D/AndroidRuntime(1959): Shutting down VM
12-08 12:14:30.488: D/AndroidRuntime(1959): --------- beginning of crash
12-08 12:14:30.488: E/AndroidRuntime(1959): FATAL EXCEPTION: main
12-08 12:14:30.488: E/AndroidRuntime(1959): Process: com.example.anjaneya, PID: 1959
12-08 12:14:30.488: E/AndroidRuntime(1959): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.anjaneya/com.example.anjaneya.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
12-08 12:14:30.488: E/AndroidRuntime(1959):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
12-08 12:14:30.488: E/AndroidRuntime(1959):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
12-08 12:14:30.488: E/AndroidRuntime(1959):     at android.app.ActivityThread.-wrap11(ActivityThread.java)
12-08 12:14:30.488: E/AndroidRuntime(1959):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
12-08 12:14:30.488: E/AndroidRuntime(1959):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-08 12:14:30.488: E/AndroidRuntime(1959):     at android.os.Looper.loop(Looper.java:148)
12-08 12:14:30.488: E/AndroidRuntime(1959):     at android.app.ActivityThread.main(ActivityThread.java:5417)
12-08 12:14:30.488: E/AndroidRuntime(1959):     at java.lang.reflect.Method.invoke(Native Method)
12-08 12:14:30.488: E/AndroidRuntime(1959):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-08 12:14:30.488: E/AndroidRuntime(1959):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-08 12:14:30.488: E/AndroidRuntime(1959): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
12-08 12:14:30.488: E/AndroidRuntime(1959):     at com.example.anjaneya.MainActivity.onCreate(MainActivity.java:58)
12-08 12:14:30.488: E/AndroidRuntime(1959):     at android.app.Activity.performCreate(Activity.java:6237)
12-08 12:14:30.488: E/AndroidRuntime(1959):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
12-08 12:14:30.488: E/AndroidRuntime(1959):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
12-08 12:14:30.488: E/AndroidRuntime(1959):     ... 9 more
12-08 12:14:37.514: I/Process(1959): Sending signal. PID: 1959 SIG: 9

0 个答案:

没有答案