0

正在创建一个我正在使用框架布局的应用程序.....我已经使用了三个类 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>
4

2 回答 2

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();

} }

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

于 2012-05-21T07:45:28.780 回答
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班级内。

在那里你有一个名为eventtype的变量MotionEvent,它没有被初始化,但是你在你的changeImage方法中使用它:

i.onTouchEvent(event);

这必须抛出NPE. 由于您从 调用该方法onCreate,因此您无法为其分配有效的MotionEvent方法,因此您需要

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

Eidt2 : ...在评论/聊天之后,你应该这样做:

  • i.onTouchEvent(event);从您的changeImage方法中删除;
  • 正确实施该onTouch方法以显示所需的文本。
  • 删除ImageView实例,然后this改用。

这样你的FirstImage班级应该改变:

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;
    }
}

Edit3:如果你能简化/澄清你的addClickListener方法,那也很好。

您真的不需要一遍又一遍地将侦听器添加到这些按钮...

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--;
        }
    });
}

Edit4:另外,请更正您的布局 xml 文件,而不是ImageViews声明button1and 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属性完成的,而不是由您在它们上的可绘制对象完成的!

back = (ImageButton) findViewById(R.id.button1);
next = (ImageButton) findViewById(R.id.button2);
于 2012-05-21T07:36:40.843 回答