Android studioでチャットアプリを作成しようとしています。アプリにビルドエラーがなく、アプリを起動できます。ただし、listOfMessage.setAdapter(adapter);
を使用すると、アプリがクラッシュします。それでもアプリは起動しますが、1〜2秒後にアプリがクラッシュします。logcat
に_Class does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped error message.
_が表示されます。解決方法を教えてください
listOfMessage.setAdapter(adapter);
を使用しない場合、アプリで書き留めたものはすべてlistview
に表示されませんが、エントリが正常にFirebaseに到達しています。以下のコード:
_public class MainActivity extends AppCompatActivity {
private static int SIGN_IN_REQUEST_CODE = 1;
private FirebaseListAdapter<ChatMessage> adapter;
RelativeLayout activity_main;
FloatingActionButton sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity_main =(RelativeLayout)findViewById(R.id.activiy_main);
sendButton = (FloatingActionButton) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText input = (EditText)findViewById(R.id.input);
FirebaseDatabase.getInstance().getReference().Push().setValue(new ChatMessage(input.getText().toString(),
FirebaseAuth.getInstance().getCurrentUser().getEmail()));
input.setText("");
}
});
if(FirebaseAuth.getInstance().getCurrentUser() == null) {
startActivityForResult(AuthUI.getInstance().createSignInIntentBuilder().build(), SIGN_IN_REQUEST_CODE);
}
else
{
Snackbar.make(activity_main,"Welcome "+FirebaseAuth.getInstance().getCurrentUser().getEmail(),Snackbar.LENGTH_SHORT).show();
displayChatMessage();
}
}
private void displayChatMessage() {
ListView listOfMessage = (ListView)findViewById(R.id.list_of_message);
adapter = new FirebaseListAdapter<ChatMessage>(this,ChatMessage.class,R.layout.list_item,FirebaseDatabase.getInstance().getReference())
{
@Override
protected void populateView(View v, ChatMessage model, int position) {
TextView messageText, messageUser,messageTime;
messageText = (TextView)v.findViewById(R.id.message_text);
messageUser = (TextView)v.findViewById(R.id.message_user);
messageTime = (TextView)v.findViewById(R.id.message_time);
messageText.setText(model.getMessageText());
messageUser.setText(model.getMessageText());
messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",model.getMessageTime()));
}
};
//BELOW ONE NEEDS TO BE ACTIVE...TO MAKE ENTRIES VISIBLE
//listOfMessage.setAdapter(adapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SIGN_IN_REQUEST_CODE)
{
if(resultCode == RESULT_OK)
{
Snackbar.make(activity_main, "Successfully signed in. Welcome !", Snackbar.LENGTH_SHORT).show();
displayChatMessage();
}
else {
Snackbar.make(activity_main, "we couldn't sign you in. Please try again", Snackbar.LENGTH_SHORT).show();
finish();
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.menu_sign_out)
{
AuthUI.getInstance().signOut(this).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Snackbar.make(activity_main,"You have been signed out..", Snackbar.LENGTH_SHORT).show();
finish();
}
});
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
}
_
および以下の_ChatMessage.class
_:
_public class ChatMessage {
private String messageText;
private String messageUser;
private long messageTime;
public ChatMessage(String messageText, String messageUser) {
this.messageText = messageText;
this.messageUser = messageUser;
messageTime = new Date().getTime();
}
public ChatMessage(String messageText) {
this.messageText = messageText;
}
public String getMessageText() {
return messageText;
}
public void setMessageText(String messageText) {
this.messageText = messageText;
}
public String getMessageUser() {
return messageUser;
}
public void setMessageUser(String messageUser) {
this.messageUser = messageUser;
}
public long getMessageTime() {
return messageTime;
}
public void setMessageTime(long messageTime) {
this.messageTime = messageTime;
}
}
_
logcat
の完全なエラー:
_com.google.firebase.database.DatabaseException: Class com.example.cagrik.chat.ChatMessage does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
at com.google.Android.gms.internal.firebase_database.zzku.zza(Unknown Source:44)
at com.google.Android.gms.internal.firebase_database.zzkt.zzb(Unknown Source:196)
at com.google.Android.gms.internal.firebase_database.zzkt.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at com.firebase.ui.database.FirebaseListAdapter.parseSnapshot(FirebaseListAdapter.Java:120)
at com.firebase.ui.database.FirebaseListAdapter.getItem(FirebaseListAdapter.Java:109)
at com.firebase.ui.database.FirebaseListAdapter.getView(FirebaseListAdapter.Java:137)
at Android.widget.AbsListView.obtainView(AbsListView.Java:3189)
at Android.widget.ListView.makeAndAddView(ListView.Java:2197)
at Android.widget.ListView.fillDown(ListView.Java:824)
at Android.widget.ListView.fillFromTop(ListView.Java:885)
at Android.widget.ListView.layoutChildren(ListView.Java:1924)
at Android.widget.AbsListView.onLayout(AbsListView.Java:2961)
at Android.view.View.layout(View.Java:20854)
at Android.view.ViewGroup.layout(ViewGroup.Java:6401)
at Android.widget.RelativeLayout.onLayout(RelativeLayout.Java:1080)
at Android.view.View.layout(View.Java:20854)
at Android.view.ViewGroup.layout(ViewGroup.Java:6401)
at Android.widget.FrameLayout.layoutChildren(FrameLayout.Java:323)
at Android.widget.FrameLayout.onLayout(FrameLayout.Java:261)
at Android.view.View.layout(View.Java:20854)
at Android.view.ViewGroup.layout(ViewGroup.Java:6401)
at Android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.Java:443)
at Android.view.View.layout(View.Java:20854)
at Android.view.ViewGroup.layout(ViewGroup.Java:6401)
at Android.widget.FrameLayout.layoutChildren(FrameLayout.Java:323)
at Android.widget.FrameLayout.onLayout(FrameLayout.Java:261)
at Android.view.View.layout(View.Java:20854)
at Android.view.ViewGroup.layout(ViewGroup.Java:6401)
at Android.widget.LinearLayout.setChildFrame(LinearLayout.Java:1791)
at Android.widget.LinearLayout.layoutVertical(LinearLayout.Java:1635)
at Android.widget.LinearLayout.onLayout(LinearLayout.Java:1544)
at Android.view.View.layout(View.Java:20854)
at Android.view.ViewGroup.layout(ViewGroup.Java:6401)
at Android.widget.FrameLayout.layoutChildren(FrameLayout.Java:323)
at Android.widget.FrameLayout.onLayout(FrameLayout.Java:261)
at com.Android.internal.policy.DecorView.onLayout(DecorView.Java:945)
at Android.view.View.layout(View.Java:20854)
at Android.view.ViewGroup.layout(ViewGroup.Java:6401)
at Android.view.ViewRootImpl.performLayout(ViewRootImpl.Java:2956)
at Android.view.ViewRootImpl.performTraversals(ViewRootImpl.Java:2643)
at Android.view.ViewRootImpl.doTraversal(ViewRootImpl.Java:1780)
at Android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.Java:7827)
at Android.view.Choreographer$CallbackRecord.run(Choreographer.Java:911)
at Android.view.Choreographer.doCallbacks(Choreographer.Java:723)
at Android.view.Choreographer.doFrame(Choreographer.Java:658)
at Android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.Java:897)
at Android.os.Handler.handleCallback(Handler.Java:789)
at Android.os.Handler.dispatchMessage(Handler.Java:98)
at Android.os.Looper.loop(Looper.Java:164)
at Android.app.ActivityThread.main(ActivityThread.Java:6944)
at Java.lang.reflect.Method.invoke(Native Method)
at com.Android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.Java:327)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:1374)
_
私は単に欠けていました:
public ChatMessage(){}
on ChatMessage.class
-解決策を見つけてくれて@Michaelに感謝します。
また、内部クラスを使用している場合は、そのメッセージを受け取る可能性があります。次に、その内部クラスを独自のファイルに抽出するのが最善です。この場合は、ChatMessage.Javaです。