不幸的是应用已经停止java.lang.NullPointerException是什么意思?

时间:2013-09-10 19:46:18

标签: java android eclipse

当我按下运行我的应用程序时,我收到此消息。 CAnanyone请帮帮我吗?我在这里发布我的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);
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:3)

  

java.lang.NullPointerException是什么意思?

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

  

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

这告诉我们该应用程序停止工作。第一行告诉我们

造成了什么
Caused by: java.lang.NullPointerException

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

  

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

第36行CurrencyConverter。有些东西null那里......试图找出原因和原因。如果您需要更多帮助,请发布相关代码。