framelayout不起作用

时间:2012-05-21 06:07:13

标签: android android-framelayout

正在创建一个我正在使用framelayout的应用程序..... 我用了三节课 1)活动 2)选择图像 3)在图像上绘图

所以我的活动课程如下

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         i= new FirstImage(this);


         j=(Draw)findViewById(R.id.info);
         t=(TextView)findViewById(R.id.textView);
         k=(TextView)findViewById(R.id.textView1);
         back=(ImageButton)findViewById(R.drawable.monotone_arrow_next_left);
         next=(ImageButton)findViewById(R.drawable.monotone_arrow_next_lrightcopy);
         if (count==0){
             ((FirstImage) i).changeImage(R.drawable.human);

         }
         addListenerOnButton();
    }
    private void addListenerOnButton() {
        // TODO Auto-generated method stub




        next.setOnClickListener( new OnClickListener() {


            @Override
            public void onClick(View arg0) {
                if(count==0){

                    //i.startAnimation(animationFadeout);
                    ((FirstImage) i).changeImage(R.drawable.hand);
                back.setEnabled(true);
                count++;

                addListenerOnButton();

                }
                else if (count==1){
                    //i.startAnimation(animationFadeout);
                    ((FirstImage) i).changeImage(R.drawable.tissue);
                    count++;

                    addListenerOnButton();

                }

                else if (count==2){
                    //i.startAnimation(animationFadeout);
                    ((FirstImage) i).changeImage(R.drawable.cell);
                    count++;

                    addListenerOnButton();


                }
                else if (count==3){
                    //nucleus
                    //i.startAnimation(animationFadeout);
                    ((FirstImage) i).changeImage(R.drawable.nucleus);
                    count++;

                    addListenerOnButton();

                }
                else if (count==4){
                    //nchromosome
                    //i.startAnimation(animationFadeout);
                    ((FirstImage) i).changeImage(R.drawable.chromosome);
                    count++;

                    addListenerOnButton();

                }
                else
                {   //double helix
                    //i.startAnimation(animationFadeout);
                    count++;
                    ((FirstImage) i).changeImage(R.drawable.dnahelix);
                    next.setEnabled(false);

                    addListenerOnButton();

                }

            }
        });

        back.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View arg0) {
                if(count==1){
                    //i.startAnimation(animationFadeout);
                    ((FirstImage) i).changeImage(R.drawable.human);
                    back.setEnabled(false);
                    count--;

                    addListenerOnButton();

                    }
                    else if (count==2){
                        //i.startAnimation(animationFadeout);
                        ((FirstImage) i).changeImage(R.drawable.hand);
                        count--;

                        addListenerOnButton();

                    }

                    else if (count==3){
                        //i.startAnimation(animationFadeout);
                        ((FirstImage) i).changeImage(R.drawable.tissue);
                        count--;

                        addListenerOnButton();


                    }
                    else if (count==4){
                        //i.startAnimation(animationFadeout);
                        ((FirstImage) i).changeImage(R.drawable.cell);
                        count--;

                        addListenerOnButton();

                    }
                    else if (count==5){
                        //nucleus
                        //i.startAnimation(animationFadeout);
                        ((FirstImage) i).changeImage(R.drawable.nucleus);
                        count--;

                        addListenerOnButton();


                    }
                    else {
                        //chromosome
                        //count==6
                        //i.startAnimation(animationFadeout);
                        ((FirstImage) i).changeImage(R.drawable.chromosome);
                        count--;

                        addListenerOnButton();
                        next.setEnabled(true);

                    }
                }
            });
    }


}

我的班级正在选择一个图像,如下所示..

   public class FirstImage extends ImageView implements OnTouchListener {

    ImageView i;
    MotionEvent event;
    int x;
    Bitmap image;
    String huma ="human";
    String info = "";
     float y = 0; //init value 
     float z = 0; //init value
     Animation animationFadeIn;
    Draw child;

    public FirstImage(Context context) { 
        super(context); 
        child=new Draw (context);
        i= new ImageView (context); 


        } 
    public FirstImage(Context context, AttributeSet attrs) { 
        super(context, attrs);
        } 


    public void changeImage(int id){
        final Animation animationFadeout=AnimationUtils.loadAnimation(getContext(), R.anim.zoomout);
                    i.setImageResource(id);
                    i.startAnimation(animationFadeout);
                    i.setOnTouchListener(this);

    }
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        // TODO Auto-generated method stub
        return false;
    }
    } 

我的课程将在图像上绘制,如下所示

public class Draw extends View {
    String info = "";
    float x = 0;    
    float y = 0;    
    int color = Color.GREEN;    
    public Draw(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    public Draw(Context context, AttributeSet attrs) {
          super(context, attrs);

    }

    @Override protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(color);
        paint.setStrokeWidth(2);
        paint.setTextSize(30);

        canvas.drawLine(x-10, y, x+10, y, paint);
        canvas.drawLine(x, y-10, x, y+10, paint);
        canvas.drawText(info, x, y, paint);

    }

    public void updateInfo(String t_info, float t_x, float t_y){
        info = t_info;
        x = t_x;
        y = t_y;

        invalidate();
    }

    public void clearInfo(){
        info = "";
        x = 0;
        y = 0;
        invalidate();
    }
}

它显示了未经检验的应用程序关闭......但是在logcat中显示

   05-21 15:55:18.964: E/AndroidRuntime(850): FATAL EXCEPTION: main
05-21 15:55:18.964: E/AndroidRuntime(850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nam/com.example.nam.AshActivity}: java.lang.NullPointerException
05-21 15:55:18.964: E/AndroidRuntime(850):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-21 15:55:18.964: E/AndroidRuntime(850):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-21 15:55:18.964: E/AndroidRuntime(850):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-21 15:55:18.964: E/AndroidRuntime(850):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-21 15:55:18.964: E/AndroidRuntime(850):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 15:55:18.964: E/AndroidRuntime(850):  at android.os.Looper.loop(Looper.java:123)
05-21 15:55:18.964: E/AndroidRuntime(850):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-21 15:55:18.964: E/AndroidRuntime(850):  at java.lang.reflect.Method.invokeNative(Native Method)
05-21 15:55:18.964: E/AndroidRuntime(850):  at java.lang.reflect.Method.invoke(Method.java:507)
05-21 15:55:18.964: E/AndroidRuntime(850):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-21 15:55:18.964: E/AndroidRuntime(850):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-21 15:55:18.964: E/AndroidRuntime(850):  at dalvik.system.NativeStart.main(Native Method)
05-21 15:55:18.964: E/AndroidRuntime(850): Caused by: java.lang.NullPointerException
05-21 15:55:18.964: E/AndroidRuntime(850):  at com.example.nam.AshActivity.addListenerOnButton(AshActivity.java:48)
05-21 15:55:18.964: E/AndroidRuntime(850):  at com.example.nam.AshActivity.onCreate(AshActivity.java:40)
05-21 15:55:18.964: E/AndroidRuntime(850):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-21 15:55:18.964: E/AndroidRuntime(850):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-21 15:55:18.964: E/AndroidRuntime(850):  ... 11 more

我的xml文件如下....

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


  <!--  Screen Design for VIDEOS -->
<TableLayout 
    android:id="@+id/tablelayout"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:stretchColumns="1">

     <TableRow
          android:id="@+id/tableRow1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >

          <TextView
               android:id="@+id/textView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="15dip"
              android:text="pic on click which will tell where is the dna located in human body or cell "
              android:textSize="18dip" />
      </TableRow>


      <TableRow
          android:id="@+id/tableRow2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >
    <FrameLayout
        android:layout_width="wrap_content"
          android:layout_height="wrap_content" >
         <com.example.nam.FirstImage
             android:id="@+id/image"
             android:layout_height="wrap_content" 
             android:layout_width="fill_parent" />
          <com.example.nam.Draw
             android:id="@+id/info"
             android:layout_height="wrap_content" 
             android:layout_width="fill_parent" />

      </FrameLayout>   
      </TableRow>

      <TableRow
          android:id="@+id/tableRow3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >

          <LinearLayout
              android:id="@+id/linearLayout1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

              <ImageView
                  android:id="@+id/button1"
                  android:layout_width="100dp"
                  android:layout_height="wrap_content"
                  android:src="@drawable/monotone_arrow_next_left"/>
              <ImageView
                  android:id="@+id/button2"
                  android:layout_width="100dp"
                  android:layout_height="wrap_content"
                 android:src="@drawable/monotone_arrow_next_lrightcopy"/>

          </LinearLayout>

      </TableRow>
       <TableRow
          android:id="@+id/tableRow4"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >
          <TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:padding="15dip"
              android:text="please don't mind  "
              android:textSize="18dip" />
          </TableRow>

  </TableLayout>
  </ScrollView>

2 个答案:

答案 0 :(得分:1)

使用自定义视图时,错误指向一行:com.example.nam.FirstImage

在xml布局中使用自己的View扩展时,应该为它们声明一个命名空间。

你缺少的是一条线:

xmlns:name="http://schemas.android.com/apk/res/com.example.nam"

所以你的布局的根元素应如下所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:nam="http://schemas.android.com/apk/res/com.example.nam" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

Edit1 :现在NullPointerException位于您的FirstImage课程内。

你有一个名为event的{​​{1}}类型的变量,它没有被初始化,但你在MotionEvent方法中使用它:

changeImage

这必须抛出i.onTouchEvent(event); 。由于您从NPE调用该方法,因此您无法为其分配有效的onCreate,这样您就需要

  • 组成一个,或
  • 检查MotionEvent成员是否为空,如果它不为空则仅调用event

Eidt2 :...评论/聊天后,您应该做的就是:

  • onTouchEvent方法移除i.onTouchEvent(event);;
  • 正确实施changeImage方法以显示所需的文字。
  • 删除onTouch个实例,然后使用ImageView

这样您的this课程就会改变:

FirstImage

Edit3 :如果您想简化/澄清一下public class FirstImage extends ImageView implements OnTouchListener { MotionEvent event; int x; Bitmap image; String huma = "human"; String info = ""; float y = 0; // init value float z = 0; // init value Animation animationFadeIn; Draw child; public FirstImage(Context context) { super(context); child = new Draw(context); } public FirstImage(Context context, AttributeSet attrs) { super(context, attrs); } public void changeImage(int id) { final Animation animationFadeout = AnimationUtils.loadAnimation( getContext(), R.anim.zoomout); this.setImageResource(id); this.startAnimation(animationFadeout); this.setOnTouchListener(this); } @Override public boolean onTouch(View arg0, MotionEvent arg1) { // TODO: Here you show the text you want, e.g.: // final TextView tv = (TextView)findViewById(R.id.text); // tv.setText("This image is about..."); return true; } } 方法,那也不错。

你真的不需要一遍又一遍地将听众添加到这些按钮......

addClickListener

Edit4 :另外,请更正您的布局xml文件,而不是为private void addListenerOnButton() { next.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { switch (count) { case 0: ((FirstImage) i).changeImage(R.drawable.hand); back.setEnabled(true); break; case 1: ((FirstImage) i).changeImage(R.drawable.tissue); break; case 2: ((FirstImage) i).changeImage(R.drawable.cell); break; case 3: ((FirstImage) i).changeImage(R.drawable.nucleus); break; case 4: ((FirstImage) i).changeImage(R.drawable.chromosome); break; case 5: ((FirstImage) i).changeImage(R.drawable.dnahelix); next.setEnabled(false); break; default: break; } count++; } }); back.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { switch (count) { case 1: ((FirstImage) i).changeImage(R.drawable.human); back.setEnabled(false); break; case 2: ((FirstImage) i).changeImage(R.drawable.hand); break; case 3: ((FirstImage) i).changeImage(R.drawable.tissue); break; case 4: ((FirstImage) i).changeImage(R.drawable.cell); break; case 5: ((FirstImage) i).changeImage(R.drawable.nucleus); break; case 6: ((FirstImage) i).changeImage(R.drawable.chromosome); next.setEnabled(true); break; default: break; } count--; } }); } ImageViews声明button1,请使用button2当你试图用ImageButtons方法抛出它们时:

onCreate

并且分配是通过按钮的<ImageButton android:id="@+id/button1" android:layout_width="100dp" android:layout_height="wrap_content" android:src="@drawable/monotone_arrow_next_left"/> <ImageButton android:id="@+id/button2" android:layout_width="100dp" android:layout_height="wrap_content" android:src="@drawable/monotone_arrow_next_lrightcopy"/> 属性完成的,而不是通过你对它们的绘图来完成的!

@id

答案 1 :(得分:1)

从官方文档中查看有关FrameLayout的信息:

  

FrameLayout旨在阻挡屏幕上的某个区域以显示单个项目。通常,FrameLayout应该用于保存单个子视图,因为很难以可扩展到不同屏幕大小而儿童不会相互重叠的方式组织子视图。但是,您可以使用android:layout_gravity属性将多个子项添加到FrameLayout并通过为每个子项分配重力来控制它们在FrameLayout中的位置。

如果我是你,我会尝试使用两个不同的FrameLayouts来查看它是否会起作用..但是我可以从你的堆栈跟踪中看到问题出现在你的xml文件中。

编辑:

所以这是正在运行的代码。把它放在你的活动上:

public class AshActivity extends Activity implements OnTouchListener {
ImageView i;
Draw j;
TextView t,k;
ImageView back;
ImageView next;
int count=0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     i= new FirstImage(this);
     if (count==0)
         ((FirstImage) i).changeImage(R.drawable.ic_launcher);
     j=(Draw)findViewById(R.id.info);
     t=(TextView)findViewById(R.id.textView);
     k=(TextView)findViewById(R.id.textView1);
     back=(ImageView)findViewById(R.id.button1);
     next=(ImageView)findViewById(R.id.button2);
     addListenerOnButton();

} }

这应该可以解决问题! :)