我想将电话号码显示到textView然后屏蔽最后4位数,如果可能,但我无法正常运行,应用程序突然停止...这里是我想要的示例/参考代码做。如果使用toast它会显示数字但是当我使用intent时我的代码不起作用:运行时错误。
发件人(第二活动)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class MyCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Intent a = new Intent(context,MainActivity.class);
a.putExtra("key", incomingNumber);
context.startActivity(a);
}
主要活动
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv1,tv2;
String gotBread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
tv1.setSelected(true);
Bundle gotBasket = getIntent().getExtras();
gotBread = gotBasket.getString("key");
tv2.setText(gotBread);
}
public void initialize() {
// TODO Auto-generated method stub
tv1 = (TextView) this.findViewById(R.id.tV1);
tv2 = (TextView) this.findViewById(R.id.tV2);
}
}
这是我的logcat希望它有帮助...
08-18 02:50:10.121:D / AndroidRuntime(1306):关闭VM 08-18 02:50:10.121:W / dalvikvm(1306):threadid = 1:线程退出 未捕获的异常(组= 0xb2a3fba8)08-18 02:50:10.131: E / AndroidRuntime(1306):致命异常:主08-18 02:50:10.131: E / AndroidRuntime(1306):进程:com.example.detectincomingcall,PID: 1306 08-18 02:50:10.131:E / AndroidRuntime(1306): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.detectincomingcall / com.example.detectincomingcall.MainActivity}: java.lang.NullPointerException 08-18 02:50:10.131: E / AndroidRuntime(1306):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 08-18 02:50:10.131:E / AndroidRuntime(1306):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 08-18 02:50:10.131:E / AndroidRuntime(1306):at android.app.ActivityThread.access $ 800(ActivityThread.java:135)08-18 02:50:10.131:E / AndroidRuntime(1306):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 08-18 02:50:10.131:E / AndroidRuntime(1306):at android.os.Handler.dispatchMessage(Handler.java:102)08-18 02:50:10.131:E / AndroidRuntime(1306):at android.os.Looper.loop(Looper.java:136)08-18 02:50:10.131: E / AndroidRuntime(1306):at android.app.ActivityThread.main(ActivityThread.java:5017)08-18 02:50:10.131:E / AndroidRuntime(1306):at java.lang.reflect.Method.invokeNative(Native Method)08-18 02:50:10.131:E / AndroidRuntime(1306):at java.lang.reflect.Method.invoke(Method.java:515)08-18 02:50:10.131: E / AndroidRuntime(1306):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 08-18 02:50:10.131:E / AndroidRuntime(1306):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)08-18 02:50:10.131:E / AndroidRuntime(1306):at dalvik.system.NativeStart.main(Native Method)08-18 02:50:10.131: E / AndroidRuntime(1306):引起:java.lang.NullPointerException 08-18 02:50:10.131:E / AndroidRuntime(1306):at com.example.detectincomingcall.MainActivity.onCreate(MainActivity.java:21) 08-18 02:50:10.131:E / AndroidRuntime(1306):at android.app.Activity.performCreate(Activity.java:5231)08-18 02:50:10.131:E / AndroidRuntime(1306):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 08-18 02:50:10.131:E / AndroidRuntime(1306):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 08-18 02:50:10.131:E / AndroidRuntime(1306):... 11更多
答案 0 :(得分:0)
转动评估并使用yoda notation。这样可以确保您尝试执行null.equals()
时无法获得NPE。还要确保检查参数,使其不为null。 Java从左到右有严格的评估顺序,因此您可以使用short-circuit evaluation。
if (null != intent && TelephonyManager.EXTRA_STATE_RINGING.equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE)))
在你的onCreate()中你可能会认为有点偏执
Intent intent = getIntent();
if (null != intent) {
Bundle gotBasket = intent.getExtras();
if (null != gotBasket) {
gotBread = gotBasket.getString("key");
tv2.setText(gotBread);
}
}