我刚开始编写一个小密码管理器,但每次我在模拟器中运行它时,它都会崩溃并且我收到以下错误:
android.widget.TextView 无法转换为 android.widget.EditText
每个人都看到了错误吗?
MainActivity.java:
package de.lennartschoch.passwordmanager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button login;
EditText masterpassfield;
String masterpass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
masterpassfield = (EditText) findViewById(R.id.masterpass);
masterpass = "password";
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(masterpassfield.getText().toString() == masterpass) {
Intent success = new Intent(view.getContext(), Passwords.class);
startActivityForResult(success, 0);
}
}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
活动主.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"
tools:context=".MainActivity" >
<EditText
android:id="@+id/masterpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignRight="@+id/textView1"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/masterpass"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="@string/login" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/masterpass"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="@string/masterpasstext"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>