gpt4 book ai didi

java - 应用程序不幸停止了 Intent 数据解析

转载 作者:行者123 更新时间:2023-12-01 09:56:32 24 4
gpt4 key购买 nike

当我将用户名从日志记录 Activity 传递到菜单然后尝试添加提醒 Activity 时,我在 AddReminder Activity 上收到运行时错误“RemindMe 不幸停止”,但不幸的是它停止了。另外 logcat 没有向我显示任何内容(它是空的)。

日志记录.XML

<?xml version="1.0" encoding="utf-8"?>
<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.example.champ.remindme2.Login"
android:background="@drawable/back">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:src="@drawable/remind_me_logo"
android:contentDescription="Logo" />
<EditText
android:id="@+id/edtitem"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="@drawable/username_rounded_edited_text"
android:inputType="text"
android:hint="Username"
android:textAlignment="center"
android:layout_marginTop="37dp"
android:layout_below="@+id/imageView2"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/password"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="@drawable/pass_rounded_edited_text"
android:inputType="text"
android:hint="Password"
android:textAlignment="center"
android:layout_below="@+id/edtitem"
android:layout_centerHorizontal="true" />

<Button
android:id="@+id/LoginButton"
android:background="@drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
style="@style/ButtonText"
android:onClick="Login"
android:layout_below="@+id/password"
android:layout_alignLeft="@+id/password"
android:layout_alignStart="@+id/password"
android:layout_alignRight="@+id/password"
android:layout_alignEnd="@+id/password" />
<Button
android:id="@+id/txtSignup"
android:background="@drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
style="@style/ButtonText"
android:onClick="Signup"
android:layout_below="@+id/LoginButton"
android:layout_alignLeft="@+id/LoginButton"
android:layout_alignStart="@+id/LoginButton" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView3"
android:src="@drawable/fb_loging"
android:layout_alignBottom="@+id/txtSignup"
android:layout_toRightOf="@+id/txtSignup"
android:layout_toEndOf="@+id/txtSignup" />
</RelativeLayout>

日志记录.java

public class Login extends AppCompatActivity {
EditText ed1Username, ed2Pass;
String Username;
int counter=3;
Button LoginButton;
// TextView txtAttempts;
//private final int interval = 3000;
/*private Runnable runnable = new Runnable(){
public void run() {
LoginButton.setEnabled(true);
}
}; */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LoginButton= (Button)findViewById(R.id.LoginButton);
ed1Username=(EditText)findViewById(R.id.edtitem);
Username=ed1Username.getText().toString();
ed2Pass=(EditText)findViewById(R.id.password);
}


public void Login(View v){

if (ed1Username.getText().toString().equals("Admin") && ed2Pass.getText().toString().equals("123")){
Toast.makeText(Login.this, "Login Successful", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, Menu.class);
intent.putExtra("Username",Username);
startActivity(intent);
}
else {
Toast.makeText(Login.this, "Login Failed", Toast.LENGTH_LONG).show();
counter--;
}
// txtAttempts.setText("Attempts Left: " + counter);

if (counter == 0) {
LoginButton.setEnabled(false);
// new Timer().schedule((TimerTask) runnable,interval);
}
}

public void Signup(View v) {
Intent intent = new Intent(this, signup.class);
startActivity(intent);
}
}

菜单.XML

<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="@drawable/back"
tools:context="com.example.champ.remindme2.Menu">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView4"
android:src="@drawable/remind_me_logo"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Reminder"
android:id="@+id/button"
android:onClick="addReminder"
android:layout_below="@+id/imageView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="49dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View List"
android:id="@+id/button2"
android:layout_alignTop="@+id/button"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Shop"
android:id="@+id/button3"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View profile"
android:id="@+id/button4"
android:layout_alignTop="@+id/button3"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2" />
</RelativeLayout>

菜单.java

public class Menu extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}

public void addReminder(View v) {
Intent intent = new Intent(this, AddReminder.class);
String Username = intent.getExtras().getString("Username");
intent.putExtra("Username", Username);
startActivity(intent);
}
}

AddReminder.xml

<?xml version="1.0" encoding="utf-8"?>
<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.example.champ.remindme2.AddReminder"
android:background="@drawable/back">


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView6"
android:src="@drawable/remind_me_logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp" />

<EditText
android:id="@+id/edtitem"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="@drawable/rounded_edited_text"
android:inputType="text"
android:textAlignment="center"
android:layout_alignBottom="@+id/plusButton"
android:layout_alignLeft="@+id/imageView6"
android:text="Add Item"
android:layout_alignTop="@+id/plusButton" />

<Button
android:id="@+id/plusButton"
android:background="@drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
style="@style/ButtonText"
android:onClick="plus"
android:layout_below="@+id/imageView6"
android:layout_alignParentRight="true"
android:layout_alignLeft="@+id/NextButton"
android:layout_alignStart="@+id/NextButton" />

<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="47dp" />
<Button
android:id="@+id/NextButton"
android:background="@drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
style="@style/ButtonText"
android:onClick="AddEventPlace"
android:layout_alignTop="@+id/BackButton"
android:layout_alignRight="@+id/imageView6" />
<Button
android:id="@+id/BackButton"
android:background="@drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
style="@style/ButtonText"
android:onClick="Back"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/edtitem"
android:layout_alignStart="@+id/edtitem" />
</RelativeLayout>

AddReminder.java

public class AddReminder extends AppCompatActivity {
String item;
//Passing Value Through String
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reminder);
EditText edtItem= (EditText)findViewById(R.id.edtitem);
item=edtItem.getText().toString();
}

public void AddEventPlace(View v){
Intent intent = new Intent(this, AddEventPlace.class);
String Username = intent.getExtras().getString("Username");
intent.putExtra("Username", Username);
intent.putExtra("item",item);
startActivity(intent);
}
}

最佳答案

检查我更新的代码,希望它会有所帮助...

1) 更新了菜单 Activity -

public class Menu extends AppCompatActivity {
String username;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
username = getIntent().getStringExtras("Username");
}

public void addReminder(View v) {
Intent intent = new Intent(this, AddReminder.class);
intent.putExtra("Username", username);
startActivity(intent);
}
}

2) 更新了 AddReminder Activity -

public class AddReminder extends AppCompatActivity {
String item;
String username;
//Passing Value Through String
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reminder);
EditText edtItem= (EditText)findViewById(R.id.edtitem);
item=edtItem.getText().toString();
username = getIntent().getStringExtras("Username");
}

public void AddEventPlace(View v){
Intent intent = new Intent(this, AddEventPlace.class);
intent.putExtra("Username", username);
intent.putExtra("item",item);
startActivity(intent);
}
}

关于java - 应用程序不幸停止了 Intent 数据解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37164949/

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