powered by simpleCommunicator - 2.0.49     © 2025 Programmizd 02
Форумы / Android [игнор отключен] [закрыт для гостей] / Почему Extras в intent не содержит значений?
2 сообщений из 2, страница 1 из 1
Почему Extras в intent не содержит значений?
    #39603407
Abejon
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Есть всего две activity в приложении. LoginActivity:
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
package com.myapp.myappardandroid;
 
import android.arch.persistence.room.Room;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
 
import java.util.ArrayList;
import java.util.List;
 
import static com.mayapp.myappandroid.Constants.SETTINGS;
 
public class LoginActivity extends AppCompatActivity {
 
    private SettingsDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
 
        Intent intent = getIntent();
 
        ImageButton settingsBtn = findViewById(R.id.settingsBtn);
        settingsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateSettings(view);
            }
        });
 
        db = Room.databaseBuilder(getApplicationContext(), SettingsDatabase.class, "settingsDb")
                .allowMainThreadQueries()
                .build();
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
 
        if (resultCode == RESULT_OK) {
            db.getSettingsDao().insert(new Settings("apiEndPoint", data.getStringExtra("apiEndPoint")));
            db.getSettingsDao().insert(new Settings("apiKey", data.getStringExtra("apiKey")));
        }
    }
 
    private void updateSettings(View view) {
        Intent intent = new Intent(LoginActivity.this, SettingsActivity.class);
        List<Settings> settingsList = db.getSettingsDao().getAllSettings();
        for(int i = 0; i < settingsList.size(); i++){
            Settings settings = settingsList.get(i);
            intent.putExtra(settings.getParamName(), settings.getParamValue() == null ? "" : settings.getParamValue());
        }
        startActivityForResult(intent, SETTINGS);
    }
}


и её разметка:
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
<LinearLayout 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:gravity="center_horizontal"
    android:orientation="vertical"
    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.myapp.myappandroid.LoginActivity">
 
    <!-- Login progress -->
    <ProgressBar
        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />
 
    <ScrollView
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
        <LinearLayout
            android:id="@+id/email_login_form"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
 
            <android.support.v7.widget.AppCompatImageButton
                android:id="@+id/settingsBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:src="@drawable/settings_icon"
                android:background="@android:color/transparent" />
 
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
 
                <AutoCompleteTextView
                    android:id="@+id/email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_email"
                    android:maxLines="1"
                    android:singleLine="true" />
 
            </android.support.design.widget.TextInputLayout>
 
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
 
                <EditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_password"
                    android:imeActionId="6"
                    android:imeActionLabel="@string/action_sign_in_short"
                    android:imeOptions="actionUnspecified"
                    android:inputType="textPassword"
                    android:maxLines="1"
                    android:singleLine="true" />
 
            </android.support.design.widget.TextInputLayout>
 
            <Button
                android:id="@+id/email_sign_in_button"
                style="?android:textAppearanceSmall"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="@string/action_sign_in"
                android:textStyle="bold" />
 
        </LinearLayout>
    </ScrollView>
</LinearLayout>


И SettingsActivity:
Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
package com.myapp.myappandroid;
 
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class SettingsActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
 
        Intent intent = this.getIntent();
        EditText apiEndPoint = findViewById(R.id.settingsApiEndPoint);
        apiEndPoint.setText(intent.getStringExtra("apiEndPoint"));
        EditText apiKey = findViewById(R.id.settingsApiKey);
        apiKey.setText(intent.getStringExtra("apiKey"));
 
        Button ssBtn = findViewById(R.id.saveSettingsBtn);
        ssBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveSettings(view);
            }
        });
    }
 
    public void saveSettings(View view) {
        Intent answerIntent = new Intent(SettingsActivity.this, LoginActivity.class);
        EditText apiEndPoint = findViewById(R.id.settingsApiEndPoint);
        answerIntent.putExtra("apiEndPoint", apiEndPoint.getText());
        EditText apiKey = findViewById(R.id.settingsApiKey);
        answerIntent.putExtra("apiKey", apiKey.getText());
 
        setResult(RESULT_OK, answerIntent);
        finish();
    }
}


и её разметка:
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    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.myapp.myappandroid.SettingsActivity">
 
    <EditText
        android:id="@+id/settingsApiEndPoint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/settings_end_point_hint"/>
 
    <EditText
        android:id="@+id/settingsApiKey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/settings_api_key"/>
 
    <android.support.v7.widget.AppCompatButton
        android:id="@+id/saveSettingsBtn"
        style="?android:textAppearanceSmall"
        android:layout_marginTop="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/setings_save_button_text"
        android:textStyle="bold" />
 
</android.support.v7.widget.LinearLayoutCompat>


У приложения вот такой незатейливый AndroidManifest:
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.myappandroid">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:label="List of Mobile OS"
            android:name=".LoginActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <activity android:name=".SettingsActivity" />
    </application>
 
</manifest>



Собственно, проблема: пытаюсь передавать параметры из одной activity в другую и обратно. Перед отправкой проверяю дебаггером - в обоих случаях intent в Extras содержат введённые значения.
На приёмном конце проверяю - Extras в обоих случаях пустые. Т.е не содержит пустые значения с указанными именами, а пустой, от слова "вообще".

В чём может быть дело? Что делаю не так? Где смотреть?

P.S. Не знаю, насколько это важно, но LoginActivity изначально была google.LoginActivity, я убрал всё, кроме полей ввода данных.
...
Рейтинг: 0 / 0
Почему Extras в intent не содержит значений?
    #39603994
Abejon
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Abejon,
Оказалось, что getText возвращает не строковое значение, а Editable. Правильно dj второй activity писать вот так:

answerIntent.putExtra("apiEndPoint", apiEndPoint.getText(). toString() );

Добавление выделено красным цветом. Т.е. я помещал в Extras не примитивный тип, как это требуется.
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / Android [игнор отключен] [закрыт для гостей] / Почему Extras в intent не содержит значений?
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]