当我尝试在onClick()上创建此代码时,为什么我的应用程序关闭?

时间:2014-09-18 05:36:50

标签: android onclick

我试图比较两个EditTexts的两个值,但要创建按钮并运行项目,它会出错并关闭应用程序:

public class MainActivity extends ActionBarActivity implements OnClickListener {

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

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

        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                final EditText username = (EditText) findViewById(R.id.editText1);
                final EditText password = (EditText) findViewById(R.id.editText2);

                if (username.getText().toString() == "admin"
                        && password.getText().toString() == "admin") {

                    Context context = getApplicationContext();
                    CharSequence text = "Logado com sucesso!";
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();

                } else {
                    Context context = getApplicationContext();

                    CharSequence text = "Erro! Dados inválidos";
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();

                }

            }
        });
    }

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

    //
    // @Override
    // public void onClick(DialogInterface dialog, int which) {
    // // TODO Auto-generated method stub
    //
    // final EditText username = (EditText) findViewById(R.id.editText1);
    // final EditText password = (EditText) findViewById(R.id.editText2);
    //
    // if (username.getText().toString() == "admin"
    // && password.getText().toString() == "admin") {
    //
    // Context context = getApplicationContext();
    // CharSequence text = "Logado com sucesso!";
    // int duration = Toast.LENGTH_SHORT;
    //
    // Toast toast = Toast.makeText(context, text, duration);
    // toast.show();
    //
    // } else {
    // Toast.makeText(
    // this,
    // "Your username or password is not correct! Please, insert again",
    // Toast.LENGTH_SHORT).show();
    //
    // }
    //
    // }

    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        // TODO Auto-generated method stub

    }
}

这是我的xml:

<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"
    android:background="#1c5a6d"
    tools:context="com.isckosta.app.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/editText1"
        android:layout_marginTop="104dp"
        android:gravity="left"
        android:text="@string/usuario"
        android:textSize="25sp"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:text="@string/senha"
        android:textSize="25sp"
        android:textColor="#ffffff"/>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/textView2"
        android:ems="10"
        android:inputType="textPassword"
        android:textSize="16sp"
        android:hint="@string/digite_sua_senha_"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:text="@string/entrar"
        android:textSize="20sp"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/button2"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:text="@string/criar_uma_conta"
        android:textColor="#ffffff" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:ems="8"
        android:hint="@string/digite_seu_usuario"
        android:textSize="16sp"
        android:textColor="#ffffff" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:gravity="center_horizontal"
        android:text="@string/welcome"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="20sp"
        android:textColor="#ffffff" />

</RelativeLayout>

我不知道还能做什么:(

0 个答案:

没有答案