Android OnTouchListener问题

时间:2012-05-02 07:18:35

标签: android ontouchlistener

我正在尝试在我的程序中实现onTouch事件,但它无效。我是以一种简单的方式做到的,但事实并非如此。我的代码是:

    public class VideosActivity extends Activity {
    ImageView image;
    FirstView fview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.videos_layout);
        //image = (ImageView)findViewById(R.id.imageView1);
       fview = new FirstView(this);
       setContentView(fview);
    }
     public class FirstView extends View implements OnTouchListener{

        public FirstView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            image = (ImageView)findViewById(R.id.imageView1);
            image.setOnTouchListener(this);
        }


         public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        image.setImageResource(R.drawable.dna2);
        return false;
    }
}
}

在我的程序中显示以下错误。我的堆栈被粘贴在这里......

    05-02 12:43:02.255: E/AndroidRuntime(358): FATAL EXCEPTION: main
05-02 12:43:02.255: E/AndroidRuntime(358): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtablayout/com.example.androidtablayout.VideosActivity}: java.lang.NullPointerException
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.widget.TabHost.setCurrentTab(TabHost.java:323)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.view.View.performClick(View.java:2408)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.view.View$PerformClick.run(View.java:8816)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.os.Handler.handleCallback(Handler.java:587)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.os.Looper.loop(Looper.java:123)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-02 12:43:02.255: E/AndroidRuntime(358):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 12:43:02.255: E/AndroidRuntime(358):  at java.lang.reflect.Method.invoke(Method.java:521)
05-02 12:43:02.255: E/AndroidRuntime(358):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-02 12:43:02.255: E/AndroidRuntime(358):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-02 12:43:02.255: E/AndroidRuntime(358):  at dalvik.system.NativeStart.main(Native Method)
05-02 12:43:02.255: E/AndroidRuntime(358): Caused by: java.lang.NullPointerException
05-02 12:43:02.255: E/AndroidRuntime(358):  at com.example.androidtablayout.VideosActivity$FirstView.<init>(VideosActivity.java:33)
05-02 12:43:02.255: E/AndroidRuntime(358):  at com.example.androidtablayout.VideosActivity.onCreate(VideosActivity.java:25)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 12:43:02.255: E/AndroidRuntime(358):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-02 12:43:02.255: E/AndroidRuntime(358):  ... 18 more
05-02 13:23:21.845: E/AndroidRuntime(515): FATAL EXCEPTION: main
05-02 13:23:21.845: E/AndroidRuntime(515): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtablayout/com.example.androidtablayout.VideosActivity}: java.lang.NullPointerException
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.widget.TabHost.setCurrentTab(TabHost.java:323)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.view.View.performClick(View.java:2408)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.view.View$PerformClick.run(View.java:8816)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.os.Handler.handleCallback(Handler.java:587)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.os.Looper.loop(Looper.java:123)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-02 13:23:21.845: E/AndroidRuntime(515):  at java.lang.reflect.Method.invokeNative(Native Method)
05-02 13:23:21.845: E/AndroidRuntime(515):  at java.lang.reflect.Method.invoke(Method.java:521)
05-02 13:23:21.845: E/AndroidRuntime(515):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-02 13:23:21.845: E/AndroidRuntime(515):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-02 13:23:21.845: E/AndroidRuntime(515):  at dalvik.system.NativeStart.main(Native Method)
05-02 13:23:21.845: E/AndroidRuntime(515): Caused by: java.lang.NullPointerException
05-02 13:23:21.845: E/AndroidRuntime(515):  at com.example.androidtablayout.VideosActivity$FirstView.<init>(VideosActivity.java:35)
05-02 13:23:21.845: E/AndroidRuntime(515):  at com.example.androidtablayout.VideosActivity.onCreate(VideosActivity.java:26)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 13:23:21.845: E/AndroidRuntime(515):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-02 13:23:21.845: E/AndroidRuntime(515):  ... 18 more

我的xml文件如下...

<TableLayout
      android:id="@+id/tableLayout1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >

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

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

          <ImageView
              android:id="@+id/imageView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/dna1" />

      </TableRow>
  </TableLayout>

4 个答案:

答案 0 :(得分:1)

您在image构造函数中使用的FirstView对象永远不会被赋予有效对象,当您尝试使用它时会抛出NullPointerException。如果ImageView位于您评论的布局(R.layout.videos_layout)中,那么它应该是:

super.onCreate(savedInstanceState);
setContentView(R.layout.videos_layout);
image = (ImageView) findViewById(R.id.the_id_of_the_imageview_fromThe_layout);
fview = new FirstView(this);
setContentView(fview);

否则,您必须确保为ImageView引用分配有效的image对象。

修改: 上面的代码会起作用,我不知道你为什么决定放下:

image = (ImageView)findViewById(R.id.imageView1);
fview的构造函数中的

(这将再次抛出NullPointerException)。正确的代码是:

//...
    public class VideosActivity extends Activity {
    ImageView image;
    FirstView fview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videos_layout);
        image = (ImageView)findViewById(R.id.imageView1);
        fview = new FirstView(this);
        setContentView(fview);
    }
     public class FirstView extends View implements OnTouchListener{

        public FirstView(Context context) {
            super(context);
            image.setOnTouchListener(this);
        }
//...

此外,我不认为你明白你在做什么,你应该修改你正在编写的代码。你:

  • 使用具有ImageView
  • 的布局设置contentView
  • 然后构造一个实现OnTouchListener的简单视图,并在此视图的构造函数中设置您刚刚构建的视图,作为前一个布局中ImageView的侦听器(您在尝试什么)要做什么?!?!)。
  • 你要用fview替换第一个布局(这是空的,你什么也看不见)

您发布的代码将显示一个空白屏幕,因为fview是将在活动中显示的最终视图,而您为ImageView设置的侦听器是无用的,因为{{ 1}}不再出现在屏幕上了。

我再次建议您改进代码。

答案 1 :(得分:0)

首先应该初始化图像变量: image= (ImageView ) findViewById(R.id.imageview);

然后致电fview = new FirstView(this);

答案 2 :(得分:0)

在你的oncreate中使用这一行希望它会有所帮助

image = (ImageView)findViewById(R.id.your_image_view_id);

答案 3 :(得分:0)

在这里,您创建了一个名为fview的新视图,但是没有实现它,所以java不知道那里有什么。

fview = new FirstView(this);
setContentView(fview);

您必须使用ImageView进行布局(或使用您创建的)并使用该布局,以便您可以使用您的图像。现在你的图像漂浮在某个地方,但不在你的视野中

此外,您的图像未创建 - 它为空或更糟 - 一些垃圾。这段代码真的很乱,不会起作用。我建议您至少阅读Android文档的第一章,了解有关活动,布局和其他基本内容:http://developer.android.com/guide/index.html