android studio gridview如何实现

我实现的老是黑屏退出
mainactivity.java

 private LayoutInflater inflater;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        inflater = LayoutInflater.from(getApplicationContext());                
        GridView dev_listview =(GridView)findViewById(R.id.dev_listview);
        MyAdapter gAdapter = new MyAdapter (this);          
        dev_listview.setAdapter(gAdapter);
        setContentView(R.id.dev_listview);
    }

MyAdapter.java

 public class MyAdapter extends BaseAdapter {
      private LayoutInflater inflater;
    private Context mcontext;
    private Integer[] mThumbnails = {R.drawable.unpress9};
    public MyAdapter( Context c) 
    { 
       mcontext = c;
    } 
     public int getCount() {
            return mThumbnails.length;
     }
     public Object getItem(int position) {
            return null;
        }

      public long getItemId(int position) {
            return 0;
        }


      @SuppressLint("InflateParams")
    public View getView(int position, View grid, ViewGroup parent) 
      {
          inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(grid != null)
     {
        inflater = LayoutInflater.from(mcontext);
        grid =inflater.inflate(R.layout.item_9,parent);
     }

     return grid;

     }

} 

item_9.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal">
    <LinearLayout
          android:orientation="horizontal">
        <LinearLayout
            android:orientation="horizontal">
            <TextView
                android:id="@+id/text1"/>
            <ImageView
                android:id="@+id/img3" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img4" />
            <TextView
                android:id="@+id/text2"/>
            <ProgressBar
                android:id="@+id/bar"  />
            <TextView
                android:id="@+id/text3" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img" />
            <TextView
                android:id="@+id/text4" />
            <ProgressBar
                android:id="@+id/br" />
            <TextView
                android:id="@+id/text5"/>
        </LinearLayout>
                <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img94"/>
            <TextView
                android:id="@+id/text6"/>
            <ProgressBar
                android:id="@+id/r" />
            <TextView
                android:id="@+id/text7"/>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img93" />
            <TextView
                android:id="@+id/text8"/>
            <ProgressBar
                android:id="@+id/ar" />
            <TextView
                android:id="@+id/text9"/>
        </LinearLayout>
        <LinearLayout
           android:orientation="horizontal">
            <ImageView
                android:id="@+id/img22" />
            <TextView
                android:id="@+id/text10"/>
            <ProgressBar
                android:id="@+id/ar13" />
            <TextView
                android:id="@+id/text11"/>
        </LinearLayout>
                <LinearLayout
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/img6"/>
            <TextView
                android:id="@+id/text12" />
            <ProgressBar
                android:id="@+id/r17" />
            <TextView
                android:id="@+id/text13"/>
        </LinearLayout>       
         <LinearLayout
            android:orientation="horizontal" >
            <LinearLayout
                android:orientation="vertical" >
                <LinearLayout
                   android:orientation="horizontal" >
                    <TextView
                        android:id="@+id/text17"/>
                    <ImageView
                        android:id="@+id/mg"/>
                    <ImageView
                        android:id="@+id/mg2"/>
                    <ImageView
                        android:id="@+id/dev_ctrl_stat_img3"/>
                    <ImageView
                        android:id="@+id/k"/>
                    <ImageView
                        android:id="@+id/img90"/>
                    <ImageView
                        android:id="@+id/img91"/>
                    <TextView
                        android:id="@+id/text19"/>
                                                <TextView
                        android:id="@+id/text20" />                  
                </LinearLayout>
                <LinearLayout
                    android:gravity="center_vertical" />
                    <ImageView                      
                        android:src="@drawable/img9" />
                    <TextView
                        android:gravity="center_vertical" />
                    <TextView
                        android:layout_width="wrap_content"/>
                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_weight="4.5" >
                <ImageView
                    android:id="@+id/qry"/>
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

想实现的功能是:item.xml全部写进gridview的一个item中,这个item里有9行,我下个item调用令一个5行的item5.xml,要保障不同item每行的高度都一样,如何实现呢,求大神指教!

3个回答

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        inflater = LayoutInflater.from(getApplicationContext());                
        GridView dev_listview =(GridView)findViewById(R.id.dev_listview);
        MyAdapter gAdapter = new MyAdapter (this);          
        dev_listview.setAdapter(gAdapter);
        setContentView(R.id.dev_listview);//怎么又set布局了,去掉
    }
llx2007
凌云志轩 activity_main.xml发过来了,我看着没错误呀,我把这两个界面都写好单独能跑起来才往一起合的呀
4 年多之前 回复
llx2007
凌云志轩 第30行没错误啊,就是 android:textsize="18dp"呀!
4 年多之前 回复
danielinbiti
danielinbiti 回复空城入魔: activity_main.xml的30行看看错误 Binary XML file line #30
4 年多之前 回复
danielinbiti
danielinbiti 回复空城入魔: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView dev_listview =(GridView)findViewById(R.id.dev_listview); MyAdapter gAdapter = new MyAdapter (this); dev_listview.setAdapter(gAdapter); }
4 年多之前 回复
llx2007
凌云志轩 我是直接将最外围的LayoutInflater写进适配器就行了还是在适配器里将所有的LayoutInflater及其中的组建都写进去才行呀
4 年多之前 回复
danielinbiti
danielinbiti 回复空城入魔: 看不见错误
4 年多之前 回复
llx2007
凌云志轩 04-16 16:48:45.902: E/AndroidRuntime(25616): FATAL EXCEPTION: main
4 年多之前 回复
llx2007
凌云志轩 04-16 16:48:45.902: E/AndroidRuntime(25616): FATAL EXCEPTION: main
4 年多之前 回复
llx2007
凌云志轩 04-16 16:48:45.902: E/AndroidRuntime(25616): FATAL EXCEPTION: main
4 年多之前 回复
llx2007
凌云志轩 还是不行呀,还是黑屏退出呀
4 年多之前 回复

错误代码
04-16 16:53:07.066: E/AndroidRuntime(25817): FATAL EXCEPTION: main
04-16 16:53:07.066: E/AndroidRuntime(25817): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.huyubao/com.example.huyubao.MainActivity}: android.view.InflateException: Binary XML file line #30: Error inflating class com.android.internal.widget.ActionBarView
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.app.ActivityThread.access$700(ActivityThread.java:140)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.os.Looper.loop(Looper.java:137)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.app.ActivityThread.main(ActivityThread.java:4935)
04-16 16:53:07.066: E/AndroidRuntime(25817): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 16:53:07.066: E/AndroidRuntime(25817): at java.lang.reflect.Method.invoke(Method.java:511)
04-16 16:53:07.066: E/AndroidRuntime(25817): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
04-16 16:53:07.066: E/AndroidRuntime(25817): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
04-16 16:53:07.066: E/AndroidRuntime(25817): at dalvik.system.NativeStart.main(Native Method)
04-16 16:53:07.066: E/AndroidRuntime(25817): Caused by: android.view.InflateException: Binary XML file line #30: Error inflating class com.android.internal.widget.ActionBarView
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.createView(LayoutInflater.java:619)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
04-16 16:53:07.066: E/AndroidRuntime(25817): at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3161)
04-16 16:53:07.066: E/AndroidRuntime(25817): at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3221)
04-16 16:53:07.066: E/AndroidRuntime(25817): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:312)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.app.Activity.setContentView(Activity.java:1924)
04-16 16:53:07.066: E/AndroidRuntime(25817): at com.example.huyubao.MainActivity.onCreate(MainActivity.java:14)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.app.Activity.performCreate(Activity.java:5206)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
04-16 16:53:07.066: E/AndroidRuntime(25817): ... 11 more
04-16 16:53:07.066: E/AndroidRuntime(25817): Caused by: java.lang.reflect.InvocationTargetException
04-16 16:53:07.066: E/AndroidRuntime(25817): at java.lang.reflect.Constructor.constructNative(Native Method)
04-16 16:53:07.066: E/AndroidRuntime(25817): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.createView(LayoutInflater.java:593)
04-16 16:53:07.066: E/AndroidRuntime(25817): ... 25 more
04-16 16:53:07.066: E/AndroidRuntime(25817): Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class android.widget.TextView
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.createView(LayoutInflater.java:619)
04-16 16:53:07.066: E/AndroidRuntime(25817): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-16 16:53:07.066: E/AndroidRuntime(25817): at com.android.internal.widget.ActionBarView.initTitle(ActionBarView.java:891)
04-16 16:53:07.066: E/AndroidRuntime(25817): at com.android.internal.widget.ActionBarView.setDisplayOptions(ActionBarView.java:692)
04-16 16:53:07.066: E/AndroidRuntime(25817): at com.android.internal.widget.ActionBarView.(ActionBarView.java:293)
04-16 16:53:07.066: E/AndroidRuntime(25817): ... 28 more
04-16 16:53:07.066: E/AndroidRuntime(25817): Caused by: java.lang.reflect.InvocationTargetException
04-16 16:53:07.066: E/AndroidRuntime(25817): at java.lang.reflect.Constructor.constructNative(Native Method)
04-16 16:53:07.066: E/AndroidRuntime(25817): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.view.LayoutInflater.createView(LayoutInflater.java:593)
04-16 16:53:07.066: E/AndroidRuntime(25817): ... 38 more
04-16 16:53:07.066: E/AndroidRuntime(25817): Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.content.res.TypedArray.getColor(TypedArray.java:326)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.widget.TextView.(TextView.java:662)
04-16 16:53:07.066: E/AndroidRuntime(25817): at android.widget.TextView.(TextView.java:601)
04-16 16:53:07.066: E/AndroidRuntime(25817): ... 41 more
错误

activiyt_main.xml

 <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=".DevListActivity" >
    <LinearLayout
        android:id="@+id/fullscreen_content_full"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black_overlay"
        android:orientation="vertical"
        tools:ignore="UselessParent" >
        <LinearLayout
            android:id="@+id/fullscreen_content1_controls"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="6"
            android:onClick="flush_weather"
            android:background="@drawable/weather_bj"
            android:orientation="horizontal"
            tools:ignore="UselessParent" >
            <TextView
                android:id="@+id/weather_message"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="15dp"
                android:drawableLeft="@drawable/duoyun"
                android:textColor = "#f3f3f3"
                android:gravity="center"
                android:textsize="18dp"
                android:text="@string/weather" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:drawableLeft="@drawable/flush"
                android:text=""
                android:textSize="20dp"
                android:textColor = "#f3f3f3"
                android:gravity="center"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/fullscreen_content2_controls"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#e6e3e7"
            android:orientation="vertical"
            tools:ignore="UselessParent" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="7.8"
                android:background="@drawable/clockbg_unpress" >
                <!-- android:background="#bec3c9"
                 android:textColor = "#669999"
                 android:textColor = "#5c8997"-->
                <TextView
                    android:id="@+id/dev_num_total"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:layout_marginLeft="10dp"
                    android:textColor = "#333333"
                    android:textSize="18dp"
                    android:text="设备数量:0"/>
                <TextView
                    android:id="@+id/dev_num_online"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:layout_marginLeft="20dp"
                    android:textColor = "#333333"
                    android:textSize="18dp"
                    android:text="在线设备:0"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/body_bj"
                android:layout_weight="1.5"
                android:layout_marginLeft="0dp"
                android:layout_marginRight="0dp"
                android:layout_marginBottom="0dp"
                android:layout_marginTop="0dp"  >
                <!-- android:background="@drawable/bj_qn"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="2dp" -->

                <GridView
                    android:id="@+id/dev_listview"
                    android:numColumns="1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:verticalSpacing="0dp"
                    android:horizontalSpacing="0dp"
                    android:stretchMode="columnWidth" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="7.5"
                android:gravity="center"
                android:background="#ffA8B4B9"
                android:orientation="horizontal" >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:layout_marginTop="2dp"
                    android:layout_marginRight="0.5dp"
                    android:onClick="sys_setting"
                    android:background="@drawable/set_btn">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:drawableLeft="@drawable/set"
                        android:text=" 设置"
                        android:textColor="#f3f3f3"
                        android:textSize="20dp" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:layout_marginTop="2dp"
                    android:layout_marginLeft="0.5dp"
                    android:onClick="sys_exit"
                    android:background="@drawable/set_btn">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:drawableLeft="@drawable/quit"
                        android:text=" 退出"
                        android:textColor="#f3f3f3"

                        anaaadroid:textSize="20dp" />
                </LinearLayout>

            </LinearLayout>
            <!-- <TextView
                 android:text="智慧农业养鱼管理平台"
                 android:textColor="#f3f3f3"
                 android:textSize="16dp"
                 android:gravity="center"
                 android:background="#999999">
            </TextView> -->
        </LinearLayout>

    </LinearLayout>
</RelativeLayout>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!