我在LinearLayout
内有ScrollView
,我正在尝试动态添加图片按钮到视图中。我知道其中的部分内容已得到解决,但我需要有人来查看我的代码并提供他们的意见,说明为什么它不起作用:
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
public class Carousel extends ScrollView {
int[] tabs;
public Carousel(Context context) {
super(context);
}
public void addTabs(int [] tabs){
for(int ii=0; ii < tabs.length; ii++){
//get resource id for each button
int menuButton = tabs[ii];
//set image button with expected image
ImageButton ib = new ImageButton(this.getContext());
ib.setImageResource(menuButton);
/*float imgWidth = ib.getWidth();
float imgHeight = ib.getHeight();
int width = (int) Math.round(imgWidth);
int height = (int) Math.round(imgHeight); */
LinearLayout linLayout = (LinearLayout)findViewById(R.id.menuTabs);
linLayout.addView(ib); //<-- Line 35
//set position of each button
}
}
}
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout android:id="@+id/menuTabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
logcat的:
08-23 18:53:26.276: W/dalvikvm(6736): threadid=1: thread exiting with uncaught exception (group=0x401ec760)
08-23 18:53:26.276: E/AndroidRuntime(6736): FATAL EXCEPTION: main
08-23 18:53:26.276: E/AndroidRuntime(6736): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uie.menu.app/com.uie.menu.app.HomeActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1818)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1834)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.app.ActivityThread.access$500(ActivityThread.java:122)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1027)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.os.Looper.loop(Looper.java:132)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.app.ActivityThread.main(ActivityThread.java:4126)
08-23 18:53:26.276: E/AndroidRuntime(6736): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 18:53:26.276: E/AndroidRuntime(6736): at java.lang.reflect.Method.invoke(Method.java:491)
08-23 18:53:26.276: E/AndroidRuntime(6736): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
08-23 18:53:26.276: E/AndroidRuntime(6736): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
08-23 18:53:26.276: E/AndroidRuntime(6736): at dalvik.system.NativeStart.main(Native Method)
08-23 18:53:26.276: E/AndroidRuntime(6736): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
08-23 18:53:26.276: E/AndroidRuntime(6736): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:245)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.app.Activity.setContentView(Activity.java:1786)
08-23 18:53:26.276: E/AndroidRuntime(6736): at com.uie.menu.app.HomeActivity.onCreate(HomeActivity.java:34)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1782)
08-23 18:53:26.276: E/AndroidRuntime(6736): ... 11 more
08-23 18:53:26.276: E/AndroidRuntime(6736): Caused by: java.lang.NullPointerException
08-23 18:53:26.276: E/AndroidRuntime(6736): at com.uie.menu.app.Carousel.addTabs(Carousel.java:35)
08-23 18:53:26.276: E/AndroidRuntime(6736): at com.uie.menu.app.MenuTabFragment.onCreateView(MenuTabFragment.java:27)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:846)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
08-23 18:53:26.276: E/AndroidRuntime(6736): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:660)
08-23 18:53:26.276: E/AndroidRuntime(6736): ... 20 more
08-23 18:53:28.286: I/dalvikvm(6736): threadid=4: reacting to signal 3
08-23 18:53:28.286: I/dalvikvm(6736): Wrote stack traces to '/data/anr/traces.txt'