下面是我的应用程序,我想要做的是拥有一个包含图像的列表,并能够过滤列表,并且可以点击已过滤的列表。当我使用mainActivity及其content_main.xml时,它工作正常。但我决定使用Fragments,现在它停止工作了。我所做的是创建一个名为HomePage.java的新片段,这样我就可以在其中一个片段上显示listview,然后使用其他片段来做其他的事情。但它一直在崩溃,我无法弄清楚原因。
这是正在运行的过程:
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
W/ResourceType: Skipping entry 0x105007e in package table 0 because it is not complex!
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
D/dalvikvm: GC_CONCURRENT freed 186K, 9% free 8061K/8775K, paused 12ms+14ms, total 47ms
D/AbsListView: Get MotionRecognitionManager
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41ddd2a0)
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.jermaine.jutc.HomePage.onCreateView(HomePage.java:144)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
I/Process: Sending signal. PID: 3522 SIG: 9
Application terminated.
这是包含imageView和textView:
的xml文件<?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">
<ImageView
android:id="@+id/list_icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="12dp"
android:src="@drawable/eighta"/>
<TextView
android:id="@+id/list_letters"
android:text="NAGGO HEAD"
android:layout_below="@+id/icons"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_toRightOf="@+id/list_icons"/>
<TextView
android:id="@+id/list_description"
android:text="Garveymead, Weschester,Portmore Toll, Hagle Park, "
android:layout_below="@+id/list_letters"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_toRightOf="@+id/list_icons"/>
<TextView
android:id="@+id/bus_number"
android:text="8A "
android:visibility="invisible"
android:layout_toEndOf="@+id/list_letters"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_toRightOf="@+id/list_icons"/>
</RelativeLayout>
此xml文件具有Listview:
<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.example.jermaine.jutc.HomePage">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.SearchView
android:id="@+id/txtsearch"
android:queryHint="Enter bus number"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/txtsearch"
android:id="@+id/my_listview"
android:paddingTop="5dp"
android:dividerHeight="10dp"
android:divider="#ffffff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
</RelativeLayout>
以下是我的列表适配器并过滤列表:
package com.example.jermaine.jutc;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import static android.R.attr.filter;
/**
* Created by jermaine on 2/17/17.
*/
public class ListAdapter extends BaseAdapter implements Filterable {
/*private int imagess[];
private String lettersss[];
private String description[];
private String bus_number[];*/
private Context context;
ArrayList<Route> routes;
CustomFilter searchFilter; // to filter the results from search
ArrayList<Route> routeFilter;// new array to contain filter results
//CustomFilter searchFilter;
/*int imageFilter[];
String letterFilter[];
String desFilter[];
String numberFilter[];*/
private LayoutInflater inflater;
public ListAdapter(Context ctx, ArrayList<Route> routes) {
this.context = ctx;
this.routes = routes;
this.routeFilter = routes;
//this.context = context;
/*this.imagess = imagess;
this.lettersss = lettersss;
this.description = description;
this.bus_number = bus_number;
this.imageFilter = imagess;
this.letterFilter = lettersss;
this.desFilter = description;
this.numberFilter =bus_number;*/
}
@Override
public int getCount() {
//return bus_number.length;
return routes.size();
}
@Override
public Object getItem(int position) {
//return bus_number[position];
return routes.get(position);
}
@Override
public long getItemId(int position) {
return routes.indexOf(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid = convertView;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = inflater.inflate(R.layout.list_item, null);
}
ImageView iconss = (ImageView) grid.findViewById(R.id.list_icons);
TextView letterss = (TextView) grid.findViewById(R.id.list_letters);
TextView desss = (TextView) grid.findViewById(R.id.list_description);
TextView buses = (TextView) grid.findViewById(R.id.bus_number);
// set data to them
iconss.setImageResource(routes.get(position).getImage());
letterss.setText(routes.get(position).getTitle());
desss.setText(routes.get(position).getDescription());
buses.setText(routes.get(position).getRoutes());
return grid;
}
@Override
public Filter getFilter() {
if(searchFilter == null){
searchFilter = new CustomFilter();
}
return searchFilter;
}
//inner class to hangle what being filtered
class CustomFilter extends Filter{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if(constraint !=null && constraint.length()>0){
//contraint to uppercase
constraint = constraint.toString().toUpperCase();
ArrayList<Route> filters = new ArrayList<Route>();
// get specific items
for(int i =0; i<routeFilter.size(); i++){
//if what the user types matches in the filter list
if(routeFilter.get(i).getRoutes().toUpperCase().contains(constraint)){
Route r = new Route(routeFilter.get(i).getRoutes(), routeFilter.get(i).getTitle(),routeFilter.get(i).getDescription(), routeFilter.get(i).getImage());
filters.add(r);
}
}
results.count = filters.size();
results.values = filters; //filters array list "routeFilter"
}else {
results.count = routeFilter.size();
results.values = routeFilter;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
routes = (ArrayList<Route>) results.values;
notifyDataSetChanged();//refresh the list view
}
}
}
这是新的Fragement.java我想在列表中显示:
package com.example.jermaine.jutc;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class HomePage extends ListFragment {
ListView lv; // creating a list view variable in main activity
SearchView sv; // declaring a search text box
String busNumber[] ={"1", "1A", "3", "3A",
"3AX", "4", "6", "8",
"8A", "8AX", "12", "12A",
"16", "16A", "16B", "16BX",
"17", "17A", "17AX", "17EX",
"18", "18A", "19AX", "20",
"20A", "20AX", "42", "53",
"54", "101", "103", "121",
"123", "124", "125", "126",
"127", "128", "129", "130",
"295", "301", "303", "308",
"312", "314", "315", "316",
"317", "318", "319", "320",
};
String letterList[] ={"Hellshire To City", "Hellshire To HWT", "Christian Garden To City", "Christian Garden To HWT",
"CHRISTIAN GARDEN", "GREATER PORTMORE", "NAGGO HEAD", "NAGGO HEAD",
"NAGGO HEAD", "NAGGO HEAD", "NAGGO HEAD", "NAGGO HEAD",
"GREATER PORTMORE", "GREATER PORTMORE", "GREATER PORTMORE", "GREATER PORTMORE",
"GREATER PORTMORE", "GREATER PORTMORE", "GREATER PORTMORE", "GREATER PORTMORE",
"GREATER PORTMORE", "GREATER PORTMORE", "GREATER PORTMORE", "GREATER PORTMORE",
"GREATER PORTMORE", "GREATER PORTMORE", "CONSTANT SPRING", "ABOVE ROCKS",
"BORDER", "GREATER PORTMORE", "SPANISH TOWN", "INSWOOD VILLAGE",
"ENSOME ACRES", "EBONY VALE", "GREEN ACRES", "WHITE WATERS",
"ANGELS/ELTHAM", "ANGELS", "ANGELS/ELTHAM", "LONGVILLE",
"HARBOUR VIEW", "HELLSHIRE", "CHRISTIAN GARDENS", "NAGOO HEAD",
"CUMBERLAND", "CUMBERLAND", "CITY", "GREATER PORTMORE",
"GREATERR PORTMORE", "GREATERR PORTMORE", "GREATERR PORTMORE", "GREATERR PORTMORE"
}; // array for the bus title
String desList[] ={"(via)", "(via)Naggo Head->Dawkins Dr->Three Miles", "(via)Waterford->Dawkins Dr->Portmore Toll->Marcus Garvey Dr", "NAGGO HEAD",
"CHRISTIAN GARDEN", "GREATER PORTMORE", "NAGGO HEAD", "NAGGO HEAD",
"NAGGO HEAD", "NAGGO HEAD", "NAGGO HEAD", "NAGGO HEAD",
"GREATER PORTMORE", "GREATER PORTMORE", "GREATER PORTMORE", "GREATER PORTMORE",
"GREATER PORTMORE", "GREATER PORTMORE", "GREATER PORTMORE", "GREATER PORTMORE",
"GREATER PORTMORE", "GREATER PORTMORE", "GREATER PORTMORE", "GREATER PORTMORE",
"GREATER PORTMORE", "GREATER PORTMORE", "CONSTANT SPRING", "ABOVE ROCKS",
"BORDER", "GREATER PORTMORE", "SPANISH TOWN", "INSWOOD VILLAGE",
"ENSOME ACRES", "EBONY VALE", "GREEN ACRES", "WHITE WATERS",
"ANGELS/ELTHAM", "ANGELS", "ANGELS/ELTHAM", "LONGVILLE",
"HARBOUR VIEW", "HELLSHIRE", "CHRISTIAN GARDENS", "NAGOO HEAD",
"CUMBERLAND", "CUMBERLAND", "CITY", "GREATER PORTMORE",
"GREATERR PORTMORE", "GREATERR PORTMORE", "GREATERR PORTMORE", "GREATERR PORTMORE"
};// array for the bus route
int lettersIcon[] = {R.drawable.one, R.drawable.onea, R.drawable.three, R.drawable.threea,
R.drawable.threeax, R.drawable.four, R.drawable.six, R.drawable.eight,
R.drawable.eighta, R.drawable.eightax, R.drawable.twelve, R.drawable.twelvea,
R.drawable.sixteen, R.drawable.sixteena, R.drawable.sixteenb, R.drawable.sixteenbx,
R.drawable.seventeen, R.drawable.seventeena, R.drawable.seventeenax, R.drawable.seventeenex,
R.drawable.eighteen, R.drawable.eighteenax, R.drawable.nineteenax, R.drawable.twenty,
R.drawable.twentya, R.drawable.twentyax, R.drawable.fourtwo, R.drawable.fivethree,
R.drawable.fivefour, R.drawable.oneoone, R.drawable.oneothree, R.drawable.onetwoone,
R.drawable.onetwothree, R.drawable.onetwofour, R.drawable.onetwofive, R.drawable.onetwosix,
R.drawable.onetwoseven, R.drawable.onetwoeight, R.drawable.onetwonine, R.drawable.onethreezero,
R.drawable.twoningfive, R.drawable.threeoone, R.drawable.threeothree, R.drawable.threeoeight,
R.drawable.threetwelve, R.drawable.threefourteen, R.drawable.threefifteen, R.drawable.threesixteen,
R.drawable.threeseventeen, R.drawable.threeeighteen, R.drawable.threenineteen, R.drawable.threetwenty
}; // array of image bus numbers
private ArrayList<Route> getRoutes()
{
ArrayList<Route> routes = new ArrayList<>();
Route r;
for(int i=0; i<busNumber.length; i++)
{
r = new Route(busNumber[i], letterList[i], desList[i], lettersIcon[i]);
routes.add(r);
}
return routes;
}//to fill the array with values from defined arrays
public HomePage() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View homeView ;
homeView= inflater.inflate(R.layout.fragment_home_page, container, false);
//assigning a my_listview (listview) located in my_listview.xml file
lv = (ListView)getActivity().findViewById(R.id.my_listview);
sv = (SearchView)getActivity().findViewById(R.id.txtsearch);
// declaring a new ListAdapter and assigning the arrays in it
final ListAdapter adapter = new ListAdapter(getActivity(),getRoutes());
lv.setAdapter(adapter);
// Inflate the layout for this fragment
return homeView;
}
}
答案 0 :(得分:0)
您的列表视图和搜索视图位于主视图中,因此您应该调用主视图来查找它们:
lv = (ListView)homeView.findViewById(R.id.my_listview);
sv = (SearchView)homeView.findViewById(R.id.txtsearch);