我有一个调用共享意图的按钮操作。我想从strings.xml
构造一个字符串下面我有我的字符串:
<string name="app_name">NowIcons</string> <string name="market">market://details?id=com.nmiltner.nowicon</string>
我的字符串构造函数在这里:
public String share = "Check out " + getResources().getString(R.string.app_name) + getResources().getString(R.string.market);
按钮点击此处:
btn_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launching Share Intent
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, share);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
});
当我测试应用程序时,我得到一个强制关闭,错误日志有一个空指针异常。
我也使用类似的方法将用户发送到Play商店以对应用进行评级。
public String marketurl = getResources().getString(R.string.market);
btn_rate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Go to market Listing
Uri uri1 = Uri.parse(marketurl);
startActivity(new Intent(Intent.ACTION_VIEW, uri1));
}
});
固定!!!
编辑:
我能够解决这个问题,这是修复:
public String marketurl;
public String share;
btn_rate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Go to market Listing
String marketurl = getResources().getString(R.string.market);
Uri uri1 = Uri.parse(marketurl);
startActivity(new Intent(Intent.ACTION_VIEW, uri1));
}
});
btn_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launching Share Intent
String share = "Check out " + getResources().getString(R.string.app_name) + " " + getResources().getString(R.string.market) + " on the Play Store";
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, share);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
});