-3

我在 Eclipse 中的代码没有错误,但是当我在我的设备上运行它时,它说:“不幸的是,SMSFree 已停止。”

这是我的日志猫的副本:

07-04 10:57:11.773: E/AndroidRuntime(17647): Caused by: java.lang.NullPointerException

这是我的源代码的副本:

package com.johnnydicamillo.freesms;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class SMSFreeActivity extends Activity implements
        OnItemSelectedListener, OnClickListener {
    /** Called when the activity is first created. */
    Button send;
    EditText message, number;
    WebView wv;
    Spinner spinner;
    String[] carriers = { "Sprint", "Verizon", "AT&T", "Boost", "T-Mobile" };
    String carrierAddy;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    send = (Button) findViewById(R.id.bSend);
    message = (EditText) findViewById(R.id.etMessage);
    number = (EditText) findViewById(R.id.etNumber);
    wv = (WebView) findViewById(R.id.wvGmail);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setLoadWithOverviewMode(true);
    wv.getSettings().setUseWideViewPort(true);
    wv.getSettings().setBuiltInZoomControls(true);
    send.setOnClickListener(this);
    try {
        wv.loadUrl("http://www.gmail.com");
    } catch (Exception e) {
        e.printStackTrace();
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            SMSFreeActivity.this, android.R.layout.simple_spinner_item,
            carriers);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    int position = spinner.getSelectedItemPosition();
    switch (position) {
    case 0:
        carrierAddy = "@messaging.sprintpcs.com";
        break;
    case 1:
        carrierAddy = "@vtext.com";
        break;
    case 2:
        carrierAddy = "@txt.att.net";
        break;
    case 3:
        carrierAddy = "@myboostmobile.com";
        break;
    case 4:
        carrierAddy = "@tmomail.net";
        break;
    }
}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

public void onClick(View v) {
    // TODO Auto-generated method stub

    }
}

我假设没有人会出于尊重而窃取此代码。谢谢你们的帮助。我正在开始android开发人员。

4

1 回答 1

3

I don't see where your define spinner in your onCreate(). You need something like

spinner = (Spinner) findViewById(R.id.spinner);

before:

spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
于 2012-07-04T18:08:24.783 回答