从另一个Activity调用一个Activity中的静态方法

时间:2013-02-02 17:06:59

标签: java android

我在X.class中有这些方法:

    public void sendMessage(String message) {
    // Check that we're actually connected before trying anything
    if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
        Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
        return;
    }

    // Check that there's actually something to send
    if (message.length() > 0) {
        // Get the message bytes and tell the BluetoothChatService to write
        byte[] send = message.getBytes();
        mChatService.write(send);

        // Reset out string buffer to zero and clear the edit text field
        mOutStringBuffer.setLength(0);
        //mOutEditText.setText(mOutStringBuffer);
    }
}

public static void sendMessage1(String message1){

    call1.sendMessage(message1);
}

我有以下toggleSwitch,我点击另一个活动,即Y.class:

tb3.setOnClickListener(new OnClickListener() {
        public void onClick(View v){
            if (tb3.isChecked()){
                String message = "u";
                X.sendMessage1(message);
                //clearer();
            } else if (!tb3.isChecked()){
                String message = "y";
                //clearer();
            }
        }
    });

虽然我在编码时没有收到任何错误但是当我在Android上加载应用程序然后点击这个toggleswitch我的应用程序死了?

日志:

E/AndroidRuntime( 4866): java.lang.NullPointerException
E/AndroidRuntime( 4866):    at com.winacro.andRHOME.andRHOME.sendMessage1(andRHOME.java:282)
E/AndroidRuntime( 4866):    at com.winacro.andRHOME.EECA2$1.onClick(EECA2.java:57)
E/AndroidRuntime( 4866):    at android.view.View.performClick(View.java:3534)
E/AndroidRuntime( 4866):    at android.widget.CompoundButton.performClick(CompoundButton.java:104)
E/AndroidRuntime( 4866):    at android.view.View$PerformClick.run(View.java:14263)
E/AndroidRuntime( 4866):    at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime( 4866):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 4866):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 4866):    at android.app.ActivityThread.main(ActivityThread.java:4441)
E/AndroidRuntime( 4866):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 4866):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 4866):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 4866):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime( 4866):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  273):   Force finishing activity com.winacro.andRHOME/.EECA2

0 个答案:

没有答案