当我尝试在“与我们联系”活动中发送消息时,我的应用程序崩溃了。 我是编程的新手,所以目前我正试图依靠tut。 这是我的代码:
Logcat错误
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@14d37fce time:3633891094
I/System.out: normalized = hi
I/System.out: No match.
I/System.out: writeCertainIFCaegories learnf.aiml size= 0
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lrtapp.ardentmap, PID: 20001
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.lrtapp.ardentmap.Adapter.ChatMessageAdapter.getView(ChatMessageAdapter.java:61)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillSpecific(ListView.java:1344)
at android.widget.ListView.layoutChildren(ListView.java:1663)
at android.widget.AbsListView.onLayout(AbsListView.java:2151)
at android.view.View.layout(View.java:15679)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:15679)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15679)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15679)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15679)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15679)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15679)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2098)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1855)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1073)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5903)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:773)
at android.view.Choreographer.doCallbacks(Choreographer.java:586)
at android.view.Choreographer.doFrame(Choreographer.java:556)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:759)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
I/Process: Sending signal. PID: 20001 SIG: 9
Application terminated.
联系人课程
package com.lrtapp.ardentmap;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.lrtapp.ardentmap.Adapter.ChatMessageAdapter;
import com.lrtapp.ardentmap.Model.ChatMessage;
import org.alicebot.ab.AIMLProcessor;
import org.alicebot.ab.Bot;
import org.alicebot.ab.Chat;
import org.alicebot.ab.MagicStrings;
import org.alicebot.ab.PCAIMLProcessorExtension;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
public class Contact extends AppCompatActivity {
ListView listView;
FloatingActionButton btnSend;
EditText edtTextMsg;
ImageView imageView;
private Bot bot;
public static Chat chat;
private ChatMessageAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
listView = findViewById(R.id.listView);
btnSend = findViewById(R.id.btnSend);
edtTextMsg = findViewById(R.id.user_message);
imageView = findViewById(R.id.imageView);
adapter = new ChatMessageAdapter(this, new ArrayList<ChatMessage>());
listView.setAdapter(adapter);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = edtTextMsg.getText().toString();
String response = chat.multisentenceRespond(edtTextMsg.getText().toString());
if (TextUtils.isEmpty(message)){
Toast.makeText(Contact.this, "Enter a query", Toast.LENGTH_SHORT).show();
return;
}
sendMessage(message);
botsReply(response);
//clear editText
edtTextMsg.setText("");
listView.setSelection(adapter.getCount() -1 );
}
});
boolean available = isSDCartAvailable();
AssetManager assets = getResources().getAssets();
File fileName = new File(Environment.getExternalStorageDirectory().toString() + "TBC/bots/TBC");
boolean makeFile = fileName.mkdirs();
if (fileName.exists()){
//read the line
try {
for(String dir : assets.list("TBC")){
File subDir = new File(fileName.getPath() + "/" + dir);
boolean subDir_Check = subDir.mkdirs();
for(String file : assets.list("TBC" + dir)){
File newFile = new File(fileName.getPath() + "/" + dir + "/" + file);
if(newFile.exists()){
continue;
}
InputStream in;
OutputStream out;
in = assets.open("TBC/" + dir + "/" + file);
out = new FileOutputStream(fileName.getPath() + "/" + dir + "/" + file);
//copy files from assets to the mobile's sd card or any secondary storage
copyFile(in, out);
in.close();
out.flush();
out.close();
}
}
} catch (IOException e){
e.printStackTrace();
}
}
//get the working directory
MagicStrings.root_path = Environment.getExternalStorageDirectory().toString() + "/TBC";
AIMLProcessor.extension = new PCAIMLProcessorExtension();
bot = new Bot("TBC", MagicStrings.root_path, "chat");
chat = new Chat(bot);
}
private void copyFile(InputStream in, OutputStream out) throws IOException{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
public static boolean isSDCartAvailable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)? true : false;
}
private void botsReply(String response) {
ChatMessage chatMessage = new ChatMessage(false, false, response);
adapter.add(chatMessage);
}
private void sendMessage(String message) {
ChatMessage chatMessage = new ChatMessage(false, true, message);
adapter.add(chatMessage);
}
}
联系xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".Contact">
<android.support.design.widget.FloatingActionButton
android:clickable="true"
android:src="@drawable/ic_send"
android:id="@+id/btnSend"
android:tint="@android:color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:layout_above="@id/btnSend"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll">
</ListView>
<EditText
android:layout_toLeftOf="@id/btnSend"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:id="@+id/user_message"
android:hint="Enter your message"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignStart="@+id/listView"
android:layout_alignTop="@+id/btnSend"
android:src="@drawable/bot" />
</RelativeLayout>
分级构建
apply plugin: 'com.android.application'
ext {
bintrayRepo = 'maven'
bintrayName = 'chat-message-view'
publishedGroupId = 'me.himanshusoni.chatmessageview'
libraryName = 'chat-message-view'
artifact = 'chat-message-view'
libraryDescription = 'Android library to create chat message view easily'
siteUrl = 'https://github.com/himanshu-soni/ChatMessageView'
gitUrl = 'https://github.com/himanshu-soni/ChatMessageView.git'
libraryVersion = '1.0.7'
developerId = 'himanshu-soni'
developerName = 'Himanshu Soni'
developerEmail = 'himanshusoni.me@gmail.com'
licenseName = 'The Apache Software License, Version 2.0'
licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
allLicenses = ["Apache-2.0"]
}
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.lrtapp.ardentmap"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.google.android.gms:play-services-base:15.0.1'
implementation 'com.google.android.gms:play-services-auth:16.0.0'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-messaging:17.3.1'
implementation 'com.google.firebase:firebase-storage:11.8.0'
implementation 'com.google.firebase:firebase-auth:11.8.0'
implementation 'com.google.maps.android:android-maps-utils:0.5+'
testImplementation 'junit:junit:4.12'
implementation 'com.google.firebase:firebase-core:16.0.3'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'me.biubiubiu.justifytext:library:1.1'
implementation 'com.github.cpiz:BubbleView:1.0.3'
implementation 'com.github.Google:gson:1.7'
implementation 'com.android.support:design:27.1.1'
//implementation 'com.google.code.gson:gson:2.8.5'
//implementation 'com.cpiz.bubbleview:bubbleview:{X.Y.Z}'
//Google Play Services
implementation 'com.google.android.gms:play-services-gcm:15.0.1'
//implementation 'com.google.android.gms:play-services:11.4.0'
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services-location:15.0.1'
implementation 'com.google.android.gms:play-services-places:15.0.1'
implementation files('libs/Ab.jar')
//implementation 'me.himanshusoni.chatmessageview:chat-message-view:1.0.7'
}
apply plugin: 'com.google.gms.google-services'
清单中使用的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
预先感谢能够做出回应的人们。任何答案将不胜感激。对不起,如果代码混乱。
答案 0 :(得分:2)
从错误日志中:问题是ImageView
的可绘制对象。
确保它存在于drawable
文件夹中。
也许您将其存储在drawable-v21
这样的子文件夹中,所以找不到它。
答案 1 :(得分:0)
似乎找不到 ImageView 中的@drawable/bot
。尝试更换它。确保drawable文件名不使用破折号
file-name.xml
-不好file_name.xml
-很好