提问人:deeko 提问时间:4/13/2023 更新时间:4/13/2023 访问量:36
尝试将 linkedlist 传递给 fragment 并对其进行任何操作时,Android 应用程序崩溃
Android app crashing when attempting to pass linkedlist to fragment and do anything with it
问:
我正在尝试在列表视图中显示这些对象的字段,该列表视图正在工作并且位于片段中。我的问题是我拥有的任何示例都有效,它似乎是针对数组列表而不是链接列表。然后,我将转换为 arraylist 以 puplate listview 或找到一种使用 linkedlist 的方法。
在我的代码中,我创建了一个新的链表,并在mainactivity中填充了对象。我在mainactivity中有一个getter来检索列表。我有一个片段列表视图(在解析数据并填充 linkedlist 后创建)。我正在尝试用这些对象的字段填充列表视图项,但不知道如何做到这一点。我正处于一个阶段,在我尝试对链表做任何事情之前,我无法以任何方式访问它,而不会我的应用程序崩溃。
如下图所示,我正在创建和填充我的链表等,然后在 listviewfragment 中尝试访问它,并最终使用/更改它来填充我的列表视图。请原谅代码的混乱,我一直在尝试多种不同的方法来做到这一点,并且每次启动时崩溃时都会遇到相同的问题,没有错误消息。
据我了解,我正在我的片段中创建一个项目列表,该列表被设置为主活动返回的列表,并且我尝试了简单的每个循环来获取描述,但无论我对任何代码段进行什么更改,应用程序都会在启动时立即崩溃,没有任何错误消息。
MainActivity 公共类 MainActivity 扩展 AppCompatActivity 实现 OnClickListener { 私有 TextView rawDataDisplay;
private Button startButton;
private String result = "";
private String url1="http://www.bgs.ac.uk/images/logos/bgs_c_w_227x50.gif";
private String[] items;
Map<String, List<ItemClass>> mylist;
LinkedList <ItemClass> alist = null;
ItemClass[] arrayofitems = null;
List <ItemClass> Testlist = null;
List<ItemClass> list = null;
ItemClass item = null;
private String urlSource="http://quakes.bgs.ac.uk/feeds/MhSeismology.xml";
private IncidentFragment incidentfragment;
private DateRefineFragment daterefinefragment;
private SearchFragment searchfragment;
private ViewFragment viewfragment;
//private LogoFragment logofragment;
private ListviewFragment listviewfragment;
private ListtextviewFragment listtextviewfragment;
private BtntextviewFragment btntextviewfragment;
@Override
protected void onCreate(Bundle savedInstanceState)
{
LinkedList <ItemClass> alist = null;
items = new String[50];
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startProgress();//Parse data and prepare for user selection on startup of app
// Set up the raw links to the graphical components
daterefinefragment = new DateRefineFragment();
searchfragment = new SearchFragment();
viewfragment = new ViewFragment();
listviewfragment = new ListviewFragment();
listtextviewfragment = new ListtextviewFragment();
btntextviewfragment = new BtntextviewFragment();
//fragments
FragmentManager manager1 = getSupportFragmentManager();
FragmentTransaction transaction1 = manager1.beginTransaction();
transaction1.replace(R.id.listtextviewFragment, listtextviewfragment);
transaction1.commit();
FragmentManager manager2 = getSupportFragmentManager();
FragmentTransaction transaction2 = manager2.beginTransaction();
transaction2.replace(R.id.btntextviewFragment, btntextviewfragment);
transaction2.commit();
FragmentManager manager3 = getSupportFragmentManager();
FragmentTransaction transaction3 = manager3.beginTransaction();
transaction3.replace(R.id.daterefineFragment, daterefinefragment);
transaction3.commit();
FragmentManager manager4 = getSupportFragmentManager();
FragmentTransaction transaction4 = manager4.beginTransaction();
transaction4.replace(R.id.searchFragment, searchfragment);
transaction4.commit();
FragmentManager manager5 = getSupportFragmentManager();
FragmentTransaction transaction5 = manager5.beginTransaction();
transaction5.replace(R.id.viewFragment, viewfragment);
transaction5.commit();
FragmentManager manager6 = getSupportFragmentManager();
FragmentTransaction transaction6 = manager6.beginTransaction();
transaction6.replace(R.id.listviewFragment, listviewfragment);
transaction6.commit();
// More Code goes here
}
public void onClick(View aview) //probs dont need this with button fragments
{
//startProgress();
}
public void startProgress()
{
// Run network access on a separate thread;
new Thread(new Task()).start();
} //
// Need separate thread to access the internet resource over network
// Other neater solutions should be adopted in later iterations.
private class Task implements Runnable
{
@Override
public void run()
{
AccessWeb aw = new AccessWeb();//Create object to make use of class
result = aw.getResult(urlSource);//Runs code within class to retrieve resulting string of data
new Thread(new dataparsing()).start();//runs parsing of data on seperate thread
}
}
public class dataparsing implements Runnable
{
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void run() { //creates new ParseData object and sets alist to returned templist.
ParseData dp = new ParseData();
alist = dp.parseData(result);
//arrayofitems = alist.toArray();
list = new ArrayList<ItemClass>(alist);
//Log.d("UI thread", "ARRAYOFITEMS " + arrayofitems[0].);
mylist = alist.stream().collect(Collectors.groupingBy(ItemClass::getlocation)); //Set mylist to returned list grouped by location
Testlist = mylist.get("HIGHLAND");//set test list to list returned by get with location of passed string ("GWENT")
item = Testlist.get(1); //set item to object returned in first index of previously returned list (Testlist)
Log.d("UI thread", "List test = " + list.get(0) + " " + item.getdescription());//prints location of first item in
// previously returned list
}
}
public void loggingMethod() //Method used for logging information.
{
Log.d("UI thread", "I am the UI thread");
//rawDataDisplay.setText(result); doesnt work as its not being run in main thread
Log.d("UI thread", "first in list printed is " + alist.getFirst() + " and last in list printed is " + alist.getLast());
Log.d(“UI 线程”, “url1 是” + url1);呜呜呜创建一个新项目来获取这个工人 }
public List<ItemClass> getList() {
return alist;
}
Listview Fragment 公共类 ListviewFragment 扩展了 Fragment 实现 AdapterView.OnItemClickListener、View.OnClickListener {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
)
{
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.frag_listviewfragment, container, false);
return v;
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
ListView listView = (ListView) view.findViewById(R.id.list);
listView.setOnItemClickListener((AdapterView.OnItemClickListener) this);
List<ItemClass> items = ((MainActivity)getActivity()).getList();
for(ItemClass i : items){
Log.d("UI thread", "I am the UI thread " + i.getdescription());
}
//UserAdapter<ItemClass> adapter = new UserAdapter<ItemClass>(users);
}
答:
您的应用程序崩溃似乎来自 getList() 值。
它似乎在开始时返回 null。
请尝试设置回调方法,以便在 alist 值不是 null 值时通知 ListviewFragment 类。
评论