我正在构建与DialogFlow和Firebase集成的聊天机器人。如何在Firebase LogCat中修复会话错误问题(如下所示(?是由于我的Android Studio中的问题还是我与Firebase的联系?我也遇到了膨胀我的回收器视图的问题,它使用FirebaseRecyclerAdapter膨胀了它。
-
firebase应用程序注册以连接到其服务器需要大量时间,所以我跳过了步骤,以为我会通过我的Google-json文件连接。
-
由于我正在使用firebase UI数据库v5.0.0,因此我必须根据此处给出的最新更新来更改firebaseRecyClerAdapter方法:https://github.com/firebase/firebase/firebaseui-android/baseu-android/blob/blob/blob/master/master/database/readme.md
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.internchatbot">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:appComponentFactory"
android:appComponentFactory="androidx">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
app.gradle依赖性:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-database:18.0.0'
// FirebaseUI for Firebase Realtime Database
implementation 'com.firebaseui:firebase-ui-database:5.0.0'
implementation 'com.google.firebase:firebase-messaging:19.0.1'
//implementation 'com.firebaseui:firebase-ui-database:2.3.0'
implementation 'ai.api:sdk:2.0.7@aar'
//implementation
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'commons-io:commons-io:2.4'
implementation "ai.api:libai:1.6.12"
implementation 'com.google.firebase:firebase-core:17.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:recyclerview-v7:28.0.0-rc02'
}
apply plugin: 'com.google.gms.google-services'
mainActivity.java:
public class MainActivity extends AppCompatActivity implements AIListener{ //because I am recording audio too
RecyclerView recyclerView;
EditText editText;
RelativeLayout addBtn;
private DatabaseReference ref; //declaration of variables
Boolean flagFab = true;
final AIConfiguration config = new AIConfiguration("0c01e159babc4349b38eca698bd2f107",
AIConfiguration.SupportedLanguages.English,
AIConfiguration.RecognitionEngine.System);
private AIService aiService;
final AIDataService aiDataService = new AIDataService(config);
final AIRequest aiRequest = new AIRequest();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.RECORD_AUDIO},1);//permission from manifest file
recyclerView = findViewById(R.id.recyclerView);
editText = findViewById(R.id.editText);
addBtn = findViewById(R.id.addBtn);
recyclerView.setHasFixedSize(true);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
Query query = FirebaseDatabase.getInstance().getReference().child("chats").limitToLast(50);//upto 50 characters to be displayed
ref.keepSynced(true);//i don't know its purpose. Please explain :')
aiService = AIService.getService(this, config);
aiService.setListener(this);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = editText.getText().toString().trim();
if (!message.equals("")) {
ChatMessage chatMessage = new ChatMessage(message, "user");
ref.child("chat").push().setValue(chatMessage);
aiRequest.setQuery(message);
//MyAsyncTask is a subclass of MainActivity extending ASyncTask
MyAsyncTask myasynctask = new MyAsyncTask();
myasynctask.execute();
}
else {
aiService.startListening();
}
editText.setText("");
}
});
//Below code changes the image ie when audio is recorded it is mic else //arrow for sending message
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
ImageView fab_img = findViewById(R.id.fab_img);
Bitmap img = BitmapFactory.decodeResource(getResources(),R.drawable.ic_send_white_24dp);
Bitmap img1 = BitmapFactory.decodeResource(getResources(),R.drawable.ic_mic_white_24dp);
if (s.toString().trim().length()!=0 && flagFab){
ImageViewAnimatedChange(MainActivity.this,fab_img,img);
flagFab=false;
}
else if (s.toString().trim().length()==0){
ImageViewAnimatedChange(MainActivity.this,fab_img,img1);
flagFab=true;
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
//FirebaseRecyclerOption below because firebase+recycler view
FirebaseRecyclerOptions<ChatMessage>options=new FirebaseRecyclerOptions.Builder<ChatMessage>().setQuery(query,ChatMessage.class).build();
//Chatmessage,chat_rec//<image_details,blogviewholder>=<same>()<image_details.class,r.lao
final FirebaseRecyclerAdapter<ChatMessage, chat_rec> adapter = new FirebaseRecyclerAdapter<ChatMessage, chat_rec>(options) {
@NonNull
@Override
public chat_rec onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.msglist,parent,false);//R.layout.msglist is the default layout for chats
return new chat_rec(view);
}
@Override
protected void onBindViewHolder(chat_rec holder, int position, ChatMessage model) {
// Bind the Chat object to the ChatHolder
// ...
if (model.getMsgUser().equals("user")) {
holder.rightText.setText(model.getMsgText());
holder.rightText.setVisibility(View.VISIBLE);
holder.leftText.setVisibility(View.GONE);
}
else {
holder.leftText.setText(model.getMsgText());
holder.rightText.setVisibility(View.GONE);
holder.leftText.setVisibility(View.VISIBLE);
}
}
};
adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int msgCount = adapter.getItemCount();
int lastVisiblePosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
if (lastVisiblePosition == -1 ||
(positionStart >= (msgCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
recyclerView.scrollToPosition(positionStart);
}
}
});
recyclerView.setAdapter(adapter);
}//onCreate ends here
//Ayncronous Task defined here:
private class MyAsyncTask extends AsyncTask<AIRequest,Void,AIResponse>{
@Override
protected AIResponse doInBackground(AIRequest... aiRequests) {
final AIRequest request = aiRequests[0];
try {
final AIResponse response = aiDataService.request(aiRequest);
return response;
} catch (AIServiceException e) {
}
return null;
}
@Override
protected void onPostExecute(AIResponse response) {
//super.onPostExecute(response);
if (response != null) {
Result result = response.getResult();
String reply = result.getFulfillment().getSpeech();
ChatMessage chatMessage = new ChatMessage(reply, "KG Precision Moulders");
ref.child("Tech Support").push().setValue(chatMessage);
}
}
}
/*While recording audio there should be mic image in button and when typing message there should be arrow*/
public void ImageViewAnimatedChange(Context c, final ImageView v, final Bitmap new_image) {
final Animation anim_out = AnimationUtils.loadAnimation(c, R.anim.zoom_out);
final Animation anim_in = AnimationUtils.loadAnimation(c, R.anim.zoom_in);
anim_out.setAnimationListener(new Animation.AnimationListener()
{
@Override public void onAnimationStart(Animation animation) {}
@Override public void onAnimationRepeat(Animation animation) {}
@Override public void onAnimationEnd(Animation animation)
{
v.setImageBitmap(new_image);
anim_in.setAnimationListener(new Animation.AnimationListener() {
@Override public void onAnimationStart(Animation animation) {}
@Override public void onAnimationRepeat(Animation animation) {}
@Override public void onAnimationEnd(Animation animation) {}
});
v.startAnimation(anim_in);
}
});
v.startAnimation(anim_out);
}
@Override
public void onResult(ai.api.model.AIResponse response) {
Result result = response.getResult();
String message = result.getResolvedQuery();
ChatMessage chatMessage0 = new ChatMessage(message, "user");
ref.child("chat").push().setValue(chatMessage0);
String reply = result.getFulfillment().getSpeech();
ChatMessage chatMessage = new ChatMessage(reply, "bot");
ref.child("chat").push().setValue(chatMessage);
}
代码中有零错误,但是当我选择firebase时的logcat显示以下错误:
E/SystemUpdate-Connection: create session error : RESULT_ERROR
E/MiPicks-Connection: create session error : RESULT_ERROR
E/MiPicks-ConnectionRSA: get key exception : com.android.org.bouncycastle.util.encoders.DecoderException: unable to decode base64 string: invalid characters encountered in base64 data
,我也得到了回收clerviewException。错误可能是由于回收库不会被夸大的事实:
Process: com.example.internchatbot, PID: 6443
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.internchatbot/com.example.internchatbot.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class android.support.v7.widget.RecyclerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class android.support.v7.widget.RecyclerView
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v7.widget.RecyclerView
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.RecyclerView" on path: DexPathList[[zip file "/data/app/com.example.internchatbot-1/base.apk", zip file "/data/app/com.example.internchatbot-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.internchatbot-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.internchatbot-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.internchatbot-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.internchatbot-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.internchatbot-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.internchatbot-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.internchatbot-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.internchatbot-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.internchatbot-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.internchatbot-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.internchatbot-1/lib/arm64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:74)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.inflate(LayoutInflater.java:522)
at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.internchatbot.MainActivity.onCreate(MainActivity.java:68)
at android.app.Activity.performCreate(Activity.java:6845)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
2019-07-02 00:47:44.291 6443-6443/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
我可以直到这里。帮我!我死了!
firebase应用程序注册要连接到其服务器需要大量时间,因此我跳过了步骤,以为我会通过我的Google-json文件连接它。
重要的是,将您的应用程序注册到Firebase控制台,然后确保您的项目中有Google-Services.json文件。它包含API密钥和一些配置的东西。没有它可能会导致错误,例如您现在正在处理的问题。
似乎由于回收器的视图而出现了您的火箱错误。这意味着在Firebase开始其会话之前,应用程序崩溃了。
因此,我建议您首先修复回收即可通货膨胀错误。为此,请检查下面的库是否存在于您的应用程序级别build.gradle
文件中。
implementation 'com.android.support:recyclerview-v7:28.0.0'
在这种情况下,由于我从Android迁移到Androidx,因此我不得不更改以前的RecyClerview IE的布局/activity_main.xml从 Yosi先生向我指出的android.support.v7.widget.RecyclerView
至androidx.recyclerview.widget.RecyclerView
。
因此,我更新的activity_main.xml文件:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.internchatbot.MainActivity"
>
<androidx.recyclerview.widget.RecyclerView <!--change made here-->
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingBottom="50dp"
android:clipToPadding="false"
android:background="#f4f6f7"
/>
...
...
</RelativeLayout>