-11

当我按下运行我的应用程序时,我收到此消息。有人请帮帮我吗?我在这里发布我的 LogCat:

09-10 21:40:54.102: E/AndroidRuntime(11894): FATAL EXCEPTION: main
09-10 21:40:54.102: E/AndroidRuntime(11894): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shoppingapp/com.example.shoppingapp.CurrencyConverter}: java.lang.NullPointerException
09-10 21:40:54.102: E/AndroidRuntime(11894):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at android.app.ActivityThread.access$700(ActivityThread.java:143)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at android.os.Looper.loop(Looper.java:137)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at android.app.ActivityThread.main(ActivityThread.java:4963)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at java.lang.reflect.Method.invokeNative(Native Method)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at java.lang.reflect.Method.invoke(Method.java:511)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at dalvik.system.NativeStart.main(Native Method)
09-10 21:40:54.102: E/AndroidRuntime(11894): Caused by: java.lang.NullPointerException
09-10 21:40:54.102: E/AndroidRuntime(11894):    at com.example.shoppingapp.CurrencyConverter.onCreate(CurrencyConverter.java:36)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at android.app.Activity.performCreate(Activity.java:5184)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-10 21:40:54.102: E/AndroidRuntime(11894):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
09-10 21:40:54.102: E/AndroidRuntime(11894):    ... 11 more

我在这里发布 CurrencyConverter 类。我已经看到了第 36 行。可能是因为我在 R.java 中找不到 simple_spinner_item 导致的错误...我该怎么办?

package com.example.shoppingapp;

import com.example.shoppingapp.ConvertECB;
import com.example.shoppingapp.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class CurrencyConverter extends Activity{

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start_app);

        final Button button = (Button) findViewById(R.id.exit);
        final Button count = (Button) findViewById(R.id.ok);
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final EditText curr = (EditText) findViewById(R.id.entry);
        final TextView result = (TextView) findViewById(R.id.converted);
        final TextView date = (TextView) findViewById(R.id.date);
        final Spinner spinner = (Spinner) findViewById(R.id.spinner);
        final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);

        // stworzenie parsera

        final ConvertECB cp = new ConvertECB (this);

        date.setText("Last updated: " + cp.getPubDate()
                + ". Data received from ECB.");
        // tablica z symbolami walut
        final String spinner_array[] = new String[cp.currencyCode.size()];

        // przypisanie pobranych symboli do tablicy
        for (int i = 0; i < cp.currencyCode.size(); i++) {
            spinner_array[i] = cp.currencyCode.get(i);
        }

        // ustawienie tablicy z symbolami walut jako listy dla spinnerow oraz
        // wygladu
        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_spinner_item, spinner_array);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner1.setAdapter(adapter);

        // okienko pytajace przy probie wyjscia z programu
        builder.setMessage("Are you sure you want to exit?")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                finish();
                            }
                        })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        // listnery do przyciskow
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                final AlertDialog alert;
                alert = builder.create();
                alert.show();
            }
        });
        count.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {

                    float value = Float.parseFloat(curr.getText().toString());
                    value = value
                            * cp.getRelativeExchange(spinner.getSelectedItem()
                                    .toString(), spinner1.getSelectedItem()
                                    .toString());
                    result.setText(String.valueOf(value));
                } catch (Exception ex) {
                    result.setText(R.string.error);
                }
            }
        });
    }
}
4

1 回答 1

3

java.lang.NullPointerException 是什么意思?

这意味着您正在尝试使用变量,可能是通过在其上设置方法或侦听器。以下是我们的阅读方式。

09-10 21:40:54.102:E/AndroidRuntime(11894):致命异常:主要

这告诉我们应用程序停止工作。第一行告诉我们它是由什么引起的

Caused by: java.lang.NullPointerException

告诉我们一个NPE。然后引用您的项目的下一行是一个很好的起点。

在 com.example.shoppingapp.CurrencyConverter.onCreate(CurrencyConverter.java:36)

第 36 行CurrencyConverter。有些东西在null那里....试着弄清楚是什么以及为什么。如果您需要更多帮助,请发布相关代码。

于 2013-09-10T19:50:37.030 回答