我正在从事基于 Fragment 的学校项目。我已经制作了 XML 文件,但在 nearfragment.java 中出现错误。你能告诉我错误在哪里吗,因为我熟悉 Activity 但不熟悉 Fragment。请帮助我纠正错误。
near_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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="{relativePackage}.${activityClass}" >
<ListView
android:id="@+id/list_li"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
near_layout_mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon_list"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/item_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33" />
<TextView
android:id="@+id/textView1_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
CustomListNearAdapter.java
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListNearAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
public CustomListNearAdapter(Activity context, String[] itemname, Integer[] imgid) {
super(context, R.layout.near_layout_mylist, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.near_layout_mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item_list);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon_list);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1_list);
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Description "+itemname[position]);
return rowView;
};
}
近 fragment
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;
/**
* Created by Ratan on 7/29/2015.
*/
public class NearFragment extends Fragment {
ListView list;
String[] itemname ={
"Safari",
"Camera",
"Global",
"FireFox",
"UC Browser",
"Android Folder",
"VLC Player",
"Cold War"
};
Integer[] imgid={
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.near_layout, container,false);
CustomListNearAdapter adapter=new CustomListNearAdapter(this, itemname, imgid);
ListView listView = (ListView) view.findViewById(R.id.list_item);
listView.setAdapter(new CustomListNearAdapter(getActivity()));
return view;
}
}
错误在nearfragment,我没法写出fragment的代码。所以请给我一些指示。
最佳答案
在您的代码中,您在设置 adapter 时没有将正确的值传递给构造函数,这意味着在您的代码中的行。
listView.setAdapter(new CustomListNearAdapter(getActivity()));
上面一行有问题,所以用下面几行代码替换。
CustomListNearAdapter adapter=new CustomListNearAdapter(getActivity(), itemname, imgid);
ListView listView = (ListView) view.findViewById(R.id.list_li);
listView.setAdapter(adapter);
关于带 fragment 的 Android ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35739037/
我一直在尝试使用Rails的缓存功能,但我无法使某些缓存片段过期,尽管它们似乎已过期。使用Rails教程站点中指出的“俄罗斯娃娃缓存”,我正在使用此配置我在release_controller.rbController中使外部缓存过期,我在其中使用expire_fragment("all_available_releases")使片段过期。我在更新、删除或添加条目的Controller的每个方法中都使用它。这是WEBrick的日志,尽管过期片段已在其中注册,但5行后过期片段被读取并使用,而这是不应该的。这个例子是在destroy调用之后。ProcessingbyReleasesCont
我几乎什么都试过了,但似乎无法使用来自模型的expire_fragment?我知道你不应该这样做非MVC,但肯定有很多方法可以做到。我在lib/cache_helper.rb中用我所有的过期助手创建了一个模块,在每个中只是一堆expire_fragment调用。我有我所有的/app/sweepers下的缓存清扫器设置,并有一个“includeCacheHelper”在我的应用程序Controller中,因此在通过Controller调用时应用程序工作正常。然后事情是我有一些外部守护进程,尤其是一些循环的cron任务调用一个调用特定任务的rake任务方法。此方法进行一些处理并将条目输入到
我正在使用three.js运行资源匮乏的片段着色器。我已将渲染大小设置为800*600,以保持着色器即使在低端卡上也能流畅运行。我正在这样设置我的渲染Canvas:varcanvas1=document.getElementById('canvas1');renderer=newTHREE.WebGLRenderer(canvas1);renderer.setSize(800,600);renderer.autoClear=false;document.body.appendChild(renderer.domElement);在body元素上我有然后我在cssheader中做widt
当我点击fragment中的线性布局时,它不会给出任何错误,但不会开始新的Activity。我的代码有什么错误吗?请有人帮助我。我的项目的这个fragmentxml文件代码。Fragment_one.xml我的项目的这段java类代码。FragmentOne.javainfo.androidhive.listviewfeed;importandroid.app.Fragment;importandroid.content.Intent;importandroid.os.Build;importandroid.os.Bundle;importandroid.view.LayoutInfl
我在tv.setMovementMethod(newScrollingMovementMethod());上遇到了NullPointerException但我完全不知道为什么!我已经在XML文件中声明了textView并在onCreateView中对其进行了初始化。任何帮助,将不胜感激。这里是java代码的相关部分:publicclassTestConnectionextendsFragment{publicTestConnection(){}JSONParserjParser=newJSONParser();//buttonpressedtogettheitemsButtongetA
我使用GoogleMapsAndroidv1API创建了我的应用程序的第一个版本。但是现在当我发布第二个版本的应用程序时,谷歌地图停止工作。我认为这是因为它已被弃用。所以现在我正在尝试创建示例Android应用程序以使用链接使用Googlemaphttps://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2但是我遇到了强制关闭错误FATALEXCEPTION:mainjava.lang.RuntimeException:Unabletostarta
我正在尝试开发一个时间线,就像下面的facebook一样。你看,我有一个ViewPager可以在“fragment”(如提要、个人资料、关于等)之间导航用户。我有“main.xml”,它上面只有viewpager;我还有fragmenta.xml、fragmentb.xml、fragmentc.xml。让我们检查一下fragmenta.xml,我把它放在里面;并制作了singlerow.xml来将这些框创建成这样的时间线;我让它只使用一个静态框。但是当我尝试运行这个结构时,我在Debug模式下遇到错误“找不到来源编辑源查找路径”在我尝试像这样将我的适配器和其他关于listview的东西
所以我尝试在我的应用程序中创建一个fragment,其中包含使用appcompatActivity的工具栏(作为操作栏),以便我可以在多个View中重复使用它。我已经使用仅扩展fragment的类测试了一个简单的纯文本fragment并使其正常工作,但要包含一个工具栏,我需要在View中扩展AppCompatActivity(我认为).当我尝试让它工作时,我收到一条错误消息,指出我的action_bar_fragment不是Fragment。我觉得我在这里缺少一些基本的东西。有什么想法吗?主要Activity:packagecom.example.aaron.personaldataa
我试图让我的fragment出现在我的手机上,但由于某种原因它不会这样做。然而,当在平板电脑上加载相同的东西时,它看起来很正常。有人知道我的代码有什么问题以及如何解决这个问题吗?所有相关帮助将不胜感激。类(class)链接FragmentWCLine.javaWCBankActivity.javaFragmentWCBank.javaWCBankViewPagerAdapter.java布局链接fragment_wc_line.xmlfragment_wc_bank.xml 最佳答案 在FragmentWCBank中,您应该使用子f
我正在尝试添加MaterialSearchView在fragment中使用自定义工具栏。所以首先我为它创建了xml布局。search_view.xml然后我添加了菜单项..然后在fragment类中我添加了自定义操作栏。经过测试,代码运行良好。但是当我添加MaterialSearchView时..应用程序停止响应..这是我的java代码..publicclasssearchViewextendsFragment{Toolbarsearch;MaterialSearchViewsearch_view;@Nullable@OverridepublicViewonCreateView(Lay