启动我的应用程序后,它立即崩溃。由于某种原因,它声明了一个IllegalArgumentException,尽管我不明白为什么。我已经正确实例化了setContentView。我试图不带R.id就将参数设置为activity_main,尽管我没有任何位置,而我在stackoverflow上看了一会儿。
错误:
06-14 17:00:44.262 6209-6209/com.example.jarvis_main.chatapptest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jarvis_main.chatapptest, PID: 6209
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jarvis_main.chatapptest/com.example.jarvis_main.chatapptest.MainActivity}: java.lang.IllegalArgumentException: Cannot add a null child view to a ViewGroup
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IllegalArgumentException: Cannot add a null child view to a ViewGroup
at android.view.ViewGroup.addView(ViewGroup.java:4647)
at android.view.ViewGroup.addView(ViewGroup.java:4629)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:283)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:145)
at com.example.jarvis_main.chatapptest.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6980)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
码:
package com.example.jarvis_main.chatapptest;
import android.content.Intent;
import android.text.format.DateFormat;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.database.FirebaseListAdapter;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.FirebaseDatabase;
import static com.example.jarvis_main.chatapptest.R.layout.activity_main;
public class MainActivity extends AppCompatActivity {
private static int SignInRequestCode = 1;
private FirebaseListAdapter<ChatMessage> adapter;
RelativeLayout activity_main;
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity_main = (RelativeLayout)findViewById(R.id.activity_main);
setContentView(R.layout.activity_main);
fab = (FloatingActionButton)findViewById(R.id.fab);
fab.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(),SignInRequestCode);
}
else
{
Snackbar.make(activity_main, "Welcome "+FirebaseAuth.getInstance().getCurrentUser().getEmail(), Snackbar.LENGTH_SHORT).show();
//Load content
displayChatMessage();
}
}
@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,"Successfully signed out. Bye!", Snackbar.LENGTH_SHORT).show();
finish();
}
});
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SignInRequestCode)
{
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 later", Snackbar.LENGTH_SHORT).show();
finish();
}
}
}
private void displayChatMessage(){
ListView listOfMsg = (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) findViewById(R.id.message_text);
messageUser = (TextView) findViewById(R.id.message_user);
messageTime = (TextView) findViewById(R.id.message_time);
messageText.setText(model.getMessageTexts());
messageUser.setText(model.getMessageUsers());
messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime()));
}
};
listOfMsg.setAdapter(adapter);
}
}
最佳答案
在尝试获取子视图的句柄之前,请确保设置内容视图。
像这样:
. . .
setContentView(R.layout.activity_main);
activity_main = (RelativeLayout)findViewById(R.id.activity_main);
.
.