我正在尝试通过单击按钮加载新活动但我遇到了一些问题。点击后我收到一个错误:"应用程序已停止......"
这是我的申请:
LoginPage.java:
public class LoginPage extends ActionBarActivity {
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
loginButton = (Button)findViewById(R.id.loginButon);
loginButton.setOnClickListener(new LoginButtonListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.login_page, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class LoginButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginPage.this, MasterUserActivity.class);
startActivity(intent);
}
}
}
MasterUserActivity.java:
public class MasterUserActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.master_user);
Toast toast = Toast.makeText(getApplicationContext(), "It works :)", Toast.LENGTH_LONG);
toast.show();
}
}
activity_login_page.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"
tools:context="com.majewski.androidbankapplication.LoginPage" >
<TextView
android:id="@+id/BankNameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingTop="5dp"
android:gravity="center_vertical|center_horizontal"
android:text="@string/welcome_message"
android:textSize="25sp" />
<ImageView
android:id="@+id/bankIconImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/BankNameTextView"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:maxHeight="250dp"
android:maxWidth="250dp"
android:scaleType="centerInside"
android:layout_marginTop="5dp"
android:src="@drawable/bank_icon" />
<EditText
android:id="@+id/loginEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bankIconImageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:ems="10"
android:hint="@string/login_text_edit" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/loginEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:ems="10"
android:hint="@string/password_text_edit"
android:inputType="textPassword" />
<Button
android:id="@+id/loginButon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/passwordEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:text="@string/login_button" />
</RelativeLayout>
master_user.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/master_layout_padding" >
<Button
android:id="@+id/addUserButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/add_user_button_text" />
<Button
android:id="@+id/logOutButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/log_out_button_text" />
</TableRow>
<ListView
android:id="@+id/usersList"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</TableLayout>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.majewski.androidbankapplication"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".LoginPage"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.majewski.androidbankapplication.MasterUserActivity"
android:label="@string/app_name2" />
</application>
</manifest>
我做错了什么?
答案 0 :(得分:1)
Toast toast = Toast.makeText(MasterUserActivity.this, "It works :)", Toast.LENGTH_LONG);
toast.show();
把它写在烤面包上。没有其他错误可见。一切都很好
答案 1 :(得分:1)
尝试在 private void myAdorner_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
MouseButtonEventArgs revent = new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, MouseButton.Right);
revent.RoutedEvent = e.RoutedEvent;
//find you control
control.RaiseEvent(revent);
}
/ LoginPage.this.startActivity()
onClickListener()
LoginButtonListener()
答案 2 :(得分:1)
试试这个
从MasterUserActivity
而不是ActionBarActivity
扩展您的Activity
。
答案 3 :(得分:0)
将您的LoginButtonListener class
更改为:
private class LoginButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MasterUserActivity.class);
v.getContext().startActivity(intent);
}
}
顺便说一句,如果你这样做会更容易......
loginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MasterUserActivity.class);
v.getContext().startActivity(intent);
}
});