#android #toolbar #layout-inflater
Вопрос:
Я хочу, чтобы имя пользователя появилось на панели инструментов в правом углу.
После того, как я отправлю диалоговое окно входа в систему, попробуйте изменить имя пользователя, но выдал ошибку.
2021-10-11 12:16:47.528 22775-22775/hu.fherp.application W/System.err: android.view.InflateException: Binary XML file line #32 in hu.fherp.application:layout/activity_main: Binary XML file line #32 in hu.fherp.application:layout/activity_main: Error inflating class androidx.appcompat.widget.Toolbar
2021-10-11 12:16:47.528 22775-22775/hu.fherp.application W/System.err: Caused by: android.view.InflateException: Binary XML file line #32 in hu.fherp.application:layout/activity_main: Error inflating class androidx.appcompat.widget.Toolbar
2021-10-11 12:16:47.530 22775-22775/hu.fherp.application W/System.err: Caused by: java.lang.reflect.InvocationTargetException
2021-10-11 12:16:47.530 22775-22775/hu.fherp.application W/System.err: at java.lang.reflect.Constructor.newInstance0(Native Method)
2021-10-11 12:16:47.531 22775-22775/hu.fherp.application W/System.err: at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
2021-10-11 12:16:47.531 22775-22775/hu.fherp.application W/System.err: at android.view.LayoutInflater.createView(LayoutInflater.java:852)
2021-10-11 12:16:47.531 22775-22775/hu.fherp.application W/System.err: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)
2021-10-11 12:16:47.531 22775-22775/hu.fherp.application W/System.err: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
2021-10-11 12:16:47.532 22775-22775/hu.fherp.application W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
2021-10-11 12:16:47.532 22775-22775/hu.fherp.application W/System.err: at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
2021-10-11 12:16:47.532 22775-22775/hu.fherp.application W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
2021-10-11 12:16:47.532 22775-22775/hu.fherp.application W/System.err: at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
2021-10-11 12:16:47.533 22775-22775/hu.fherp.application W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
2021-10-11 12:16:47.533 22775-22775/hu.fherp.application W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
2021-10-11 12:16:47.533 22775-22775/hu.fherp.application W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
2021-10-11 12:16:47.533 22775-22775/hu.fherp.application W/System.err: at hu.fherp.application.WebserviceHelper.loginSuccesfull(WebserviceHelper.kt:293)
2021-10-11 12:16:47.533 22775-22775/hu.fherp.application W/System.err: at hu.fherp.application.WebserviceHelper.getJSONObjectWebservice(WebserviceHelper.kt:169)
2021-10-11 12:16:47.534 22775-22775/hu.fherp.application W/System.err: at hu.fherp.application.MainActivity.putCustomerOpenBaskets(MainActivity.kt:147)
2021-10-11 12:16:47.534 22775-22775/hu.fherp.application W/System.err: at hu.fherp.application.MainActivity.dashboardBindings(MainActivity.kt:78)
2021-10-11 12:16:47.534 22775-22775/hu.fherp.application W/System.err: at hu.fherp.application.MainActivity.applyTexts(MainActivity.kt:206)
2021-10-11 12:16:47.535 22775-22775/hu.fherp.application W/System.err: at hu.fherp.application.DialogLogin.onCreateDialog$lambda-1(DialogLogin.kt:49)
2021-10-11 12:16:47.535 22775-22775/hu.fherp.application W/System.err: at hu.fherp.application.DialogLogin.lambda$4zIzu4sTTPHlZVOdwhB12WdRF9Q(Unknown Source:0)
2021-10-11 12:16:47.535 22775-22775/hu.fherp.application W/System.err: at hu.fherp.application.-$Lambda$DialogLogin$4zIzu4sTTPHlZVOdwhB12WdRF9Q.onClick(Unknown Source:2)
2021-10-11 12:16:47.535 22775-22775/hu.fherp.application W/System.err: at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
2021-10-11 12:16:47.536 22775-22775/hu.fherp.application W/System.err: at android.os.Handler.dispatchMessage(Handler.java:106)
2021-10-11 12:16:47.536 22775-22775/hu.fherp.application W/System.err: at android.os.Looper.loop(Looper.java:246)
2021-10-11 12:16:47.536 22775-22775/hu.fherp.application W/System.err: at android.app.ActivityThread.main(ActivityThread.java:8512)
2021-10-11 12:16:47.536 22775-22775/hu.fherp.application W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2021-10-11 12:16:47.537 22775-22775/hu.fherp.application W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
2021-10-11 12:16:47.537 22775-22775/hu.fherp.application W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139)
2021-10-11 12:16:47.538 22775-22775/hu.fherp.application W/System.err: Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f0300dd a=-1}
2021-10-11 12:16:47.539 22775-22775/hu.fherp.application W/System.err: at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:997)
2021-10-11 12:16:47.539 22775-22775/hu.fherp.application W/System.err: at android.content.res.TypedArray.getDrawable(TypedArray.java:981)
2021-10-11 12:16:47.540 22775-22775/hu.fherp.application W/System.err: at android.view.View.<init>(View.java:5954)
2021-10-11 12:16:47.540 22775-22775/hu.fherp.application W/System.err: at android.view.ViewGroup.<init>(ViewGroup.java:715)
2021-10-11 12:16:47.540 22775-22775/hu.fherp.application W/System.err: at android.view.ViewGroup.<init>(ViewGroup.java:711)
2021-10-11 12:16:47.540 22775-22775/hu.fherp.application W/System.err: at androidx.appcompat.widget.Toolbar.<init>(Toolbar.java:237)
2021-10-11 12:16:47.541 22775-22775/hu.fherp.application W/System.err: at androidx.appcompat.widget.Toolbar.<init>(Toolbar.java:233)
2021-10-11 12:16:47.541 22775-22775/hu.fherp.application W/System.err: ... 27 more
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@ id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/light_grey_bg"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:fitsSystemWindows="true"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.appcompat.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
tools:layout_editor_absoluteY="16dp"
tools:ignore="MissingConstraints">
<RelativeLayout
android:orientation="horizontal"
android:id="@ id/toolbar_appname_container"
android:gravity="start"
android:layout_marginStart="60dp"
android:layout_marginRight="60dp"
android:layout_width="10pt"
android:layout_height="match_parent">
<TextView
android:id="@ id/textAppName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginRight="20dp"
android:textSize="22dp"
android:text=""
android:textColor="@android:color/white" />
</RelativeLayout>
<RelativeLayout
android:orientation="horizontal"
android:id="@ id/toolbar_item_container"
android:gravity="end"
android:layout_marginStart="20dp"
android:layout_marginRight="20dp"
android:layout_width="120dp"
android:layout_height="match_parent">
<TextView
android:id="@ id/textUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginRight="20dp"
android:text="Username"
android:textColor="@android:color/white" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
<FrameLayout
android:id="@ id/frameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
.....
WebserviceHelper.kt
class WebserviceHelper {
private lateinit var activityContext: Context
....
fun getJSONArrayWebservice(context: Context, wsName: String, activityContext: AppCompatActivity, supportFragmentManager: FragmentManager): JSONArray {
this.activityContext = context
...
}
private fun loginSuccesfull () {
val mContext: Context = this.activityContext.applicationContext
val inflater = LayoutInflater.from(mContext)
val layout: View = inflater.inflate(R.layout.activity_main, null)
val textView = layout.findViewById<View>(R.id.textUserName) as TextView
textView.text = this.username
//val activity: AppCompatActivity = (AppCompatActivity) this.activityContext
//val textUserName: TextView = activity.findViewById(R.id.textUserName)
//textUserName.text = this.username
}
Я прочитал много тем из этого, но это не помогает. Например, «измените стиль». Я изменился, но исключение не исчезло.
stlye.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="CheckBoxGreyGreen" parent="@style/Theme.AppCompat.Light">
<item name="colorControlNormal">@color/dark_grey</item>
<item name="colorControlActivated">@color/box_green</item>
</style>
<style name="RadioButtonGreyGreen" parent="@style/Theme.AppCompat.Light">
<item name="colorControlNormal">@color/dark_grey</item>
<item name="colorControlActivated">@color/box_green</item>
</style>
</resources>
Спасибо за любые идеи.
Решение:
val textUserName: TextView = this.appCompatActivity.findViewById(R.id.textUserName)
textUserName.text = this.username
Комментарии:
1. Не используйте
applicationContext
для раздувания макетов; на нем не задана ваша тема. ИспользуйтеActivity
вместо этого. (Хотя я не уверен, что это сделает то, что вы думаете.activity_main
Экземпляр, который вы там создаете, не имеет никакого отношения к какому-либоMainActivity
экземпляру, который, возможно, уже запущен.)2. Я пробовал использовать Aitivity, прежде чем отправлять эту тему. Посмотрите мой код, часть // удалена. Но не сработало, потому что «неизвестная функция findViewById», но попробуйте еще раз и работайте сейчас. Я вам очень благодарен. Спасибо. Пожалуйста, напишите как обычный ответ, и я поставлю галочку. val имя_текста: TextView = this.AppCompatActivity.findViewById(R. id.имя_текста) имя_текста.текст = this.имя пользователя
3. О, это круто. 🙂 Ничего серьезного. Хотя, спасибо. Я ценю ваше предложение. Рад, что ты все понял. Ура!