简单的程序强制关闭

时间:2013-11-25 19:16:38

标签: java android

我正在使用Eclipse制作Android程序。这是我的MainAcitvity.java代码:

package com.example.changeme;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button set = (Button)findViewById(R.id.set);
    TextView out = (TextView)findViewById(R.id.out);
    EditText in = (EditText)findViewById(R.id.input);

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

        set.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                setcontent();
            }
        });
    }
    public void setcontent()
    {
        String con = in.getText().toString();
        out.setText(con);
    }

    @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 false;
    }

}

主要活动的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/out"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/set"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="37dp"
    android:text="@string/Change"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/set"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/input"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:text="@string/Set" />

<EditText
    android:id="@+id/input"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp"
    android:ems="10"
    android:inputType="textPersonName" />

当我在N4上运行程序时崩溃。

1 个答案:

答案 0 :(得分:1)

在检索元素之前,您需要为布局充气,否则findViewById将返回null,因此行set.setOnClickListener(/**/);会抛出NullPointerException这会让你的应用崩溃。

Button set;
TextView out;
EditText in;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);  //<-- the layout is inflated, i.e it's rendered

    /******************************************************/
    //Now you can retrieve your elements from this layout
    set = (Button)findViewById(R.id.set); 
    out = (TextView)findViewById(R.id.out);
    in = (EditText)findViewById(R.id.input);
    /*****************************************************/

    set.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            setcontent();
        }
    });
}