当前位置: 代码迷 >> Android >> 求教Framelayout布局有关问题
  详细解决方案

求教Framelayout布局有关问题

热度:65   发布时间:2016-05-01 22:10:30.0
求教Framelayout布局问题
想在LinearLayout下嵌套一个Framelayout,xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

  <Button
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Button" />

  <FrameLayout
  android:id="@+id/layout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

  <ListView
  android:id="@+id/listView1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
  </ListView>
  </FrameLayout>

</LinearLayout>


  程序代码:
  package com.circle.test;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ListView;

public class Test_CircleActivity extends Activity {
private Button btn;
private FrameLayout layout;
private FrameLayout.LayoutParams params1;
private ListView lv;
private String items[] = {"a","b","c","d","e","f","g","h","i"};
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
   
  btn = (Button)findViewById(R.id.button1);
  layout = (FrameLayout)findViewById(R.id.layout);
  params1 = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  MyView view1 = new MyView(this);
  layout.addView(view1, params1);
  lv = (ListView)findViewById(R.id.listView1);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, items);
  lv.setAdapter(adapter);
  }
  public class MyView extends View{

public MyView(Context context) {
super(context);
}
  @Override
  protected void onDraw(Canvas canvas) {
 
  super.onDraw(canvas);
  Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Style.STROKE);

canvas.drawCircle(200, 200, 10, paint);
canvas.drawCircle(100, 200, 10, paint);
canvas.drawCircle(220, 200, 10, paint);

  }
  }
}

程序运行报错:ava.lang.RuntimeException: Unable to start activity ComponentInfo{com.circle.test/com.circle.test.Test_CircleActivity}: java.lang.ClassCastException: android.widget.FrameLayout
  求助高人帮我看下,哪里出错了?

------解决方案--------------------
模拟器上试了一下没问题,能出来list和三个红圈。你再查查你的xml等配置文件