如何修复活动在运行时不显示

时间:2019-05-24 03:11:06

标签: java android xml

我正在创建一个聊天应用程序,您可以在其中创建群组。创建组我创建了一个新活动。它显示在预览中,但是当我运行应用程序并打开活动时不会显示。我在另一部手机上尝试过,只显示了CircleImageView。但是它的OnClickListner无法正常工作。编译,记录或调试时没有错误。

我什至尝试使用断点检查错误,但是没有输出。展示广告的专家。尝试更改布局,但未显示任何内容。

我不知道错误,所以我粘贴了CreateGroupActivity.java文件以及activity_create_group.xml

CreateGroupActivity.java:

package com.satyamedh.chitchatmessenger;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;

import de.hdodenhof.circleimageview.CircleImageView;

public class CreateGroupActivity extends AppCompatActivity
{


    private CircleImageView circleImageView;
    private EditText editText;
    private Button buttonconf, buttoncanc;

    private final static int chosenProfileImage = 1;


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

        initializeFields();

        buttoncanc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent profileIntent = new Intent(CreateGroupActivity.this, MainActivity.class);
                startActivity(profileIntent);
            }
        });

        buttonconf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(editText.getText())){
                    Toast.makeText(CreateGroupActivity.this, "Please enter text..", Toast.LENGTH_SHORT).show();
                }
            }
        });

        circleImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent();
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent, chosenProfileImage);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);



        if(requestCode == chosenProfileImage && resultCode == RESULT_OK && data != null)
        {
            Uri imageUri = data.getData();

            CropImage
                    .activity()
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .start(this);

        }

        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
        {
            final CropImage.ActivityResult result = CropImage.getActivityResult(data);

            if(resultCode == RESULT_OK) {
                assert result != null;
                Uri resultUri = result.getUri();

                DatabaseReference rootref = FirebaseDatabase.getInstance().getReference();

                Intent settingsIntent = new Intent(CreateGroupActivity.this, chooseUserActivity.class);
                String key = rootref.push().getKey();
                settingsIntent.putExtra("groupKey", key);
                settingsIntent.putExtra("groupImage", resultUri);
                settingsIntent.putExtra("groupName", editText.getText().toString());
                startActivity(settingsIntent);

            }

        }


    }

    private void initializeFields()
    {


        MobileAds.initialize(this, "ca-app-pub-3127817354023186~6842500243");
        final AdView mAdView;

        mAdView = findViewById(R.id.my_adView_createGroup);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                // Code to be executed when an ad request fails.
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdClicked() {



                // Code to be executed when the user clicks on an ad.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });

        circleImageView = findViewById(R.id.create_group_image);
        editText = findViewById(R.id.create_group_name);
        buttonconf = findViewById(R.id.confirm_button);
        buttoncanc = findViewById(R.id.cancel_button);


    }
}

activity_create_group.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CreateGroupActivity">

    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="51dp"
        android:layout_marginTop="87dp"
        android:layout_marginEnd="259dp"
        android:layout_marginBottom="551dp"
        android:src="@drawable/profile_image"
        android:id="@+id/create_group_image"/>

    <EditText
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="167dp"
        android:layout_marginTop="88dp"
        android:layout_marginEnd="4dp"
        android:layout_marginBottom="596dp"
        android:background="@drawable/inputs"
        android:id="@+id/create_group_name"
        android:inputType="text"
        android:hint="Group name here"/>

    <Button
        android:id="@+id/confirm_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="168dp"
        android:layout_marginTop="141dp"
        android:layout_marginEnd="156dp"
        android:layout_marginBottom="542dp"
        android:text="Confirm"
        android:background="@drawable/buttons"/>

    <Button
        android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="263dp"
        android:layout_marginTop="140dp"
        android:layout_marginEnd="60dp"
        android:layout_marginBottom="546dp"
        android:text="Cancel"
        android:background="@drawable/buttons"/>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_adView_createGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

android手机(redmi 2)屏幕截图:

https://drive.google.com/file/d/18MMzLvcdIi8iLog1nR_NrJPcJf-NZG51/view?usp=sharing

pc android studio屏幕截图:

https://drive.google.com/file/d/1E3Suy967AS3aQS9TEkUlAh5kPjWr86u-/view?usp=sharing

我是stackoverflow的新手,所以我无法附加图像。抱歉。

也没有错误。

1 个答案:

答案 0 :(得分:0)

这是正常现象,因为您使用了太多的对齐值和属性,导致布局无法响应,请使用下面的xml,并且不要忘记添加所需的image:src和背景

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CreateGroupActivity">

    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="20dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:id="@+id/create_group_image"/>

    <EditText
        android:layout_toRightOf="@id/create_group_image"
        android:layout_width="250dp"
        android:layout_marginTop="20dp"
        android:layout_height="wrap_content"
        android:id="@+id/create_group_name"
        android:inputType="text"
        android:hint="Group name here"/>

    <Button
        android:layout_below="@id/create_group_name"
        android:layout_toRightOf="@id/create_group_image"
        android:id="@+id/confirm_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Confirm"/>

    <Button
        android:layout_below="@id/create_group_name"
        android:layout_toRightOf="@id/confirm_button"
        android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="Cancel"/>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_adView_createGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>

    </RelativeLayout>