按钮发送时出现空指针异常

时间:2014-05-13 13:22:46

标签: java android eclipse

请在下面找到主要课程

package com.example.myfirstapp;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class MainActivity extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

public void addListenerOnSpinnerItemSelection() {
    spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

Button buttonSend;
EditText textTo;
EditText textSubject;
EditText textMessageContact;
EditText textMessageEmail;
EditText textMessageAmount;
Spinner spinner;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new    PlaceholderFragment()).commit();
    }

    buttonSend = (Button) findViewById(R.id.buttonSend);
    textTo = (EditText) findViewById(R.id.editTextSendTo);
    textSubject = (EditText) findViewById(R.id.editTextName);
    textMessageContact = (EditText) findViewById(R.id.editTextContact);
    textMessageEmail = (EditText) findViewById(R.id.editTextEmail);
    textMessageAmount = (EditText) findViewById(R.id.editTextAmount);
    spinner = (Spinner) findViewById(R.id.spinner);
    buttonSend.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            String to = textTo.getText().toString();
            String subject = textSubject.getText().toString();

            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
            email.putExtra(Intent.EXTRA_SUBJECT, subject);
            email.setType("message/rfc822");
            startActivity(Intent.createChooser(email,
                    "Choose an Email client :"));

        }
    });
}

  }

另请参阅下面的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<!-- android:background="@drawable/mmm_bg" -->

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="40dip" >

    <TableRow>

        <TextView
            android:id="@+id/textViewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dip"
            android:text="Name:"
            android:textColor="#FF0000"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editTextName"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center_horizontal"
            android:layout_weight="3"
            android:ems="10"
            android:hint="enter the name"
            android:textColor="#000000"
            android:textStyle="bold" >

            <requestFocus />
        </EditText>
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textViewContact"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dip"
            android:text="Contact:"
            android:textColor="#FF0000"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editTextContact"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center_horizontal"
            android:layout_weight="3"
            android:ems="10"
            android:hint="enter the contact no."
            android:textStyle="bold" >

            <requestFocus />
        </EditText>
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textViewEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dip"
            android:text="Email: "
            android:textColor="#FF0000"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editTextEmail"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center_horizontal"
            android:layout_weight="3"
            android:ems="10"
            android:hint="enter the email address"
            android:textColorHint="#008080" >

            <requestFocus />
        </EditText>
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textViewProduct"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dip"
            android:text="Product:"
            android:textColor="#FF0000"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/product_array" />

        <requestFocus />

        <EditText>
        </EditText>
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textViewAmount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dip"
            android:text="Amount:"
            android:textColor="#FF0000"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editTextAmount"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center_horizontal"
            android:layout_weight="3"
            android:ems="10"
            android:hint="enter the amount"
            android:textStyle="bold" >

            <requestFocus />
        </EditText>
    </TableRow>

    <TableRow>

        <TextView
            android:id="@+id/textViewSendTo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dip"
            android:text="Send To:"
            android:textColor="#FF0000"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editTextSendTo"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center_horizontal"
            android:layout_weight="3"
            android:ems="10"
            android:hint="reciever&apos;s email address"
            android:textStyle="bold" >

            <requestFocus />
        </EditText>
    </TableRow>
  </TableLayout>

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="3" >

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Send" />
   </LinearLayout>

  </LinearLayout>

请在下面找到log cat

05-13 09:03:49.935: E/AndroidRuntime(14062): Caused by: java.lang.NullPointerException
05-13 09:03:49.935: E/AndroidRuntime(14062):    at           com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:89)
05-13 09:03:49.935: E/AndroidRuntime(14062):    at   android.app.Activity.performCreate(Activity.java:5231)
05-13 09:03:49.935: E/AndroidRuntime(14062):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-13 09:03:49.935: E/AndroidRuntime(14062):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-13 09:03:49.935: E/AndroidRuntime(14062):    ... 11 more
05-13 09:08:50.395: I/Process(14062): Sending signal. PID: 14062 SIG: 9

我正在尝试创建一个具有4到5个文本框和微调器和发送按钮的应用程序。 点击发送按钮后,电子邮件应转到收件人,并在文本框中输入详细信息。但是会出现空指针异常。 我无法解决问题。为什么NPE正在发生以及如何解决它。

提前致谢。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

空指针异常非常常见,意味着您正在尝试访问null为对象的方法或属性。显然,不可能调用这样的方法(因为它不存在)。

我怀疑textTo或textSubject是null,因为它们是用户定义的对象。如果在设置变量textTo的行上设置断点(例如),则应注意它为空。

当您发现值为null时,您可以弄清楚如何纠正这种情况。如果结果是像'textTo'这样的变量,那么检查资源是否确实存在,或者将其投射到正确的对象

答案 1 :(得分:-4)

在声明if中的所有观看次数后,只需写下onCreate()循环,如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     buttonSend = (Button) findViewById(R.id.buttonSend);
    textTo = (EditText) findViewById(R.id.editTextSendTo);
    textSubject = (EditText) findViewById(R.id.editTextName);
    textMessageContact = (EditText) findViewById(R.id.editTextContact);
    textMessageEmail = (EditText) findViewById(R.id.editTextEmail);
    textMessageAmount = (EditText) findViewById(R.id.editTextAmount);
    spinner = (Spinner) findViewById(R.id.spinner);
    buttonSend.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            String to = textTo.getText().toString();
            String subject = textSubject.getText().toString();

            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
            email.putExtra(Intent.EXTRA_SUBJECT, subject);
            email.setType("message/rfc822");
            startActivity(Intent.createChooser(email,
                    "Choose an Email client :"));

        }
    });

 if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new    PlaceholderFragment()).commit();
    }
}
相关问题