2017-02-16 71 views
1

我的應用程序在啓動時崩潰而沒有顯示任何錯誤。
我找不到原因。我嘗試了許多解決方案,但白白我的應用程序在啓動時崩潰

這裏是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

我不認爲你知道'findViewById'如何工作......'findViewById(R.id.checkbox);'返回null。你的應用程序確實有錯誤,並且它是一個nullpointerexception –

+0

它總是顯示一個錯誤。在android監視器上從「僅顯示選擇的應用程序」切換到「無過濾器」。 – ZeroOne

回答

0

這些都需要在適配器,而不是活動,如果你想在應用程序運行

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

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

無論這些意見都在裏面activity_main

+0

男人你真棒!但你可以解決我的問題的其餘部分?當我把複選框中的適配器類..如何將我檢查的項目保存在我的數據庫? –

+0

如果還有其他問題,請點擊[提問](// stackoverflow.com/questions/ask)按鈕。 –

+0

好的謝謝你的幫助 –

2

嘗試清潔您的構建:(設置 - >應用程序 - > MyApp - >內存 - >清除數據)已知可以解決類加載器問題。

否則在你的xml佈局中可能會有問題。