以下是我的要求: 我定义了一个主要的线性布局。 我从Api那里得到一个arraylist。
现在,我想添加带有1个imageView和1个textView的自定义线性布局,并从API数据中为这些添加值。
上述线性布局将添加到垂直方向的主线性布局中。
自定义线性布局类:
public class LandingPageLinearLayout extends LinearLayout{
private ImageView iv_outletInfo_Icon;
private TextView tv_outletInfo;
public LandingPageLinearLayout(Context context, String tv_value, int iv_drawable_id) {
super(context);
// TODO Auto-generated constructor stub
/* View view = LayoutInflater.from(getContext()).inflate(
R.layout.landing_page_ll_custom, null);*/
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.landing_page_ll_custom, this, true);
iv_outletInfo_Icon = (ImageView) getChildAt(0).findViewById(R.id.imgview_outletInfoIcon);
iv_outletInfo_Icon.setBackgroundResource(iv_drawable_id);
tv_outletInfo = (TextView) getChildAt(1).findViewById(R.id.tv_outletInfo);
tv_outletInfo.setText(tv_value);
// this.addView(view);
}
}
自定义线性布局的xml布局:
<?xml version="1.0" encoding="utf-8"?>
<com.iwillcode.interfaces.LandingPageLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:gravity="center_vertical"
android:padding="14dp" >
<ImageView
android:id="@+id/imgview_outletInfoIcon"
android:layout_width="22dp"
android:layout_height="22dp"
android:contentDescription="@null" />
<TextView
android:id="@+id/tv_outletInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="3.5"
android:textColor="@color/text_color"
android:textSize="12sp" />
</com.iwillcode.interfaces.LandingPageLinearLayout>
活动中调用的方法:
private void generateTextViews(int n, LinearLayout mainLinearLayout){
// Dynamic generation of TextViews as per requirement
// n = size of arrayList
final LandingPageLinearLayout[] myTextViews = new LandingPageLinearLayout[n];
hashMap = new HashMap<String,String>();
//RatingBar Linear Layout
for (int i = 0; i < n; i++) {
// create a new Custom Linear Layout
hashMap = arraylist_outletInfo.get(i);
String name = hashMap.get(Config.KEY_NAME);
String value = hashMap.get("value");
if(name.equalsIgnoreCase("rating")){
ll_ratingLayout.setVisibility(View.VISIBLE);
tv_rating.setText(hashMap.get("value"));
ratingBar.setRating(Float.parseFloat(value));
}
final LandingPageLinearLayout rowCustomView = new LandingPageLinearLayout(context,value,R.drawable.outlet_icon);
// Set text of rowTextView
// rowTextView.setText("This is row #" + i+value);
// Condition for alternate colors
if(n%2==0){
rowCustomView.setBackgroundColor(Color.WHITE);
}else{
rowCustomView.setBackgroundColor(Color.parseColor("#f2f2f2"));
}
// add the textview to the linearlayout
mainLinearLayout.addView(rowCustomView);
// save a reference to the textview for later
// myTextViews[i] = rowTextView;
}
}
logcat的:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.goldvip.crownit/com.goldvip.crownit.LandingPageActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class com.goldvip.interfaces.LandingPageLinearLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2345)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.access$800(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5315)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class com.goldvip.interfaces.LandingPageLinearLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:616)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.goldvip.interfaces.LandingPageLinearLayout.<init>(LandingPageLinearLayout.java:26)
at com.goldvip.crownit.LandingPageActivity.generateTextViews(LandingPageActivity.java:1777)
at com.goldvip.crownit.LandingPageActivity.onCreate(LandingPageActivity.java:269)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.access$800(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5315)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getConstructor(Class.java:495)
at android.view.LayoutInflater.createView(LayoutInflater.java:580)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.goldvip.interfaces.LandingPageLinearLayout.<init>(LandingPageLinearLayout.java:26)
at com.goldvip.crownit.LandingPageActivity.generateTextViews(LandingPageActivity.java:1777)
at com.goldvip.crownit.LandingPageActivity.onCreate(LandingPageActivity.java:269)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.access$800(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5315)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)
加载活动时应用程序崩溃。
答案 0 :(得分:1)
Logcat说: 引起:java.lang.NoSuchMethodException:[class android.content.Context,interface android.util.AttributeSet]
你忘了实现一个超类构造函数,即: LinearLayout(Context context,AttributeSet attrs)
它被调用,因为你在XML中使用你的视图。 确保也实现这两个构造函数:
LinearLayout(Context context,AttributeSet attrs)
LinearLayout(Context context,AttributeSet attrs,int defStyleAttr)
答案 1 :(得分:0)
如果你想在你需要的布局中声明你的自定义视图,至少是以Context
和AttributeSet
作为参数的构造函数
public LandingPageLinearLayout(Context context, AttributeSet attr) {
super(context, attr);
你应该避免重载View的构造函数。可以为自定义布局声明自定义属性。
答案 2 :(得分:0)
希望它会对你有所帮助。
在xmlns之后在XML中添加新行:android =&#34; http://schemas.android.com/apk/res/android"
- 新行:xmlns:app =&#34; http://schemas.android.com/apk/res/YourMainPackageName"
清理和建造项目。