我正在开发一个应用程序,向用户显示数据库的课程列表 我的问题是,当我尝试运行我的应用程序时,它崩溃而没有显示错误
我认为我的问题在于适配器,但我不确定 这是我的MainActivity.java`
package com.example.android.mycoursesapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.ListView;
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;
import java.util.List;
import static com.example.android.mycoursesapp.R.id.checkButton;
public class MainActivity extends AppCompatActivity {
ListView mCourseListView;
CheckBox mCheckBox;
CourseAdapter courseAdapter;
private ChildEventListener mchildEventListener; //to read from the database
private FirebaseDatabase mfirebasedatabase; //to access the database
private DatabaseReference mCoursesDatabaseReference; //references a specific part in the database
private DatabaseReference mMessagesDatabaseReference; //class references a specific part of the database
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize course ListView and its adapter
final List<Course> course = new ArrayList<>();
courseAdapter = new CourseAdapter(this, R.layout.list_item, course);
mCourseListView.setAdapter(courseAdapter);
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
//get reference to child node
mDatabase.child("courses").push().setValue("Physics");
mDatabase.child("courses").push().setValue("Electronics");
mDatabase.child("courses").push().setValue("System Analysis");
mDatabase.child("courses").push().setValue("Artificial Intelligence");
mDatabase.child("courses").push().setValue("English");
mDatabase.child("courses").push().setValue("Economics");
mchildEventListener=new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Course course=dataSnapshot.getValue(Course.class);
courseAdapter.add(course);
}
@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);
/*add the Value event listener to update the data in real-time
- displays the friendsDatabase items in a list*/
//addValueEventListener(friendsDatabaseReference);
/*Course course=new Course(listItemTextView.getText.
mMessagesDatabaseReference.push().setValue(course);*/
// Initialize references to views
mCourseListView = (ListView) findViewById(R.id.courseListView);
mCheckBox=(CheckBox) findViewById(checkButton);
//Creating Adapter object for setting to list
/*CourseAdapter adapter=new CourseAdapter(courseList,MainActivity.this);
mCourseListView.setAdapter(adapter);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_item,
listItemTextView,values);
mCourseListView.setAdapter(adapter);*/
}
}
这是Course.java
package com.example.android.mycoursesapp;
/**
*/
public class Course {
public String courseName;
//private boolean checked;
//private boolean checked = false;
public Course(String courseName) {
this.courseName = courseName;
//this.checked = checked;
}
public String getCourseName() {return courseName;}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
/*public void setChecked(boolean checked) {this.checked = checked;}
public boolean isChecked() {return checked;}*/
/*public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}*/
}
这里是CourseAdapter.java
package com.example.android.mycoursesapp;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
/**
*/
public class CourseAdapter extends ArrayAdapter<Course>{
//private Context mContext;
//ArrayList<Course> mylist=new ArrayList<>();
public CourseAdapter(Context context, int resource, List<Course> objects) {//resourse =list_item
super(context,resource,objects);
}
//@Override public int getCount() {return mylist.size();}
//@Override public Object getItem(int position) {return mylist.get(position).toString();}
/*@Override
public long getItemId(int position) {
return position;
}
public void onItemSelected(int position) {}
public class ViewHolder {
public TextView coursetext;
public CheckBox tick;}*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.list_item, parent, false);
}
TextView courseTextView = (TextView) convertView.findViewById(R.id.listItemTextView);
Course course=getItem(position);
courseTextView.setText(course.getCourseName());
/* TODO Auto-generated method stub
ViewHolder view = null;
LayoutInflater inflator = ((Activity) mContext).getLayoutInflater();
if (view == null) {
view = new ViewHolder();
convertView = inflator.inflate( R.layout.list_item, null);
view.coursetext= (TextView) convertView.findViewById(R.id.listItemTextView);
view.tick=(CheckBox)convertView.findViewById(R.id.checkButton);
view.tick.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
int getPosition = (Integer) button.getTag(); // Here
// we get the position that we have set for the checkbox using setTag.
mylist.get(getPosition).setChecked(button.isChecked()); // Set the value of checkbox to maintain its state.
if (isChecked) {
//do sometheing here
}
else
{
// code here
}
}
});
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
view.tick.setTag(position);
view.coursetext.setText("" + mylist.get(position).getCourseName());
view.tick.setChecked(mylist.get(position).isChecked());*/
return convertView;
}
}
任何帮助??
编辑:这是日志显示的内容
02-14 15:22:26.838 15829-15829/? I/art: Late-enabling -Xcheck:jni
02-14 15:22:26.932 15829-15829/com.example.android.mycoursesapp W/System: ClassLoader referenced unknown path: /data/app/com.example.android.mycoursesapp-2/lib/arm64
02-14 15:22:26.938 15829-15829/com.example.android.mycoursesapp I/InstantRun: Instant Run Runtime started. Android package is com.example.android.mycoursesapp, real application class is null.
02-14 15:22:27.460 15829-15829/com.example.android.mycoursesapp W/System: ClassLoader referenced unknown path: /data/app/com.example.android.mycoursesapp-2/lib/arm64
02-14 15:22:27.585 15829-15829/com.example.android.mycoursesapp I/FA: App measurement is starting up, version: 10084
02-14 15:22:27.586 15829-15829/com.example.android.mycoursesapp I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
02-14 15:22:27.604 15829-15829/com.example.android.mycoursesapp I/FA: To enable faster debug mode event logging run:
adb shell setprop debug.firebase.analytics.app com.example.android.mycoursesapp
02-14 15:22:27.684 15829-15829/com.example.android.mycoursesapp I/HwCust: Constructor found for class android.app.HwCustAlarmManagerImpl
02-14 15:22:27.694 15829-15829/com.example.android.mycoursesapp I/FirebaseInitProvider: FirebaseApp initialization successful
02-14 15:22:27.887 15829-15829/com.example.android.mycoursesapp 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-14 15:22:28.183 15829-15829/com.example.android.mycoursesapp I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:5
02-14 15:22:28.183 15829-15829/com.example.android.mycoursesapp I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 5
02-14 15:22:28.204 15829-15829/com.example.android.mycoursesapp W/System: ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/m/00000007/n/arm64-v8a
02-14 15:22:28.273 15829-15829/com.example.android.mycoursesapp I/HwSecImmHelper: mSecurityInputMethodService is null
02-14 15:22:28.286 15829-15829/com.example.android.mycoursesapp E/HAL: load: id=gralloc != hmi->id=gralloc
02-14 15:22:28.301 15829-15906/com.example.android.mycoursesapp I/System: core_booster, getBoosterConfig = false
02-14 15:22:28.333 15829-15905/com.example.android.mycoursesapp E/HAL: load: id=gralloc != hmi->id=gralloc
02-14 15:22:28.333 15829-15905/com.example.android.mycoursesapp I/OpenGLRenderer: Initialized EGL, version 1.4
02-14 15:22:28.338 15829-15905/com.example.android.mycoursesapp W/OpenGLRenderer: load: so=/system/lib64/libhwuibp.so
dlopen failed: library "/system/lib64/libhwuibp.so" not found
02-14 15:22:28.338 15829-15905/com.example.android.mycoursesapp W/OpenGLRenderer: Initialize Binary Program Cache: Load Failed
02-14 15:22:28.338 15829-15905/com.example.android.mycoursesapp E/HAL: load: id=gralloc != hmi->id=gralloc
02-14 15:22:29.325 15829-15939/com.example.android.mycoursesapp I/System: core_booster, getBoosterConfig = false
02-14 15:22:30.704 15829-15829/com.example.android.mycoursesapp I/Process: Sending signal. PID: 15829 SIG: 9
答案 0 :(得分:1)
在setContentView(R.layout.activity_main);
行之后向下移动两行。
mCourseListView = (ListView) findViewById(R.id.courseListView);
mCheckBox=(CheckBox) findViewById(checkButton);