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