我的android应用程序保持强制关闭

时间:2012-07-04 18:06:35

标签: android

我在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开发人员。

1 个答案:

答案 0 :(得分:3)

我没有在你的onCreate()中看到你定义spinner的位置。你需要像

这样的东西
spinner = (Spinner) findViewById(R.id.spinner);

之前:

spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);