这是我的MainActivity.java代码 在这里,我正在创建一个非常基本的LoginApp。当应用程序运行时,它“不幸地停止了工作错误......”
package com.example.loginapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
EditText t1,t2;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1= (Button) b1.findViewById(R.id.button1);
t1=(EditText) t1.findViewById(R.id.editText1);
t2=(EditText) t2.findViewById(R.id.editText2);
}
@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 void onClick(View arg0)
{
//String user;
//user = t1.setText("user");
if (arg0.findViewById(R.id.button1)==b1)
{
if ((boolean)((t1.getText().toString()=="user")) && (t2.getText().toString()=="user"))
{
Toast.makeText(this, "Login Sucessful", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
t1.setText("");
t2.setText("");
}
}
}
}
以下是activity_main.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:gravity="center"
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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/editText1"
android:text="Username"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_centerVertical="true"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="115dp"
android:layout_marginTop="22dp"
android:ems="10" >
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="numberPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginTop="46dp"
android:layout_toRightOf="@+id/textView2"
android:clickable="true"
android:onClick="onClick"
android:text="Login" />
</RelativeLayout>
AndroidManifest.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.loginapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.loginapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1= (Button) findViewById(R.id.button1);
t1=(EditText) findViewById(R.id.editText1);
t2=(EditText) findViewById(R.id.editText2);
}
这对你有帮助。 修改完代码后,它对我来说很好。请尝试让我知道。