NavigationDrawer中的Listview

时间:2017-05-28 10:20:30

标签: android listview navigation-drawer

任何人都可以帮我解决这个错误。我想要导航抽屉中的列表视图,但它会引发错误。

错误

public static void main(String args[])
{
    System.setProperty("webdriver.gecko.driver", "D:\\Eclipse and workspace\\eclipse\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();

    driver.get("https://www.amazon.in/");

    WebElement elem = driver.findElement(By.id("twotabsearchtextbox"));

    elem.sendKeys("Camera DSLR");

    driver.findElement(By.className("nav-input")).click();


    WebDriverWait wait = new WebDriverWait(driver,10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='result_0']/div/div/div/div[2]/div[3]/div[2]/div[1]/span/span/a/i[1]/span")));

    WebElement elem2 = driver.findElement(By.xpath(".//*[@id='result_0']/div/div/div/div[2]/div[3]/div[2]/div[1]/span/span/a/i[1]/span"));
    elem2.getText();

}

我将我的代码放在这里。

这是主要活动

DummyActivity.java

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.example.android.logincoupon.DummyActivity.onCreate(DummyActivity.java:29)
                                                                                         at android.app.Activity.performCreate(Activity.java:6251)
                                                                                         at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
                                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:148) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5461) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

activity_dummy.xml

package com.example.android.logincoupon;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import com.example.android.logincoupon.adapter.NavlistAdapter;
import com.example.android.logincoupon.utils.NavList;

import java.util.ArrayList;

public class DummyActivity extends AppCompatActivity {
DrawerLayout drawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dummy);
    drawerLayout=(DrawerLayout) findViewById(R.id.drwar);

    final ArrayList<NavList> navListsitem = new ArrayList<NavList>();
    navListsitem.add(new NavList(R.drawable.ic_manicon, "Person"));
    navListsitem.add(new NavList(R.drawable.ic_toys_black_24dp, "Toys"));
    navListsitem.add(new NavList(R.drawable.ic_call_black_24dp, "Contacts"));
    navListsitem.add(new NavList(R.drawable.ic_transfer_within_a_station_black_24dp,"Transfer"));
    NavlistAdapter adapter = new NavlistAdapter(this, R.layout.navlist_item, navListsitem);
    ListView listview = (ListView) findViewById(R.id.list_nav_item);
    listview.setAdapter(adapter);
}

}

nav_header_navigationdrawer.xml //用于导航抽屉项目

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout android:id="@+id/drwar"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">


<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        android:gravity="center_vertical"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello"
        />
</LinearLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_navigationdrawer"
    />


</android.support.v4.widget.DrawerLayout>

navlist_item.xml //用于保存导航抽屉列表视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"

    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@android:drawable/sym_def_app_icon" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Android Studio"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"         />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android.studio@android.com" />


</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list_nav_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>


</LinearLayout>
</LinearLayout>

适配器类 NavlistAdapter.java

<?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"
    android:padding="8dp"
    android:id="@+id/navlist">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/icon_img"

        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt_list"
        android:layout_toRightOf="@+id/icon_img"
        android:layout_margin="4dp"/>

</RelativeLayout>

NavList.java

package com.example.android.logincoupon.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.android.logincoupon.R;
import com.example.android.logincoupon.utils.NavList;

import java.util.List;

public class NavlistAdapter extends ArrayAdapter<NavList>  {
    List<NavList> navLists;
    Context context;
    int layoutResID;


    public NavlistAdapter(@NonNull Context context, @LayoutRes int layoutResID, @NonNull List<NavList> objects) {
        super(context, layoutResID, objects);
        this.context=context;
        this.layoutResID=layoutResID;
        this.navLists=objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listViewView;
        listViewView=convertView;
        if(listViewView==null) {
            listViewView = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.navlist_item, parent, false);
        }
            NavList navlist=getItem(position);
            TextView listTv=(TextView) listViewView.findViewById(R.id.txt_list);
            listTv.setText(navlist.getWord());
            ImageView listImg=(ImageView) listViewView.findViewById(R.id.icon_img);
            listImg.setImageResource(navlist.getId());

        return listViewView;
    }
}

0 个答案:

没有答案