我正在尝试使用此代码通过whatsapp发送消息。我遇到的问题是,“对于类型Send,未定义方法getSherlockActivity()”。我应该重命名发送还是需要导入某些内容?蚀的唯一建议就是创建它。

import com.actionbarsherlock.app.SherlockActivity;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.widget.Toast;

public class Send extends SherlockActivity{

    public void sendWhatsAppMessageTo(String whatsappid) {

        Cursor c = getSherlockActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",
                new String[] { whatsappid }, null);
        c.moveToFirst();

        Intent whatsapp = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
        c.close();

         if (whatsapp != null) {

        startActivity(whatsapp);

        } else {
                Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                        .show();
        //download for example after dialog
                        Uri uri = Uri.parse("market://details?id=com.whatsapp");
                        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
            }

        }

}


编辑:删除getSherlockActivity()后的Logcat输出为:

02-15 02:05:10.647: D/AndroidRuntime(2831): Shutting down VM
02-15 02:05:10.647: W/dalvikvm(2831): threadid=1: thread exiting with uncaught exception (group=0x40a151f8)
02-15 02:05:10.647: E/AndroidRuntime(2831): FATAL EXCEPTION: main
02-15 02:05:10.647: E/AndroidRuntime(2831): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.wiers.whatsapp_api/de.wiers.whatsapp_api.MainActivity}: java.lang.NullPointerException
02-15 02:05:10.647: E/AndroidRuntime(2831):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at android.os.Looper.loop(Looper.java:137)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at java.lang.reflect.Method.invokeNative(Native Method)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at java.lang.reflect.Method.invoke(Method.java:511)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at dalvik.system.NativeStart.main(Native Method)
02-15 02:05:10.647: E/AndroidRuntime(2831): Caused by: java.lang.NullPointerException
02-15 02:05:10.647: E/AndroidRuntime(2831):     at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:91)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at de.wiers.whatsapp_api.Send.sendWhatsAppMessageTo(Send.java:15)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at de.wiers.whatsapp_api.MainActivity.onCreate(MainActivity.java:15)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at android.app.Activity.performCreate(Activity.java:4492)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-15 02:05:10.647: E/AndroidRuntime(2831):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-15 02:05:10.647: E/AndroidRuntime(2831):     ... 11 more
02-15 02:05:11.527: I/dalvikvm(2831): threadid=3: reacting to signal 3
02-15 02:05:11.537: I/dalvikvm(2831): Wrote stack traces to '/data/anr/traces.txt'
02-15 02:05:11.547: I/dalvikvm(2831): threadid=3: reacting to signal 3
02-15 02:05:11.547: I/dalvikvm(2831): Wrote stack traces to '/data/anr/traces.txt'


EDIT2:主文件:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import de.wiers.whatsapp_api.Send;
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Send snd = new Send();
        snd.sendWhatsAppMessageTo("[email protected]");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



}

最佳答案

根据this,您可以将getSherlockActivity更改为this

09-19 00:42