因此,我制作了一个程序,该程序自动创建CardViewes来在其中存储生日的日期和名称,并通过以下指南将它们添加到我的主活动的相对布局中:https://android--code.blogspot.com/2015/12/android-how-to-create-cardview.html。我有很多错误,并且修复了所有错误,直到现在我没有错误消息可以使用。如标题所示,CardViews不会出现在我的主活动中。如果有人可以帮助我,我将非常高兴。顺便说一句,我是编码的新手,没有太多经验,所以这可能也是问题所在。
主要活动Java:
package com.myuserwithoutme.birthdayremember;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.myuserwithoutme.birthdayremember.CreateActivity.*;
public class MainActivity extends AppCompatActivity{
private Context mContext;
RelativeLayout mRelativeLayout;
Boolean pressed = false;
String mEditText1;
String mEditText2;
/
private static final String TOAST_TEXT = "Test ads are being shown. "
+ "To show live ads, replace the ad unit ID in res/values/strings.xml with your own ad unit ID.";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
mRelativeLayout = findViewById(R.id.RelativeLayout);
AdView adView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show();
FloatingActionButton fab1 = findViewById(R.id.fab1);
fab1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent createIntent = new Intent(MainActivity.this,CreateActivity.class);
startActivity(createIntent);
}
});
Intent newcard = getIntent();
for (int x = 1; x <= 0; x++) {
if (newcard != null) {
pressed = newcard.getExtras().getBoolean("card");
}
}
Intent strings = getIntent();
for (int x = 1; x <= 0; x++) {
if (strings != null) {
mEditText1 = strings.getExtras().getString("Name");
mEditText2 = strings.getExtras().getString("Date");
}
}
if (pressed != false) {
CardView card = new CardView(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
card.setLayoutParams(params);
card.setRadius(9);
card.setContentPadding(15, 15, 15, 15);
card.setCardBackgroundColor(Color.parseColor("#d3e8e7"));
card.setMaxCardElevation(15);
card.setCardElevation(9);
TextView text = new TextView(mContext);
text.setLayoutParams(params);
text.setText(mEditText1 + "\n" + mEditText2);
text.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
text.setTextColor(Color.parseColor("#308985"));
card.addView(text);
mRelativeLayout.addView(card);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, 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);
}
}
Xml主要活动:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- view for AdMob Banner Ad -->
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Testing" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab1"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="62dp"
android:layout_marginEnd="15dp"
android:clickable="true"
android:src="@drawable/ic_baseline_add_24px"
ads:backgroundTint="@android:color/darker_gray"
ads:rippleColor="@android:color/background_light" />
感谢您的帮助
编辑:
第二活动:
package com.myuserwithoutme.birthdayremember;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class CreateActivity extends AppCompatActivity {
private EditText mEditText1;
private EditText mEditText2;
private String name;
private String date;
public boolean newcard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
mEditText1 = findViewById(R.id.editText1);
mEditText2 = findViewById(R.id.editText2 );
name = mEditText1.getText().toString();
date = mEditText2.getText().toString();
final Intent newcard = new Intent(CreateActivity.this, MainActivity.class);
FloatingActionButton fab2 = findViewById(R.id.fab2);
fab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent mainIntent = new Intent(CreateActivity.this,MainActivity.class);
startActivity(mainIntent);
newcard.putExtra("card", true);
Intent strings = new Intent(CreateActivity.this, MainActivity.class);
strings.putExtra("Name", name);
strings.putExtra("Date", date);
startActivity(strings);
}
});
}
}
第二个Xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- view for AdMob Banner Ad -->
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="61dp"
android:layout_marginEnd="17dp"
android:clickable="true"
android:src="@drawable/ic_baseline_save_24px"
ads:backgroundTint="@android:color/darker_gray"
ads:rippleColor="@android:color/background_light" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="126dp"
android:ems="10"
android:hint="DD/MM/YYYY"
android:inputType="date" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="18dp"
android:layout_marginTop="24dp"
android:text="Gib die Daten der Person ein" />
答案 0 :(得分:0)
您必须以startActivityForResult
而不是startActivity
来开始活动,并且该活动会将结果值返回给MainActivity而不是其他:
https://developer.android.com/training/basics/intents/result
这些循环在做什么:
Intent newcard = getIntent();
for (int x = 1; x <= 0; x++) {
if (newcard != null) {
pressed = newcard.getExtras().getBoolean("card");
}
}
Intent strings = getIntent();
for (int x = 1; x <= 0; x++) {
if (strings != null) {
mEditText1 = strings.getExtras().getString("Name");
mEditText2 = strings.getExtras().getString("Date");
}
}
由于起始值为x = 1
,因此不会执行,因此永远不会为x <= 0
。
也许尝试不使用循环:
Intent newcard = getIntent();
if (newcard != null) {
pressed = newcard.getExtras().getBoolean("card");
}
Intent strings = getIntent();
if (strings != null) {
mEditText1 = strings.getExtras().getString("Name");
mEditText2 = strings.getExtras().getString("Date");
}
答案 1 :(得分:0)
因此,我将尽力提供帮助。正如评论中提到的那样,可能您的代码未到达创建卡的那部分,这意味着按下的代码永远都不正确。
由于我没有您其他活动的代码,我想您可以通过删除似乎不循环的循环来摆脱问题
第一个活动
Intent intent = getIntent();
if (intent != null) {
pressed = intent.getExtras().getBoolean("card");
mEditText1 = intent.getExtras().getString("Name");
mEditText2 = intent.getExtras().getString("Date");
}
getIntent将从您来自的活动中返回意图。您无需像创建两个意图那样创建它们,因为它们是相同的。
编辑
第二活动
您的代码永远不会达到“投入量”,因为您之前已经导航到另一个活动。你应该那样做。
FloatingActionButton fab2 = findViewById(R.id.fab2);
fab2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CreateActivity.this, MainActivity.class);
intent.putExtra("card", true);
intent.putExtra("Name", name);
intent.putExtra("Date", date);
startActivity(intent);
}
});
还有一些代码提示。
代替
if (pressed != false)
// This means pressed is true.
使用
if (pressed)
// This also means pressed is true.
答案 2 :(得分:0)
对于具有相同问题的人来说,只是快速编辑,startActivityForResult起初没有起作用,因为我有一个过时的版本,我将jdk和android studio更新到了最新版本,并且它起作用了。谢谢大家:D