我的应用在启动时崩溃,但没有显示任何错误 我找不到原因。我尝试了很多解决方案,但徒劳无功
这是MainActivity.java
package com.example.android.mycourses;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private FirebaseDatabase mfirebasedatabase; //to access the database
private DatabaseReference mDatabase; //class references a specific part of the database
private ChildEventListener mchildEventListener; //to read from the database
TextView courseTV;
CheckBox checkbox;
CourseAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mfirebasedatabase=FirebaseDatabase.getInstance();//to instantiate the database(this is the access point to the database)
mDatabase=mfirebasedatabase.getReference().child("courses");
courseTV = (TextView) findViewById(R.id.messageTextView);
populateUsersList();
checkbox = ( CheckBox ) findViewById( R.id.checkbox );
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// save in database
Course cc=new Course(courseTV.getText().toString());
mDatabase.push().setValue(cc);
}
else if(!isChecked){
//remove from database
}
}
});
mchildEventListener =new ChildEventListener(){
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Course c=dataSnapshot.getValue(Course.class);
adapter.add(c);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
mDatabase.addChildEventListener(mchildEventListener);
}
private void populateUsersList() {
// Construct the data source
ArrayList<Course> arrayOfUsers = Course.getCourses();
// Create the adapter to convert the array to views
CourseAdapter adapter = new CourseAdapter(this, arrayOfUsers);
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}
这里是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.mycourses.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"></ListView>
</RelativeLayout>
这里是item_message.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:orientation="vertical">
<TextView
android:id="@+id/messageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:text="course" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox"
android:layout_alignRight="@+id/messageTextView"/>
</RelativeLayout>
这是日志显示给我的内容
02-16 16:57:52.515 9095-9095/? I/art: Late-enabling -Xcheck:jni
02-16 16:57:52.647 9095-9095/com.example.android.mycourses W/System: ClassLoader referenced unknown path: /data/app/com.example.android.mycourses-1/lib/arm64
02-16 16:57:52.686 9095-9095/com.example.android.mycourses I/FA: App measurement is starting up, version: 10084
02-16 16:57:52.686 9095-9095/com.example.android.mycourses I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
02-16 16:57:52.703 9095-9095/com.example.android.mycourses I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.example.android.mycourses
02-16 16:57:52.712 9095-9095/com.example.android.mycourses I/HwCust: Constructor found for class android.app.HwCustAlarmManagerImpl
02-16 16:57:52.722 9095-9095/com.example.android.mycourses I/FirebaseInitProvider: FirebaseApp initialization successful
02-16 16:57:52.775 9095-9095/com.example.android.mycourses W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-16 16:57:52.894 9095-9095/com.example.android.mycourses I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:5
02-16 16:57:52.894 9095-9095/com.example.android.mycourses I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 5
02-16 16:57:52.911 9095-9095/com.example.android.mycourses W/System: ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/m/00000007/n/arm64-v8a
02-16 16:57:52.967 9095-9144/com.example.android.mycourses I/System: core_booster, getBoosterConfig = false
02-16 16:57:52.989 9095-9095/com.example.android.mycourses I/Process: Sending signal. PID: 9095 SIG: 9
答案 0 :(得分:2)
尝试清理您的版本:(设置 - &gt;应用程序 - &gt; MyApp - &gt;内存 - &gt;清除数据)已知可以修复类加载器问题。
否则,您的xml布局可能存在问题。
答案 1 :(得分:0)
如果您希望应用运行
,则需要在适配器中,而不是活动courseTV = (TextView) findViewById(R.id.messageTextView);
checkbox = ( CheckBox ) findViewById( R.id.checkbox );
checkbox.setOnCheckedChangeListener
这些观点都不在activity_main
中