我尝试在我的Android应用中实现ALICE bot但是当我在手机上运行时,我收到错误
java.lang.RuntimeException:无法启动活动ComponentInfo {com.btbankdemo.bt.btbank / com.btbankdemo.bt.btbank.ChatActivity}:java.lang.UnsupportedOperationException:您忘记调用setup方法来初始化BrainLogger < / p>
我哪里错了?
我的onClick活动
public class ContactActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
}
public void Chat (View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
我的主要活动
public class MainActivity extends Activity {
private static final String FRAGMENT_DIALOG_LOG_TAG = "BrainLoggerDialog";
private ListView chatListView;
private static ChatArrayAdapter adapter;
private EditText chatEditText;
private BrainLoggerDialog dialog;
private ResponseReceiver mMessageReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getFragmentManager();
if (savedInstanceState == null) {
Log.d("MainActivity", "onCreate savedInstanceState null");
adapter = new ChatArrayAdapter(getApplicationContext());
dialog = new BrainLoggerDialog();
if (!ChatBotApplication.isBrainLoaded()) {
dialog.show(fm, FRAGMENT_DIALOG_LOG_TAG);
} else {
dialog.setPositiveButtonEnabled(true);
}
} else {
Log.d("MainActivity", "onCreate savedInstanceState NOT null");
dialog = (BrainLoggerDialog) fm.findFragmentByTag(FRAGMENT_DIALOG_LOG_TAG);
}
chatListView = (ListView) findViewById(R.id.chat_listView);
chatListView.setAdapter(adapter);
chatEditText = (EditText) findViewById(R.id.chat_editText);
chatEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
final String question = chatEditText.getText().toString();
adapter.add(new ChatMessage(false, question));
chatEditText.setText("");
Intent brainIntent = new Intent(MainActivity.this, BrainService.class);
brainIntent.setAction(BrainService.ACTION_QUESTION);
brainIntent.putExtra(BrainService.EXTRA_QUESTION, question);
startService(brainIntent);
return true;
}
return false;
}
});
//hide keyboard
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
@Override
protected void onResume() {
super.onResume();
// Register mMessageReceiver to receive messages.
IntentFilter intentFilter = new IntentFilter(
Constants.BROADCAST_ACTION_BRAIN_STATUS);
intentFilter.addAction(Constants.BROADCAST_ACTION_BRAIN_ANSWER);
intentFilter.addAction(Constants.BROADCAST_ACTION_LOGGER);
mMessageReceiver = new ResponseReceiver();
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, intentFilter);
if (dialog != null && ChatBotApplication.isBrainLoaded()) {
dialog.loadLog();
dialog.setPositiveButtonEnabled(true);
}
}
@Override
protected void onPause() {
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_log) {
FragmentManager fm = getFragmentManager();
dialog = new BrainLoggerDialog();
dialog.show(fm, FRAGMENT_DIALOG_LOG_TAG);
return true;
}
return super.onOptionsItemSelected(item);
}
// Broadcast receiver for receiving status updates from the IntentService
private class ResponseReceiver extends BroadcastReceiver {
private ResponseReceiver() {
}
// Called when the BroadcastReceiver gets an Intent it's registered to receive
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Constants.BROADCAST_ACTION_BRAIN_STATUS)) {
int status = intent.getIntExtra(Constants.EXTRA_BRAIN_STATUS, 0);
switch (status) {
case Constants.STATUS_BRAIN_LOADING:
Toast.makeText(MainActivity.this, "brain loading", Toast.LENGTH_SHORT).show();
if (dialog != null) {
dialog.show(getFragmentManager(), FRAGMENT_DIALOG_LOG_TAG);
}
break;
case Constants.STATUS_BRAIN_LOADED:
Toast.makeText(MainActivity.this, "brain loaded", Toast.LENGTH_SHORT).show();
if (dialog != null) {
dialog.setPositiveButtonEnabled(true);
}
break;
}
}
if (intent.getAction().equalsIgnoreCase(Constants.BROADCAST_ACTION_BRAIN_ANSWER)) {
String answer = intent.getStringExtra(Constants.EXTRA_BRAIN_ANSWER);
adapter.add(new ChatMessage(true, answer));
adapter.notifyDataSetChanged();
}
if (intent.getAction().equalsIgnoreCase(Constants.BROADCAST_ACTION_LOGGER)) {
String info = intent.getStringExtra(Constants.EXTENDED_LOGGER_INFO);
if (info != null) {
Log.i("EXTENDED_LOGGER_INFO", info);
if (dialog != null) {
dialog.addLine(info);
}
}
}
}
}
}
BrainLogger.java
public class BrainLogger {
private Context context;
private static BrainLogger instance;
private ArrayList<String> logs;
public static void setup(Context context){
instance = new BrainLogger(context);
}
public static BrainLogger getInstance(){
if (instance==null){
throw new UnsupportedOperationException("You forgot to call setup method to inizialize BrainLogger");
}
return instance;
}
private BrainLogger(Context context){
this.context = context;
logs = new ArrayList<String>();
}
public void info(String line){
logs.add(line);
notify(line);
}
public ArrayList<String> getLogs(){
return logs;
}
private void notify(String line){
Intent localIntent =
new Intent(Constants.BROADCAST_ACTION_LOGGER)
// Puts the status into the Intent
.putExtra(Constants.EXTENDED_LOGGER_INFO, line);
// Broadcasts the Intent to receivers in this app.
LocalBroadcastManager.getInstance(context).sendBroadcast(localIntent);
}
}
我的错误记录
致命的例外:主要 流程:com.btbankdemo.bt.btbank,PID:26681 主题:主题:{default = overlay:system,iconPack:system,fontPkg:system,com.android.systemui = overlay:system,com.android.systemui.navbar = overlay:system} java.lang.RuntimeException:无法启动活动ComponentInfo {com.btbankdemo.bt.btbank / com.btbankdemo.bt.btbank.MainActivity}:java.lang.UnsupportedOperationException:您忘记调用setup方法来初始化BrainLogger 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1363) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5461) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:java.lang.UnsupportedOperationException:你忘了调用setup方法来使用BrainLogger 在com.btbankdemo.bt.btbank.BrainLogger.getInstance(BrainLogger.java:24) 在com.btbankdemo.bt.btbank.BrainLoggerDialog.loadLog(BrainLoggerDialog.java:139) 在com.btbankdemo.bt.btbank.BrainLoggerDialog.onCreateDialog(BrainLoggerDialog.java:44) 在android.app.DialogFragment.getLayoutInflater(DialogFragment.java:407) 在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973) 在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148) 在android.app.BackStackRecord.run(BackStackRecord.java:793) 在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535) 在android.app.FragmentController.execPendingActions(FragmentController.java:325) 在android.app.Activity.performStart(Activity.java:6267) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1363) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5461) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
的AndroidManifest.xml
<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ContactActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<service
android:name=".LoadBrainService"
android:exported="false"/>
<service android:name=".BrainService"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" />
</application>