Java 尝试添加ListView,但在尝试设置适配器的位置继续获取NPE

Java 尝试添加ListView,但在尝试设置适配器的位置继续获取NPE,java,android,listview,Java,Android,Listview,我试图添加一个列表视图,其中显示客户,首先只是名称和访问地址。以前从未使用过listview,nog一直在MainActivity.java:86线上获得NPE。未填写或使用客户的所有字段。但我只是想在我继续之前先让它工作。这是我的密码 主要活动 包com.huntercrm.hunteronline.app.Activities.Main import android.app.FragmentManager; import android.app.FragmentTransaction; im

我试图添加一个列表视图,其中显示客户,首先只是名称和访问地址。以前从未使用过listview,nog一直在MainActivity.java:86线上获得NPE。未填写或使用客户的所有字段。但我只是想在我继续之前先让它工作。这是我的密码

主要活动 包com.huntercrm.hunteronline.app.Activities.Main

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.example.hunteronlinebeta.app.R;
import com.huntercrm.hunteronline.app.Activities.Main.Drawer.HunterDrawerItems;
import com.huntercrm.hunteronline.app.Activities.Main.Fragments.AddClientsFragment;
import com.huntercrm.hunteronline.app.Activities.Main.Fragments.Clients;
import com.huntercrm.hunteronline.app.Activities.Main.Fragments.SettingsFragment;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private List<Clients> ClientsArray = new ArrayList<Clients>();


    FragmentManager manager;
    private Drawer result;
    boolean login = true;




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




        populateClientsList();
        populateListView();

        if(!login){
            Intent intent = new Intent(this, LogInActivity.class);
            startActivity(intent);
        }

        HunterDrawerItems hunterDrawerItems = new HunterDrawerItems(manager);
        IDrawerItem[] drawerItemsArray = new IDrawerItem[hunterDrawerItems.size()];
        hunterDrawerItems.toArray(drawerItemsArray);

        result = new DrawerBuilder()
                .withTranslucentStatusBar(false)
                .withActionBarDrawerToggle(false)
                .withActivity(this)
                .addDrawerItems(drawerItemsArray)
                .build();



    }

    //------------------------------------------------------------------------------------------------------------------

    private void populateClientsList(){
        ClientsArray.add(new Clients("oscar", "0616397853", "oscar@gmail.com", "oscar.com", 11111.11, "Akkrum"));
        ClientsArray.add(new Clients("Ricky", "0612345678", "Ricky@gmail.com", "ricky.com", 222222.22, "ergens"));
        ClientsArray.add(new Clients("Jan", "0623456789", "Jan@gmail.com", "jan.com", 333333.33, "nergens"));
        ClientsArray.add(new Clients("Casper", "0687654321", "Casper@gmail.com", "casper.com", 444444.44, "bij jan"));
        ClientsArray.add(new Clients("John", "0698765432", "John@gmail.com", "john.com", 555555.55, "tinboektoe"));
    }



    private void populateListView(){
        ArrayAdapter<Clients> adapter = new MyListAdapter();
        ListView clientList = (ListView) findViewById(R.id.list);
        // NPE happens in line below
        clientList.setAdapter(adapter);

    }

    private class MyListAdapter extends ArrayAdapter<Clients>{
        public MyListAdapter(){
            super(MainActivity.this, R.layout.clients_view, ClientsArray);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View clientsView = convertView;
            if (clientsView == null){
                clientsView = getLayoutInflater().inflate(R.layout.clients_view, parent, false);
            }

            Clients currentClient = ClientsArray.get(position);

            TextView nameText = (TextView)clientsView.findViewById(R.id.text_name);
            nameText.setText(currentClient.getName());

            TextView visitAddressText = (TextView)clientsView.findViewById(R.id.text_visitCity);
            visitAddressText.setText(currentClient.getVisitAddress());

            return clientsView;
        }
    }

    //------------------------------------------------------------------------------------------------------------------

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_drawer:
                result.openDrawer();
                break;
            case R.id.action_settings:
                SettingsFragment sf = new SettingsFragment();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.group, sf, "sfrag");
                transaction.commit();
                break;
            case R.id.action_log_in:
                Intent intent = new Intent(this, LogInActivity.class);
                startActivity(intent);
                break;
            case R.id.action_log_off:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    public void addClient (View v){
        AddClientsFragment acf = new AddClientsFragment();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.group, acf, "acfrag");
        transaction.commit();
    }

    public void clearFields (View v){
        AddClientsFragment acf = new AddClientsFragment();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.group, acf, "acfrag");
        transaction.commit();
    }
}
客户端框架(显示客户端列表)

客户端片段XML

<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"
             tools:context="com.huntercrm.hunteronline.app.Activities.Main.Fragments.ClientsFragment">

    <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/list"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_above="@+id/addClient"/>

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add Client"
            android:id="@+id/addClient"
            android:onClick="addClient"
            android:layout_alignParentBottom="true" android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"/>


</RelativeLayout>

这是布局中
列表视图的id

android:id="@+id/list"
这就是您试图在活动中找到它的方式
onCreate()


id不匹配,您找不到视图,您会得到一个NPE。

这是布局中
列表视图的id

android:id="@+id/list"
这就是您试图在活动中找到它的方式
onCreate()



ID不匹配,你找不到视图,你得到了一个NPE。

你的ListView是空的。看起来
clientsListView
在layout
activity\u main
@shark中不可用,我知道我的ListView是空的,但是提示一下为什么或者如何解决这个问题会很好,尽管要说明这一点,我知道你误解了我。。。您“在尝试设置适配器时不断获取NPE”,我要告诉您,您的listView为null,并且在设置适配器之前必须确保它不为null。说明明显的问题将被标记为“结束->主题外>”为什么这个代码不起作用“。ListView为空与它为空无关。您的ListView为空。看起来在layout
activity\u main
@shark中没有
clientsListView
可用,我知道我的ListView为空,但提示一下为什么或如何修复这一问题会很好,尽管要说明这一点我知道您误解了我。。。您“在尝试设置适配器时不断获取NPE”,我要告诉您,您的listView为null,并且在设置适配器之前必须确保它不为null。说明明显的问题将被标记为“结束->主题外>”为什么这个代码不起作用“。ListView为空与它为空无关。很抱歉,这是我试图自己解决问题时留下的。编辑了我的代码。但这并不能解决问题,在同一台计算机上获得相同的NPElocation@D.Blazer因此,看起来
列表视图
在您的片段中,您正试图从托管活动设置它的适配器。是这样吗?如果是这样,你应该完全改变你的方法。将与
列表视图
-初始化类字段、设置适配器等相关的所有代码移动到实际承载它的片段,并将此片段添加到活动中。现在我甚至找不到将片段添加到视图中的代码。Thx,移动代码修复了我的问题。由于Listview为空,我的自定义适配器仍然无法工作。我到处找。可能是我在读它。但我似乎无法在我的代码中找到使其保持为空的缺陷。像
ListView clientsView=(ListView)getActivity().findviewbyd(android.R.id.list)那样声明它id是
android:id=“@android:id/list”
,但clientsView在这里保持为空。在网上找不到与此问题相关的任何信息specific@D.Blazer您仍在尝试通过活动查找您的
列表视图。这是错误的。您应该将
clientsView
初始化移动到片段中的
onViewCreated()
onCreateView()
方法。这里有膨胀的片段视图,您可以使用
findviewbyd()
找到它。不要忘记将适配器实际设置为列表并将数据放入其中。如果在添加数据后没有显示任何内容,可能是您忘记调用
notifyDataSetChanged()
。很抱歉,这是我自己试图解决问题时留下的。编辑了我的代码。但这并不能解决问题,在同一台计算机上获得相同的NPElocation@D.Blazer因此,看起来
列表视图
在您的片段中,您正试图从托管活动设置它的适配器。是这样吗?如果是这样,你应该完全改变你的方法。将与
列表视图
-初始化类字段、设置适配器等相关的所有代码移动到实际承载它的片段,并将此片段添加到活动中。现在我甚至找不到将片段添加到视图中的代码。Thx,移动代码修复了我的问题。由于Listview为空,我的自定义适配器仍然无法工作。我到处找。可能是我在读它。但我似乎无法在我的代码中找到使其保持为空的缺陷。像
ListView clientsView=(ListView)getActivity().findviewbyd(android.R.id.list)那样声明它id是
android:id=“@android:id/list”
,但clientsView在这里保持为空。在网上找不到与此问题相关的任何信息specific@D.Blazer您仍在尝试通过活动查找您的
列表视图。这是错误的。您应该将
clientsView
初始化移动到片段中的
onViewCreated()
onCreateView()
方法。这里有膨胀的片段视图,您可以使用
findviewbyd()
找到它。不要忘记将适配器实际设置为列表并将数据放入其中。如果添加数据后没有显示任何内容,则可能是您忘记调用
notifyDataSetChanged()
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:text="name will be shown here"
            android:id="@+id/text_name"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="visit address will be shown here"
            android:textSize="20sp"
            android:id="@+id/text_visitCity"
            android:layout_marginTop="25dp"
            android:layout_below="@+id/text_name"
            android:layout_alignParentEnd="true"/>
</RelativeLayout>
02-22 14:24:56.730    9638-9638/com.example.hunteronlinebeta.app D/AndroidRuntime﹕ Shutting down VM
02-22 14:24:56.730    9638-9638/com.example.hunteronlinebeta.app E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.hunteronlinebeta.app, PID: 9638
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hunteronlinebeta.app/com.huntercrm.hunteronline.app.Activities.Main.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.huntercrm.hunteronline.app.Activities.Main.MainActivity.populateListView(MainActivity.java:86)
at com.huntercrm.hunteronline.app.Activities.Main.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
android:id="@+id/list"
clientlist = (ListView) findViewById(R.id.clientsListView)