EditText不起作用

时间:2016-11-27 09:50:46

标签: android

这是我的代码

protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        markerlocationtext=(TextView)findViewById(R.id.locationMarkertext);

        final LocationManager manager=(LocationManager)getSystemService(LOCATION_SERVICE);

        if(googleServicesAvailabile()){
            setContentView(R.layout.activity_maps);
            iniMap();
        }

        if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            buildAlertMessageNoGps();
        }
        markerlocationtext.setText("Lat : "); //i put any word for test but not work
}

我在运行我的应用程序时出现此错误

  

11-27 12:38:03.628 16781-16781 / com.project.ytubasi.speedtaxi   E / AndroidRuntime:致命异常:主要                                                                                  处理:com.project.ytubasi.speedtaxi,PID:16781                                                                                  java.lang.RuntimeException:无法启动活动   ComponentInfo {com.project.ytubasi.speedtaxi / com.project.ytubasi.speedtaxi.MapsActivity}:   显示java.lang.NullPointerException                                                                                      在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)                                                                                      在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)                                                                                      在android.app.ActivityThread.access $ 800(ActivityThread.java:151)                                                                                      在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1223)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                      在android.os.Looper.loop(Looper.java:136)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:5293)                                                                                      at java.lang.reflect.Method.invokeNative(Native Method)                                                                                      在java.lang.reflect.Method.invoke(Method.java:515)                                                                                      在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:855)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:671)                                                                                      at dalvik.system.NativeStart.main(Native Method)                                                                                   引起:java.lang.NullPointerException                                                                                      在   com.project.ytubasi.speedtaxi.MapsActivity.onCreate(MapsActivity.java:65)                                                                                      在android.app.Activity.performCreate(Activity.java:5287)                                                                                      在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)                                                                                      在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2186)                                                                                      在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)                                                                                      在android.app.ActivityThread.access $ 800(ActivityThread.java:151)                                                                                      在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1223)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                      在android.os.Looper.loop(Looper.java:136)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:5293)                                                                                      at java.lang.reflect.Method.invokeNative(Native Method)                                                                                      在java.lang.reflect.Method.invoke(Method.java:515)                                                                                      在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:855)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:671)                                                                                      在dalvik.system.NativeStart.main(本地方法)

2 个答案:

答案 0 :(得分:0)

您尚未设置包含textView的资源xml文件..

setContentView(R.layout.yourXmlFile);

答案 1 :(得分:0)

您必须在使用setContentView进行任何视图绑定之前调用findViewById方法 所以你必须在super.onCreate(savedInstanceState);之后移到代码行下面 像这样:

protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps); <-------

        markerlocationtext=(TextView)findViewById(R.id.locationMarkertext);

        final LocationManager manager=(LocationManager)getSystemService(LOCATION_SERVICE);

        if(googleServicesAvailabile()){
            iniMap();
        }

        if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            buildAlertMessageNoGps();
        }
        markerlocationtext.setText("Lat : "); //i put any word for test but not work
}