无法实例化活动

时间:2012-01-23 18:21:05

标签: android android-layout android-intent android-activity

在这个机会中,我尝试做一个简单的活动,当我按下按钮拨打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)

4 个答案:

答案 0 :(得分:1)

你调用findViewById的方式,它发生在setContentView之前。在调用setContentView之后将这些调用移动到onCreate方法中,并且文本视图不会为null

答案 1 :(得分:0)

onCreate之后的setContentView方法中移动这些内容:

final TextView textview = (TextView) findViewById(R.id.TvResulState);
final EditText phoneNumber = (EditText)findViewById(R.id.EtPhoneNumber);

另外,请移除try ... catch。它没有用。

答案 2 :(得分:0)

您试图在膨胀XML之前启动视图:

final TextView textview = (TextView) findViewById(R.id.TvResulState);
final EditText phoneNumber = (EditText)findViewById(R.id.EtPhoneNumber);

尝试在onCreate()

之后的setContentView()中启动它们
    ...
    setContentView(R.layout.testcallactivity);
    textview = (TextView) findViewById(R.id.TvResulState);
    phoneNumber = (EditText)findViewById(R.id.EtPhoneNumber);

    ...

答案 3 :(得分:0)

您还需要按如下方式初始化_CurrTelephonyManager变量

_CurrTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
行前

_CurrTelephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
相关问题