这是一个简单的登录活动,但当它必须切换活动时,应用程序崩溃。 我尝试使用正常形式的pubic void onClick(){...}但它不起作用。
Login.java
package com.example.corrado_mattia_danny.face_offbrains;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity {
private Player player;
private Button login;
private EditText Username;
private EditText Password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login=(Button)findViewById(R.id.button_sign_in);
Username=(EditText)findViewById(R.id.nickname);
Password=(EditText)findViewById(R.id.password);
String a = Username.getText().toString();
String b = Password.getText().toString();
player.inserisciCredenziali(a,b); //insert into a player class
//username and password
signIn();
}
public void signIn() {
Intent intent = new Intent(this, Home.class);
startActivity(intent);
}
}
activity_login.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.corrado_mattia_danny.face_offbrains.Login"
android:background="@drawable/sfondo_custom" >
<EditText
android:layout_width="wrap_content"
android:layout_height="35dp"
android:inputType="textPersonName"
android:hint="Nickname"
android:ems="10"
android:id="@+id/nickname"
android:linksClickable="false"
android:textColor="#ff000000"
android:background="#ffd4d4d4"
android:textStyle="normal|bold|italic"
android:layout_marginTop="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="35dp"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/password"
android:layout_below="@+id/nickname"
android:layout_alignStart="@+id/nickname"
android:layout_marginTop="30dp"
android:hint="password"
android:textColor="#ff000000"
android:background="#ffd4d4d4"
android:textStyle="normal|bold|italic" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/button_sign_in"
android:background="#ffffeb00"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/nickname"
android:layout_marginLeft="10dp"
android:onClick="signIn"/>
</RelativeLayout>
Home.java
Home中的按钮无效。
package com.example.corrado_mattia_danny.face_offbrains;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class Home extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Intent intent = getIntent();
Button bottone_sfida_amico = (Button)findViewById(R.id.button_sfida_amico);
Button bottone_avversario_casuale = (Button)findViewById(R.id.button_avversario_casuale);
}
}
}
答案 0 :(得分:1)
通过android:onClick
属性设置方法时,方法signIn()
必须包含参数View
,这是事件的来源。因此,声明方法如下:
public void signIn(View view) {
...
}
答案 1 :(得分:0)
可以是两件事: 1你在清单上声明了吗?
<activity android:name=".SampleActivity" android:screenOrientation="landscape"
android:label="SampleActivity"
/>
另一个原因必须是你的函数的声明,当你为布局调用它时它应该像这样(View v)作为param
public void signIn(View v) {
Intent intent = new Intent(this, Home.class);
startActivity(intent);
}