gpt4 book ai didi

java - startActivity()期间的"Application was stopped"

转载 作者:行者123 更新时间:2023-12-01 22:06:08 24 4
gpt4 key购买 nike

我正在尝试通过单击按钮加载新 Activity ,但遇到一些问题。单击后出现错误:“应用程序已停止...”

这是我的应用程序:

登录页面.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>

我做错了什么?

最佳答案

Toast toast = Toast.makeText(MasterUserActivity.this, "It works :)", Toast.LENGTH_LONG);
toast.show();

把这个写在 toast 里就可以了。没有其他可见错误。一切都很好

关于java - startActivity()期间的"Application was stopped",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32739325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com