我一直在为此奋斗2天,有人可以就如何解决它提出建议吗?
这是我的WhatsApp2活动......
public class WhatsApp2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void whatsappSend(){
try {
String toNumber = "xxxxxxxxxx";
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + "" + toNumber+ "?body=" + ""));
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
}
catch (Exception e){
e.printStackTrace();
}
}
}
我试图从另一个这样的活动中调用whatsappSend()
......
public class MainActivity extends AppCompatActivity {
public WhatsApp2 whatsApp2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void whatsapp(View view) {
whatsApp2 = new WhatsApp2();
whatsApp2.whatsappSend();
}
}
它在WhatsApp2类中的Catch
异常错误如下:
java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.app.ActivityThread $ ApplicationThread android.app.ActivityThread.getApplicationThread()'
**Try this if you want call whatsapp method from another activity**
//MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Whatspp send=new Whatspp(this);
send.whatsappSend();
}
}
//Whatspp Class
public class Whatspp {
Context _cContext;
public Whatspp(MainActivity mainActivity) {
_cContext=mainActivity;
}
void whatsappSend() {
try {
String toNumber = "**********";
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + "" + toNumber + "?body=" + ""));
sendIntent.setPackage("com.whatsapp");
_cContext.startActivity(sendIntent);
} catch (Exception e) {
Log.i("TAG", "whatsappSend: "+e.getMessage());
e.printStackTrace();
}
}
}