آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
مشکل عدم دسترسی خریداران پیشین به برخی آموزش ها برطرف شد
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

درخواست راهنمایی برای حل مشکل در آموزش شماره 16 بخش Ui Parser using Reflection

وحید گروسی  5 سال پیش  5 سال پیش
0 0

با سلام بنده طبق آموزش ها پیش رفتم ولی به مشکلی بر خوردم اونم این هست تصویر زیر :
مقداری پایین تر و در این قسمت کد کلاس قرار داده شده کلیک کنید.

 W: java.lang.IllegalAccessException: Class java.lang.Class<ir.vahidgarousi.app.framework.activity.UAppCompatActivity$Founder> cannot access 
field android.widget.TextView ir.vahidgarousi.app.frameworkstartup.MainActivity$Ui.txtHello of class
java.lang.Class<ir.vahidgarousi.app.frameworkstartup.MainActivity$Ui>
W:     at ir.vahidgarousi.app.framework.activity.UAppCompatActivity$Founder.build(UAppCompatActivity.java:118)


 UAppcompatActivity
package ir.vahidgarousi.app.framework.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;

import ir.vahidgarousi.app.framework.core.UBase;


public class UAppCompatActivity extends AppCompatActivity {

@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
}

@Override
protected void onResume() {
super.onResume();
UBase.setCurrentActivity(this);
}

public static class Founder {

private static final String TAG = "Founder";
private final Activity activity;
private int[] feauters;
private boolean noTitleBar;
private boolean noActionBar;
private boolean fullScreen;
private int layoutResId;
private Object ui;

public Founder(Activity activity) {
this.activity = activity;
}

public Founder requestFeaturs(int... feauters) {
this.feauters = feauters;

return this;
}

public Founder noTitleBar() {
noTitleBar = true;
return this;
}

public Founder noActionBar() {
noActionBar = true;
return this;
}

public Founder fullScreen() {
this.fullScreen = true;
return this;
}

public Founder contentView(@LayoutRes int latoutResId) {
this.layoutResId = latoutResId;
return this;
}

public Founder extractUi(Object ui) {
this.ui = ui;
return this;
}

public Founder build() {
for (int feature : this.feauters) {
activity.getWindow().requestFeature(feature);
}
if (noTitleBar) {
activity.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}

if (fullScreen) {
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
if (noActionBar) {
activity.getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
{
ActionBar actionBar = activity.getActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
if (activity instanceof AppCompatActivity) {
AppCompatActivity castedActivity = (AppCompatActivity) activity;
android.support.v7.app.ActionBar actionBar = castedActivity.getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
}
activity.setContentView(layoutResId);
{
Class clazz = ui.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
String name = field.getName();
Class type = field.getType();
if (name.contains("$")) {
continue;
}
Log.i(TAG, "name is : " + name + " type : " + type);
int id = UBase.get().getResources().getIdentifier(name, "id", UBase.get().getPackageName());
try {
field.set(ui, activity.findViewById(id));
} catch (IllegalAccessException e) {
Log.i(TAG, "error");
e.printStackTrace();
}
}
}
return this;
}
}
}
 
0 0
این کد رو داخل حلقه for ببین درست میشه : ' if(field.isSynthetic()) { continue; }' (5 سال پیش)
0 0
نظر نه بازم خطا داد و این بار به شکل زیر که در پاسخ زیر درج می کنم (5 سال پیش)
0 0
سلام لینک (5 سال پیش)
 برای این سوال 1 پاسخ وجود دارد. مشاهده پاسخ صحیح
پاسخ به سوال 
وحید گروسی  5 سال پیش
0 0

 پاسخ صحیح

#c110562 عزیز؛
با افزودن قطعه کد زیر به حلقه For مشکل حل شد :

 
for (Field field : fields) {
String name = field.getName();
Class type = field.getType();
if (name.contains("$")) {
continue;
}
// This section fixed previus crash
// Start
if (type == long.class) {
continue;
}
// This section fixed previus crash
// End
Log.i(TAG, "name is : " + name + " type : " + type);
int id = UBase.get().getResources().getIdentifier(name, "id", UBase.get().getPackageName());
try {
field.set(ui, activity.findViewById(id));
} catch (IllegalAccessException e) {
Log.i(TAG, "error");
e.printStackTrace();
}
}

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .