setAdapter使应用程序崩溃

时间:2018-08-29 18:21:21

标签: java android listview

我尝试将字符串从Firebase获取到ListView,但是当我添加行

listViewPerifereies.setAdapter(adapterPerifereies);

该应用程序崩溃了,它只是停止了。我确定ListView在正确的.xml(activity_perifereies_list.xml)中。我在另一个活动中使用了相同的代码,并且可以使用,但是现在在我创建的任何新活动中都没有。

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

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 com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;


public class PerifereiesList extends AppCompatActivity {

DatabaseReference databasePaths;
private ListView listViewPerifereies;
private ArrayList<String> ArrayListPerifereies;
public ArrayAdapter<String> adapterPerifereies;
Paths info;

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

    listViewPerifereies = (ListView) findViewById(R.id.epitelous);
    ArrayListPerifereies = new ArrayList<>();

    databasePaths = FirebaseDatabase.getInstance().getReference("Paths");

    databasePaths.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            ArrayListPerifereies.clear();

            for (DataSnapshot items: dataSnapshot.getChildren())
            {
                info = items.getValue(Paths.class);
                ArrayListPerifereies.add(info.perifereia);
            }


            adapterPerifereies = new ArrayAdapter<String>(PerifereiesList.this, android.R.layout.simple_list_item_1, ArrayListPerifereies);
            listViewPerifereies.setAdapter(adapterPerifereies);
            adapterPerifereies.notifyDataSetChanged();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}
}

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.spiros.mypath, PID: 7607
                  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                      at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:409)
                      at android.widget.ArrayAdapter.getView(ArrayAdapter.java:371)
                      at android.widget.AbsListView.obtainView(AbsListView.java:2413)
                      at android.widget.ListView.makeAndAddView(ListView.java:1986)
                      at android.widget.ListView.fillDown(ListView.java:709)
                      at android.widget.ListView.fillFromTop(ListView.java:770)
                      at android.widget.ListView.layoutChildren(ListView.java:1721)
                      at android.widget.AbsListView.onLayout(AbsListView.java:2212)
                      at android.view.View.layout(View.java:17708)
                      at android.view.ViewGroup.layout(ViewGroup.java:5631)
                      at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1858)
                      at android.view.View.layout(View.java:17708)
                      at android.view.ViewGroup.layout(ViewGroup.java:5631)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at android.view.View.layout(View.java:17708)
                      at android.view.ViewGroup.layout(ViewGroup.java:5631)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1766)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1610)
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1519)
                      at android.view.View.layout(View.java:17708)
                      at android.view.ViewGroup.layout(ViewGroup.java:5631)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at android.view.View.layout(View.java:17708)
                      at android.view.ViewGroup.layout(ViewGroup.java:5631)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1766)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1610)
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1519)
                      at android.view.View.layout(View.java:17708)
                      at android.view.ViewGroup.layout(ViewGroup.java:5631)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at com.android.internal.policy.DecorView.onLayout(DecorView.java:774)
                      at android.view.View.layout(View.java:17708)
                      at android.view.ViewGroup.layout(ViewGroup.java:5631)
                      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2519)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2234)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1372)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6774)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:926)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:735)
                      at android.view.Choreographer.doFrame(Choreographer.java:667)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:912)
                      at android.os.Handler.handleCallback(Handler.java:761)
                      at android.os.Handler.dispatchMessage(Handler.java:98)
                      at android.os.Looper.loop(Looper.java:156)
                      at android.app.ActivityThread.main(ActivityThread.java:6595)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
I/Process: Sending signal. PID: 7607 SIG: 9
Application terminated.

1 个答案:

答案 0 :(得分:0)

请确保您首先配置了适配器 然后确保您执行以下操作: 在您的onCreate()里面;

      Adapter adapter = new Adapter (Context)

确保您仅传递了上下文,然后可以膨胀菜单