gpt4 book ai didi

java - 未找到 GetSherlockActivity

转载 作者:行者123 更新时间:2023-12-01 13:26:31 27 4
gpt4 key购买 nike

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

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'

编辑2:主文件:

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("12345678@s.whatsapp.net");
}

@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

关于java - 未找到 GetSherlockActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21791118/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com