Java I';我在编写一个片段中的Listview时遇到了麻烦
这是FirstFragment.java文件Java I';我在编写一个片段中的Listview时遇到了麻烦,java,android,listview,android-fragments,Java,Android,Listview,Android Fragments,这是FirstFragment.java文件 package com.example.mirae01.bmsmessenger; import java.util.ArrayList; public class PageOneFragment extends Fragment { ListView myList; String friendName ="James", friendSentence = "Good day."; Fadapter my
package com.example.mirae01.bmsmessenger;
import java.util.ArrayList;
public class PageOneFragment extends Fragment {
ListView myList;
String friendName ="James", friendSentence = "Good day.";
Fadapter myApt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_page_one, container, false);
myApt = new Fadapter(getActivity().getApplicationContext());
myList = (ListView)rootView.findViewById(R.id.listviewFriend);
myApt.addArray(friendName, friendSentence);
return rootView;
}
public static PageOneFragment newInstance(){ //탭 기능구현 관련 클래스
Bundle args = new Bundle();
PageOneFragment fragment = new PageOneFragment();
fragment.setArguments(args);
return fragment;
}
}
上面是Firstfragment.xml文件。
包com.example.mirae01.bmsmessenger;
导入android.content.Context;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.ImageView;
导入android.view.LayoutInflater;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类Fadapter扩展BaseAdapter{
类MyItem{//변수의 묶음방식 생성
字符串friendName,Friend句子;
//位图btmFriendImg//이미지 구현은 나중에 하자
}
private ArrayList arrItem=新ArrayList();
语境;
公共音量调节器(上下文c){
上下文=c;
}
@凌驾
public int getCount(){
返回arrItem.size();
}
@凌驾
公共对象getItem(int i){
返回项目。获取(i);
}
@凌驾
公共长getItemId(int i){
返回i;
}
@凌驾
公共视图getView(int i、视图转换视图、视图组父视图){
context=parent.getContext();
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.layout.friendview,父级,false);
}
ImageView friendImg=(ImageView)convertView.findViewById(R.id.friendImg);//화면에 표시될 视图(布局)이 膨胀된)으로부터 위젯에 대한 참조 획득
TextView txFriendName=(TextView)convertView.findViewById(R.id.txFriendName);
TextView txFriendSentence=(TextView)convertView.findViewById(R.id.txFriendSentence);
MyItem item=arrItem.get(i);//arrItem에서 我번째 값들을 받아서 我的项目방식의 项目에 저장.
friendImg.setImageResource(R.drawable.ic_launcher_前台)//나중에 이미지를 서버에서 받는 법을 배워서 수정하자!!
txFriendName.setText(item.friendName);
txFriendSentence.setText(item.friendstence);
返回视图;
}
public void addArray(String AddfriendName,String addFriendSequence){//예시로 만든, 에 값을 넣어주는 클래스
MyItem tmpItem=新的MyItem();
tmpItem.friendName=AddfriendName;
tmpItem.friendstence=添加friendstence;
添加项目(tmpItem);
}
}
这是适配器Java文件
我制作了
MainActivity.java
文件和XML,制作了3个片段java文件和tab函数,并在第一个片段中插入了widget.ListView
,制作了java文件(用于适配器函数),但我将小部件.listView
放入第一个片段时出错。我如何解决此问题?您的错误是什么?您能提供它吗?我可以在AVD中激活此代码,但我的应用程序在访问MenuSelectActivity时关闭。在这种情况下,我可以看到错误代码吗?E/AndroidRuntime:FATAL EXCEPTION:main进程:com.example.mirae01.bmsmessenger,PID:20435 android.content.res.Resources$NotFoundException:Resource ID#0x7f070064-这是我的错误代码。
package com.example.mirae01.bmsmessenger;
import java.util.ArrayList;
public class PageOneFragment extends Fragment {
ListView myList;
String friendName ="James", friendSentence = "Good day.";
Fadapter myApt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_page_one, container, false);
myApt = new Fadapter(getActivity().getApplicationContext());
myList = (ListView)rootView.findViewById(R.id.listviewFriend);
myApt.addArray(friendName, friendSentence);
return rootView;
}
public static PageOneFragment newInstance(){ //탭 기능구현 관련 클래스
Bundle args = new Bundle();
PageOneFragment fragment = new PageOneFragment();
fragment.setArguments(args);
return fragment;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".PageOneFragment">
<!-- <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="친구 정보"
android:textSize="15dp"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
/>-->
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listviewFriend">
</ListView>
</LinearLayout>
this above is Firstfragment.xml file.
package com.example.mirae01.bmsmessenger;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.view.LayoutInflater;
import android.widget.TextView;
import java.util.ArrayList;
public class Fadapter extends BaseAdapter {
class MyItem{ //변수의 묶음방식 생성
String friendName, friendSentence;
//Bitmap btmFriendImg; //이미지 구현은 나중에 하자
}
private ArrayList<MyItem> arrItem = new ArrayList<>();
Context context;
public Fadapter(Context c) {
context = c;
}
@Override
public int getCount() {
return arrItem.size();
}
@Override
public Object getItem(int i) {
return arrItem.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View convertView, ViewGroup parent) {
context = parent.getContext();
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.friendview,parent, false);
}
ImageView friendImg = (ImageView)convertView.findViewById(R.id.friendImg); // 화면에 표시될 View(Layout이 inflate된)으로부터 위젯에 대한 참조 획득
TextView txFriendName = (TextView)convertView.findViewById(R.id.txFriendName);
TextView txFriendSentence = (TextView)convertView.findViewById(R.id.txfriendSentence);
MyItem item = arrItem.get(i); //arrItem에서 i번째 값들을 받아서 MyItem 방식의 item에 저장.
friendImg.setImageResource(R.drawable.ic_launcher_foreground); //나중에 이미지를 서버에서 받는 법을 배워서 수정하자!!
txFriendName.setText(item.friendName);
txFriendSentence.setText(item.friendSentence);
return convertView;
}
public void addArray(String AddfriendName, String AddfriendSentence){ //예시로 만든, 에 값을 넣어주는 클래스
MyItem tmpItem = new MyItem();
tmpItem.friendName = AddfriendName;
tmpItem.friendSentence = AddfriendSentence;
arrItem.add(tmpItem);
}
}