我希望horizontalListview具有垂直滚动功能。之前我曾尝试使用Listview内的Horizontalscrollview实现它。它适用于静态否。数据项。但就我而言,它们是动态的。 所以我从Here
下载了 HorizontalListView 的库(代码)现在我将HorizontalListViewDemo活动变成了一个片段,并在另一个活动中调用它。我的目标是在垂直滚动视图的活动中多次调用它。 但我尝试使用1 Fragment和应用程序崩溃。 LogCat在HorizontalListViewDemo片段上的Layoutinflator中显示错误。
HorizontalListViewDemo
public class HorizontalListViewDemo extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.listviewdemo, container, false);
HorizontalListView listview = (HorizontalListView)view.findViewById(R.id.listview);
listview.setAdapter(mAdapter);
return view;
}
/* @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewdemo);
HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);
listview.setAdapter(mAdapter);
}*/
private static String[] dataObjects = new String[]{ "Text #1",
"Text #2",
"Text #3",
"Text #4" };
private BaseAdapter mAdapter = new BaseAdapter() {
private OnClickListener mOnButtonClicked = new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "clicked",Toast.LENGTH_LONG).show();
/*
AlertDialog.Builder builder = new AlertDialog.Builder(HorizontalListViewDemo.this);
builder.setMessage("hello from " + v);
builder.setPositiveButton("Cool", null);
builder.show();*/
}
};
@Override
public int getCount() {
return dataObjects.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
TextView title = (TextView) retval.findViewById(R.id.title);
Button button = (Button) retval.findViewById(R.id.clickbutton);
button.setOnClickListener(mOnButtonClicked);
title.setText(dataObjects[position]);
return retval;
}
};
}
任何帮助都将不胜感激。
logcat的
06-15 01:34:37.139: D/AndroidRuntime(300): Shutting down VM
06-15 01:34:37.139: W/dalvikvm(300): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-15 01:34:37.219: E/AndroidRuntime(300): FATAL EXCEPTION: main
06-15 01:34:37.219: E/AndroidRuntime(300): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tryntry/com.example.tryntry.Struct}: android.view.InflateException: Binary XML file line #10: Error inflating class HorizontalListView
06-15 01:34:37.219: E/AndroidRuntime(300): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.os.Handler.dispatchMessage(Handler.java:99)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.os.Looper.loop(Looper.java:123)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-15 01:34:37.219: E/AndroidRuntime(300): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 01:34:37.219: E/AndroidRuntime(300): at java.lang.reflect.Method.invoke(Method.java:521)
06-15 01:34:37.219: E/AndroidRuntime(300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-15 01:34:37.219: E/AndroidRuntime(300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-15 01:34:37.219: E/AndroidRuntime(300): at dalvik.system.NativeStart.main(Native Method)
06-15 01:34:37.219: E/AndroidRuntime(300): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class HorizontalListView
06-15 01:34:37.219: E/AndroidRuntime(300): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-15 01:34:37.219: E/AndroidRuntime(300): at com.example.tryntry.HorizontalListViewDemo.onCreateView(HorizontalListViewDemo.java:22)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-15 01:34:37.219: E/AndroidRuntime(300): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.app.Activity.setContentView(Activity.java:1647)
06-15 01:34:37.219: E/AndroidRuntime(300): at com.example.tryntry.Struct.onCreate(Struct.java:13)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-15 01:34:37.219: E/AndroidRuntime(300): ... 11 more
06-15 01:34:37.219: E/AndroidRuntime(300): Caused by: java.lang.ClassNotFoundException: android.view.HorizontalListView in loader dalvik.system.PathClassLoader[/data/app/com.example.tryntry-1.apk]
06-15 01:34:37.219: E/AndroidRuntime(300): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
06-15 01:34:37.219: E/AndroidRuntime(300): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
06-15 01:34:37.219: E/AndroidRuntime(300): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
06-15 01:34:37.219: E/AndroidRuntime(300): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
06-15 01:34:37.219: E/AndroidRuntime(300): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
06-15 01:34:37.219: E/AndroidRuntime(300): ... 30 more
06-15 01:34:39.449: I/Process(300): Sending signal. PID: 300 SIG: 9
答案 0 :(得分:0)
title.setText(数据对象[位置]); <<我想它应该是一个'字符串'