我的应用程序在启动时崩溃 [英] My application crashes on startup

查看:201
本文介绍了我的应用程序在启动时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序在启动时崩溃,没有显示任何错误。

我找不到原因。我尝试了很多解决方案,但徒劳无功



这里是MainActivity.java

  package com.example.android.mycourses; 

导入android.os.Bundle;
导入android.support.v7.app.AppCompatActivity;
导入android.widget.CheckBox;
导入android.widget.CompoundButton;
导入android.widget.ListView;
导入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;
$ b $ public class MainActivity extends AppCompatActivity {

private FirebaseDatabase mfirebasedatabase; //访问数据库
private DatabaseReference mDatabase; //类引用数据库的特定部分
private ChildEventListener mchildEventListener; //从数据库读取


TextView courseTV;
CheckBox复选框;
CourseAdapter适配器;


@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mfirebasedatabase = FirebaseDatabase.getInstance(); //实例化数据库(这是数据库的访问点)
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)
{
//保存在数据库中
课程cc = new Course(courseTV.getText()。toString());
mDatabase.push()。setValue(cc);
}
else if(!isChecked){
//从数据库中删除
}
}
});
mchildEventListener = new ChildEventListener(){
@Override $ b $ public void onChildAdded(DataSnapshot dataSnapshot,String s){
Course c = dataSnapshot.getValue(Course.class);
adapter.add(c);

$ b @Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot){


$ b @Override
public void onChildMoved(DataSnapshot dataSnapshot,String s){

}

@Override
public void onCancelled(DatabaseError databaseError){


};
mDatabase.addChildEventListener(mchildEventListener);


$ b private void populateUsersList(){
//构造数据源
ArrayList< Course> arrayOfUsers = Course.getCourses();
//创建适配器将数组转换为视图
CourseAdapter adapter = new CourseAdapter(this,arrayOfUsers);
//将适配器连接到一个ListView
ListView listView =(ListView)findViewById(R.id.listView);
listView.setAdapter(adapter);


$ / code $ / pre
$ b $ p

这里是activity_main.xml

 <?xml version =1.0encoding =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_horizo​​ntal_margin
android:paddingRight =@ dimen / activity_horizo​​ntal_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 GT;


< / RelativeLayout>

这里是item_message.xml

 <?xml version =1.0encoding =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_horizo​​ntal_margin
android:layout_marginStart =@ dimen / activity_horizo​​ntal_margin
android:orientation =vertical>

TextView $ b $ android android:layout_height =wrap_content
$ $ b android:layout_weight =0
android:textAppearance =?android:attr / textAppearanceLarge
tools:text =course/>
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:延迟启用-Xcheck:jni 
02-16 16:57:52.647 9095-9095 / com.example.android.mycourses W / System:ClassLoader引用未知路径:/ 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 started up,version:10084
02-16 16:57:52.686 9095-9095 / com.example.android.mycourses I / FA:启用调试日志记录运行:adb shell setprop log.tag.FA VERBOSE
02-16 16:57: 52.703 9095-9095 / com.example.android.mycourses I / FA:启用更快的调试模式事件记录运行:
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:找到类android.app.HwCustAlarmManagerImpl的构造函数b $ b 02-16 16:57:52.722 9095-9095 / com。 example.android.mycourses I / FirebaseInitProvider:FirebaseApp初始化成功ssful
02-16 16:57:52.775 9095-9095 / com.example.android.mycourses W / art:在Android 4.1之前,方法android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter android.graphics.PorterDuffColorFilter,android.content.res.ColorStateList,android.graphics.PorterDuff $ Mode)将不正确地覆盖android.graphics.drawable.Drawable中的包私有方法
02-16 16:57: 52.894 9095-9095 / com.example.android.mycourses I / DynamiteModule:考虑本地模块com.google.android.gms.firebase_database:4和远程模块com.google.android.gms.firebase_database:5
02- 16 16:57:52.894 9095-9095 / com.example.android.mycourses I / DynamiteModule:选定的远程版本com.google.android.gms.firebase_database,版本> = 5
02-16 16:57 :52.911 9095-9095 / com.example.android.mycourses W / System:ClassLoader引用未知路径:/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我/系统:core_booster,getBoosterConfig = false
02-16 16:57:52.989 9095-9095 / com。 example.android.mycourses I / Process:发送信号。 PID:9095 SIG:9


解决方案

如果你想让应用程序运行的话,那就不要使用Activity了。
$ b $ pre $ course $ =(TextView)findViewById(R.id.messageTextView) ;

checkbox =(CheckBox)findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener

这两个视图都不在activity_main里面


My app crashes on startup without showing any error.
I can't find why. I tried many solutions, but in vain

here is 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);
    }
}

here is 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>

here is 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>

here is what the log shows to me

   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

解决方案

These need to be in the adapter, not the Activity if you want the app to run

courseTV = (TextView) findViewById(R.id.messageTextView);

checkbox = ( CheckBox ) findViewById( R.id.checkbox );
checkbox.setOnCheckedChangeListener

Neither of those views are inside activity_main

这篇关于我的应用程序在启动时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆