来自带有asynctask的片段的RecyclerView - [Resources $ NotFoundException:String resource ID]

时间:2018-04-20 15:35:19

标签: java android asynchronous

我想从错误开始,这是以下

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.user.myapplication11, PID: 16351
                  android.content.res.Resources$NotFoundException: String resource ID #0x67b
                      at android.content.res.Resources.getText(Resources.java:354)
                      at android.widget.TextView.setText(TextView.java:4465)
                      at com.example.roman.myapplication11.MyFragment$MyAdapter.onBindViewHolder(MyFragment.java:156)
                      at com.example.roman.myapplication11.MyFragment$MyAdapter.onBindViewHolder(MyFragment.java:138)
                      at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
                      at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
                      at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
                      at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
                      at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
                      at android.view.View.layout(View.java:16958)
                      at android.view.ViewGroup.layout(ViewGroup.java:5587)
                      at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1189)
                      at android.view.View.layout(View.java:16958)
                      at android.view.ViewGroup.layout(ViewGroup.java:5587)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
                      at android.view.View.layout(View.java:16958)
                      at android.view.ViewGroup.layout(ViewGroup.java:5587)
                      at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1189)
                      at android.view.View.layout(View.java:16958)
                      at android.view.ViewGroup.layout(ViewGroup.java:5587)
                      at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)
                      at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361)
                      at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:894)
                      at android.view.View.layout(View.java:16958)
                      at android.view.ViewGroup.layout(ViewGroup.java:5587)
                      at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1171)
                      at android.view.View.layout(View.java:16958)
                      at android.view.ViewGroup.layout(ViewGroup.java:5587)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
                      at android.view.View.layout(View.java:16958)
                      at android.view.ViewGroup.layout(ViewGroup.java:5587)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844)
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1753)
                      at android.view.View.layout(View.java:16958)
                      at android.view.ViewGroup.layout(ViewGroup.java:5587)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
                      at android.view.View.layout(View.java:16958)
                      at android.view.ViewGroup.layout(ViewGroup.java:5587)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844)
                    at android.widget.LinearLayout.onLayout(LinearLayout.java:1753

#row 138

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>

#row 156

holder.vId.setText( mac.getId() );

MyFragment.java

public class MyFragment extends Fragment {
    private RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_m, container, false);
        recyclerView = view.findViewById(R.id.recyclerView_m);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        asyncRun();
    }

    public void asyncRun(){
        new MyAsyncTask().execute();
    }

    @SuppressLint("StaticFieldLeak")
    public class MyAsyncTask extends AsyncTask<Void, Void, String> {
        ProgressBar progressBar;
        String forecastJsonStr = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            RelativeLayout layout = Objects.requireNonNull(getActivity()).findViewById(R.id.fram_m);
            progressBar = new ProgressBar(getContext(),null, android.R.attr.progressBarStyleLarge);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            layout.addView(progressBar,params);
            getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected String doInBackground(Void... voids) {
            forecastJsonStr = Utility.executeActionOnServer(Utility.Ambiti.FOLDERS.toString(), "getData", "User=admin5");
            return forecastJsonStr;
        }

        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Objects.requireNonNull(getActivity()).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            progressBar.setVisibility(View.GONE);
            prepareList( new JsonParser().parse(s).getAsJsonObject() );
            recyclerView = getActivity().findViewById(R.id.recyclerView_m);
            recyclerView.setHasFixedSize(true);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter( new MyAdapter(getContext(), Utility.machines) );
        }
    }

    private void prepareList(JsonObject rootObject) {
        String titleSede = rootObject.get("Title").getAsString();
        double lat = rootObject.get("Lat").getAsDouble();
        double lon = rootObject.get("Lon").getAsDouble();

        JsonArray data = rootObject.getAsJsonArray("data");
        for (JsonElement pa : data) {
            ArrayList<MyClass> list = new ArrayList<>();
            JsonObject jsonObject = pa.getAsJsonObject();
            int id = jsonObject.get("id").getAsInt();
            int folderId = jsonObject.get("FolderId").getAsInt();
            int parentFolderId = jsonObject.get("ParentFolderId").getAsInt();
            String title = jsonObject.get("Title").getAsString();
            String description = jsonObject.get("Description").getAsString();
            JsonArray data2 = jsonObject.getAsJsonArray("data");

            for (JsonElement pa2 : data2) {
                JsonObject jsonObject2 = pa2.getAsJsonObject();
                int id2 = jsonObject2.get("id").getAsInt();
                String title2 = jsonObject2.get("Title").getAsString();
                String description2 = jsonObject2.get("Description").getAsString();
                list.add( new MyClass(id2, title2, description2) );
            }
            Utility.list.add( new MyClass(id, folderId, parentFolderId, titleSede, title, description, lat, lon, list));
        }


        StringBuilder string = new StringBuilder();
        for (MyClass m : Utility.list){
            string.append(m.toString()).append("\n");
        }
        Log.d("RESULT",  "\n" + string);
    }

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
        private ArrayList<MyClass> list;

        MyAdapter(Context context, ArrayList<MyClass> list){
            this.list = (ArrayList<MyClass>) list.clone();
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.m_layout, parent, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            MyClass mac = list.get(position);
            holder.vId.setText( mac.getId() );
            holder.vTitle.setText( mac.getTitle() );
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            private TextView vId;
            private TextView vTitle;

            MyViewHolder(View itemView) {
                super(itemView);
                vId = itemView.findViewById(R.id.textView_id_rowM);
                vTitle = itemView.findViewById(R.id.textView_title_rowM);
                itemView.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
               Log.d("RESULT", "clicked");
            }
        }
    }
}

HomeActivity.java

public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener  {
    private boolean doubleBackToExitPressedOnce = false;
    private ActionBarDrawerToggle toggle;
    private ProgressBar progressBar;

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

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        displaySelectedScreen(R.id.fram_m);
    }
private void displaySelectedScreen(int id){
        Fragment fragment = null;

        switch(id){
            case R.id.fram_m:
                fragment = new MyFragment();
                break;
        }
        //replacing fragment
        if(fragment != null){
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.content_frame, fragment);
            fragmentTransaction.commit();
        }
        DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
        drawerLayout.closeDrawer(GravityCompat.START);
    }
}

1 个答案:

答案 0 :(得分:0)

替换下面的

holder.vId.setText( mac.getId() );

holder.vId.setText( mac.getId() +"");

mac.getId()返回Integer值时,Android系统将检查具有该Id的任何匹配资源。