0

我正在尝试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

4 回答 4

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.
  • 然后,您构建一个实现 的简单视图,OnTouchListener并且在此视图的构造函数中,您将刚刚构建的视图设置为ImageView上一个布局的侦听器(您要做什么?!?!)。
  • 你比替换第一个布局fview(那是空的,你什么都看不到)

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

我再次建议您改进代码。

于 2012-05-02T07:23:01.220 回答
0

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

然后打电话fview = new FirstView(this);

于 2012-05-02T07:22:41.653 回答
0

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

image = (ImageView)findViewById(R.id.your_image_view_id);
于 2012-05-02T07:22:43.163 回答
0

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

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

您必须在其中使用 ImageView 进行布局(或使用您创建的)并使用该布局,这样您就可以使用您的图像。现在您的图像漂浮在某处,但不在您的视野中

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

于 2012-05-02T07:42:05.823 回答