В этой возможности я пытаюсь выполнить простое задание, которое получает EditText
, кнопку и дает намерение, когда я нажимаю кнопку, чтобы позвонить по номеру в EditText
.
Задание начинается, но выдается исключение, подобное этому ...
01-23 14:41:13.760: E/AndroidRuntime(305): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{android.testcall/android.testcall.TestCallActivity}: java.lang.NullPointerException
Скажите, если вам нужна дополнительная информация или код для просмотра ...
кодовый класс:
public class TestCallActivity extends Activity {
private TelephonyManager _CurrTelephonyManager;
private PhoneStateListener phoneListener;
final TextView textview = (TextView) findViewById(R.id.TvResulState);
final EditText phoneNumber = (EditText)findViewById(R.id.EtPhoneNumber);
private Date lastIdleTime;
private String currentPhoneState = "CALL_STATE_IDLE";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.testcallactivity);
phoneListener = new PhoneStateListener()
{
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
lastIdleTime = new Date();
currentPhoneState = "CALL_STATE_IDLE";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
currentPhoneState = "CALL_STATE_OFFHOOK";
break;
case TelephonyManager.CALL_STATE_RINGING:
currentPhoneState = "CALL_STATE_RINGING";
break;
}
textview.setText("Phone State: " + currentPhoneState);
}
};
_CurrTelephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception error) {
// TODO: handle exception
error.printStackTrace();
}
final Button btCall = (Button) findViewById(R.id.BtCallNumber);
btCall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CALL);
String uri = "tel:" + phoneNumber.toString().trim() ;
intent.setData(Uri.parse(uri));
startActivity(intent);
}
});
}
Полный стек ошибок:
01-23 15:25:17.234: E/AndroidRuntime(309): FATAL EXCEPTION: main
01-23 15:25:17.234: E/AndroidRuntime(309): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{android.testcall/android.testcall.TestCallActivity}: java.lang.NullPointerException
01-23 15:25:17.234: E/AndroidRuntime(309): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
01-23 15:25:17.234: E/AndroidRuntime(309): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-23 15:25:17.234: E/AndroidRuntime(309): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-23 15:25:17.234: E/AndroidRuntime(309): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-23 15:25:17.234: E/AndroidRuntime(309): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 15:25:17.234: E/AndroidRuntime(309): at android.os.Looper.loop(Looper.java:123)
01-23 15:25:17.234: E/AndroidRuntime(309): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-23 15:25:17.234: E/AndroidRuntime(309): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 15:25:17.234: E/AndroidRuntime(309): at java.lang.reflect.Method.invoke(Method.java:521)
01-23 15:25:17.234: E/AndroidRuntime(309): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-23 15:25:17.234: E/AndroidRuntime(309): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-23 15:25:17.234: E/AndroidRuntime(309): at dalvik.system.NativeStart.main(Native Method)
01-23 15:25:17.234: E/AndroidRuntime(309): Caused by: java.lang.NullPointerException
01-23 15:25:17.234: E/AndroidRuntime(309): at android.app.Activity.findViewById(Activity.java:1637)
01-23 15:25:17.234: E/AndroidRuntime(309): at android.testcall.TestCallActivity.<init>(TestCallActivity.java:21)
01-23 15:25:17.234: E/AndroidRuntime(309): at java.lang.Class.newInstanceImpl(Native Method)
01-23 15:25:17.234: E/AndroidRuntime(309): at java.lang.Class.newInstance(Class.java:1429)
01-23 15:25:17.234: E/AndroidRuntime(309): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-23 15:25:17.234: E/AndroidRuntime(309): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)