0

我正在使用 osm 数据开发地图应用程序。我能够显示地图,然后获取用户使用 OSMR 设置的两点之间的路线。到目前为止,我的应用程序运行良好。我现在添加代码以从另一个活动传递捆绑包,然后使用在此捆绑包中传递的数据来计算并在获得道路后显示自定义对话框。传递数据的代码如下。

    tourist_taxi = (Button) findViewById(R.id.btn_tourist);
    normal_taxi = (Button) findViewById(R.id.btn_normal);

    tourist_taxi.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(MenuActivity.this, MapActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("taxi_type", "normal");
            bundle.putDouble("rateperkm", 32.00);
            bundle.putDouble("waiting", 6.40);
            i.putExtras(bundle);
            startActivity(i);

        }
    });

    normal_taxi.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(MenuActivity.this, MapActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("taxi_type", "tourist");
            bundle.putDouble("rateperkm", 438.00);
            bundle.putDouble("waiting", 63.00);
            bundle.putDouble("maxrate", 1026.00);
            i.putExtras(bundle);
            startActivity(i);

        }
    });

计算和显示对话框的方法如下

private void calculateFare(Road road) {

        if (road == null)
            return;

        Bundle bundle = getIntent().getExtras();
        double rateperkm = bundle.getDouble("rateperkm");
        double waitingcharge = bundle.getDouble("waiting");
        double maxrate = bundle.getDouble("maxrate");
        String taxi_type = bundle.getString("taxi_type");

        if (taxi_type.equals("normal")){
            rv = "Rs " + rateperkm + " per Kilometer and Rs " + waitingcharge
                    + " per 2 min waiting";
        }
        if (taxi_type.equals("tourist")){
            rv = "Rs " + rateperkm + " for 5 Km and Rs " + waitingcharge
                    + " for additional km, Rs " + maxrate + "maximum";
        }
        final Dialog d = new Dialog(this, R.style.CustomDialogTheme);
        d.setContentView(R.layout.fare_popup);

        if (road.mStatus == Road.STATUS_DEFAULT) {

            distance_value.setText("Could not get distance");
            rate_value.setText(rv);
            fare_value.setText("Could not calculate fare");
            d.show();           
            return;

        }

        dv = String.valueOf(road.mLength);
        if (taxi_type.equals("normal")){
        double fare = (road.mLength * rateperkm) + 10;
        fv = String.valueOf(fare);
        }
        if (taxi_type.equals("normal")){
            if (road.mLength < 5.00){
                fv = String.valueOf(rateperkm);
            }
            else{
                double fare = ((road.mLength-5)*waitingcharge + rateperkm);
                if (fare > maxrate){
                    fare = maxrate;
                }
                fv = String.valueOf(fare);              
            }
        }
        fare_value.setText(fv);
        distance_value.setText(dv);
        rate_value.setText(rv);
        d.show();

    }

在这个添加之后,我得到了一个 NPE。由于我是新手程序员,我对 NPE 不太熟悉,并且从错误代码中我无法跟踪错误的来源。这是我的错误代码。

06-10 09:46:53.237: W/dalvikvm(384): threadid=1: thread exiting with uncaught exception (group=0x40014760)
06-10 09:46:53.257: E/AndroidRuntime(384): FATAL EXCEPTION: main
06-10 09:46:53.257: E/AndroidRuntime(384): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nirab.merobhadameter/com.nirab.merobhadameter.MapActivity}: java.lang.NullPointerException
06-10 09:46:53.257: E/AndroidRuntime(384):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1672)
06-10 09:46:53.257: E/AndroidRuntime(384):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
06-10 09:46:53.257: E/AndroidRuntime(384):  at android.app.ActivityThread.access$1500(ActivityThread.java:122)
06-10 09:46:53.257: E/AndroidRuntime(384):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
06-10 09:46:53.257: E/AndroidRuntime(384):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 09:46:53.257: E/AndroidRuntime(384):  at android.os.Looper.loop(Looper.java:132)
06-10 09:46:53.257: E/AndroidRuntime(384):  at android.app.ActivityThread.main(ActivityThread.java:4025)
06-10 09:46:53.257: E/AndroidRuntime(384):  at java.lang.reflect.Method.invokeNative(Native Method)
06-10 09:46:53.257: E/AndroidRuntime(384):  at java.lang.reflect.Method.invoke(Method.java:491)
06-10 09:46:53.257: E/AndroidRuntime(384):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-10 09:46:53.257: E/AndroidRuntime(384):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-10 09:46:53.257: E/AndroidRuntime(384):  at dalvik.system.NativeStart.main(Native Method)
06-10 09:46:53.257: E/AndroidRuntime(384): Caused by: java.lang.NullPointerException
06-10 09:46:53.257: E/AndroidRuntime(384):  at android.app.Activity.findViewById(Activity.java:1744)
06-10 09:46:53.257: E/AndroidRuntime(384):  at com.nirab.merobhadameter.MapActivity.<init>(MapActivity.java:65)
06-10 09:46:53.257: E/AndroidRuntime(384):  at java.lang.Class.newInstanceImpl(Native Method)
06-10 09:46:53.257: E/AndroidRuntime(384):  at java.lang.Class.newInstance(Class.java:1301)
06-10 09:46:53.257: E/AndroidRuntime(384):  at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
06-10 09:46:53.257: E/AndroidRuntime(384):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1663)
06-10 09:46:53.257: E/AndroidRuntime(384):  ... 11 more

它会很高兴知道如何处理这种情况以及正确的处理方法。提前致谢。

编辑:问题是我试图在不执行 setContentView 的情况下访问 TextView 元素。这是我为 calculateFare 方法更新的代码。

private void calculateFare(Road road) {

    if (road == null)
        return;

    final Dialog d = new Dialog(this, R.style.CustomDialogTheme);
    d.setContentView(R.layout.fare_popup);

    TextView fare_value = (TextView) findViewById(R.id.fare_value);
    TextView rate_value = (TextView) findViewById(R.id.rate_value);
    TextView distance_value = (TextView) findViewById(R.id.distance_value);
    String fv = "0.00" , rv="0.00" , dv="0.00";

    Bundle bundle = getIntent().getExtras();
    double rateperkm = bundle.getDouble("rateperkm");
    double waitingcharge = bundle.getDouble("waiting");
    double maxrate = bundle.getDouble("maxrate");
    String taxi_type = bundle.getString("taxi_type");

    if (taxi_type.equals("normal")){
        rv = "Rs " + rateperkm + " per Kilometer and Rs " + waitingcharge
                + " per 2 min waiting";
    }
    if (taxi_type.equals("tourist")){
        rv = "Rs " + rateperkm + " for 5 Km and Rs " + waitingcharge
                + " for additional km, Rs " + maxrate + "maximum";
    }


    if (road.mStatus == Road.STATUS_DEFAULT) {

        distance_value.setText("Could not get distance");
        rate_value.setText(rv);
        fare_value.setText("Could not calculate fare");
        d.show();           
        return;

    }

    dv = String.valueOf(road.mLength);
    if (taxi_type.equals("normal")){
    double fare = (road.mLength * rateperkm) + 10;
    fv = String.valueOf(fare);
    }
    if (taxi_type.equals("normal")){
        if (road.mLength < 5.00){
            fv = String.valueOf(rateperkm);
        }
        else{
            double fare = ((road.mLength-5)*waitingcharge + rateperkm);
            if (fare > maxrate){
                fare = maxrate;
            }
            fv = String.valueOf(fare);              
        }
    }

    fare_value.setText(fv);
    distance_value.setText(dv);
    rate_value.setText(rv);
    d.show();

}

现在我在 fare_value.setText(fv); 行得到一个 NPE。

4

0 回答 0