GetStringExtra始终返回null

时间:2018-09-25 07:12:42

标签: java android android-intent

我试图将字符串从onclick recyclerview发送到活动中,除了其中之一以外,一切都很好。

<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"
    android:baselineAligned="false"
    tools:context=".view.activity.PhotoCaptureActivity">

    <RelativeLayout
        android:id="@+id/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="0dp"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"
            android:layout_marginTop="0dp"
            android:orientation="horizontal"
            android:weightSum="3">

            <FrameLayout
                android:id="@+id/cameraFrame_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:id="@+id/cameraFrame"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal" />

                <ImageView
                    android:id="@+id/captured_image"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:scaleType="fitXY"
                    android:scaleX="-1"
                    />

                <ImageView
                    android:id="@+id/captured_image_back"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:scaleType="fitXY"
                    android:scaleX="-1"
                    android:scaleY="-1"
                    android:visibility="gone"/>

                <ImageButton
                    android:id="@+id/button_capture"
                    android:layout_width="64dp"
                    android:layout_height="64dp"
                    android:layout_gravity="center_horizontal|bottom"
                    android:layout_marginBottom="5dp"
                    android:background="@drawable/ic_camera_capture_image" />

                <ImageButton
                    android:id="@+id/swicth_camera"
                    android:layout_width="36dp"
                    android:layout_height="36dp"
                    android:layout_gravity="right|bottom"
                    android:layout_marginBottom="10dp"
                    android:layout_marginEnd="20dp"
                    android:background="@drawable/ic_camera_switch" />

                <ImageButton
                    android:id="@+id/flash_on"
                    android:layout_width="36dp"
                    android:layout_height="36dp"
                    android:layout_gravity="left|bottom"
                    android:layout_marginBottom="10dp"
                    android:layout_marginStart="20dp"
                    android:background="@drawable/ic_gallery"
                    android:visibility="gone"/>

                <ImageButton
                    android:id="@+id/fab_send_photo"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:layout_gravity="right|bottom"
                    android:layout_marginBottom="10dp"
                    android:layout_marginEnd="20dp"
                    app:srcCompat="@drawable/ic_send_blue"
                    android:visibility="gone" />

            </FrameLayout>

        </LinearLayout>

    </RelativeLayout>
</RelativeLayout>

我还尝试将getnama记录在未使用的标签中,使其运行良好,然后将数据返回给我。但是,当我在另一个活动中检索它时,它总是返回null。

GeneralItem generalItem   = (GeneralItem) consolidatedList.get(position);
                    Intent intent = new Intent(getActivity(), DetailPengumuman.class);

                    intent.putExtra("getnama", generalItem.getDaftarPengumuman().getNama_p().toString());
                    Log.e("untaging","ada isinya : "+generalItem.getDaftarPengumuman().getNama_p().toString());

                    intent.putExtra("tanggalpengumuman", generalItem.getDaftarPengumuman().getTanggal_peng());
                    intent.putExtra("judulpengumuman", generalItem.getDaftarPengumuman().getJudul());
                    intent.putExtra("deskripsipengumuman", generalItem.getDaftarPengumuman().getDeskripsi());
                    startActivity(intent);

3 个答案:

答案 0 :(得分:1)

您可以先检查意图是否包含数据。

    Bundle bundle = getIntent().getExtras();

    if (bundle != null) {
        if (bundle.containsKey("Key")) {
            String value = bundle.getString("Key");
        }
    }

答案 1 :(得分:0)

解决了,谢谢, 这是因为我缺少String属性

 Intent intent = getIntent();
        tanggalPengumumanGet = intent.getStringExtra("tanggalpengumuman");
        judulPengumumanGet = intent.getStringExtra("judulpengumuman");
        namaPengumumanGet = intent.getStringExtra("getnama");
        deskripsiPengumumanGet = intent.getStringExtra("deskripsipengumuman");

        Log.e("untaging","nama : " +namaMatkulGet);

我在namaPengumumanGet中得到了额外的收益,并记录了另一个字符串,即namaMatkkulGet

答案 2 :(得分:0)

尝试添加 .toString()来调用getStringExtra:

 intent.putExtra("tanggalpengumuman", generalItem.getDaftarPengumuman().getTanggal_peng().toString());
 intent.putExtra("judulpengumuman", generalItem.getDaftarPengumuman().getJudul().toString());
 intent.putExtra("deskripsipengumuman", generalItem.getDaftarPengumuman().getDeskripsi().toString);