选项卡上的Android通话功能点击?

时间:2014-04-02 17:28:48

标签: android android-tabhost

我正在尝试调用函数来打开一个活动,但它无法正常工作。请给我一些解决方案

我正在尝试此代码,但显示空指针异常

此行显示错误“tabHost.getChildAt(2).setOnTo”

public class Home extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        TabHost tabHost = getTabHost();   


        TabSpec photospec = tabHost.newTabSpec("Contacts");
        // setting Title and Icon for the Tab
        photospec.setIndicator("Contacts", getResources().getDrawable(android.R.drawable.ic_menu_slideshow));
        Intent photosIntent = new Intent(this, Contacts.class);
        photospec.setContent(photosIntent);


        TabSpec songspec = tabHost.newTabSpec("Notification");       
        songspec.setIndicator("Notification", getResources().getDrawable(android.R.drawable.ic_menu_revert));
        Intent songsIntent = new Intent(this, Notifications.class);
        songspec.setContent(songsIntent);


        TabSpec videospec = tabHost.newTabSpec("More"); 
        videospec.setIndicator("More", getResources().getDrawable(android.R.drawable.ic_menu_more));
        Intent videosIntent = new Intent(this, Login.class);
        videospec.setContent(videosIntent);


        tabHost.addTab(photospec);  
        tabHost.addTab(songspec);  
        tabHost.addTab(videospec);  

        tabHost.getChildAt(2).setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();

                if(action == MotionEvent.ACTION_UP)
                {
                    Toast.makeText(getApplicationContext(), "ss", 3000).show();
                    //Something to do

                    return true; // do this if you dont want the tab to change
                }
                return false;
            }
        });

    }
}

请给我一些解决方案

谢谢

堆栈跟踪:

04-02 23:02:24.685: E/AndroidRuntime(23622): FATAL EXCEPTION: main
04-02 23:02:24.685: E/AndroidRuntime(23622): Process: com.speedlock, PID: 23622
04-02 23:02:24.685: E/AndroidRuntime(23622): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.speedlock/com.speedlock.Home}: java.lang.NullPointerException
04-02 23:02:24.685: E/AndroidRuntime(23622):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at android.os.Looper.loop(Looper.java:136)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at java.lang.reflect.Method.invoke(Method.java:515)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at dalvik.system.NativeStart.main(Native Method)
04-02 23:02:24.685: E/AndroidRuntime(23622): Caused by: java.lang.NullPointerException
04-02 23:02:24.685: E/AndroidRuntime(23622):    at com.speedlock.Home.onCreate(Home.java:50)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at android.app.Activity.performCreate(Activity.java:5231)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-02 23:02:24.685: E/AndroidRuntime(23622):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-02 23:02:24.685: E/AndroidRuntime(23622):    ... 11 more

1 个答案:

答案 0 :(得分:1)

我已经解决了iisu,请将代码更改为

tabHost.getTabWidget().getChildAt(2).setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();

            if(action == MotionEvent.ACTION_UP)
            {
                Toast.makeText(getApplicationContext(), "ss", 3000).show();
                //Something to do

                return true; // do this if you dont want the tab to change
            }
            return false;
        }
    });