简体   繁体   中英

R.java won't generate

I am currently working on a project in which I was updating a ListView's adapter from ArrayAdapter to a Custom CursorAdapter.

I deleted the R.java intentionally to regenerate the R.java file after inserting a new <string></string> to the res/string.xml file since it was not automatically regenerating. The file did not generate.

I continued to debug, and ran a Project->Clean... in attempt to regenerate the R.java file. This did not work either.

After checking StackOverflow, I also tried changing the version in AndroidManifest.xml in hope to regenerate the file. This also did not work. I've tried a few other hacks in hopes something might have Eclipse regenerate the file. No Luck.

Does anyone have any idea? I am on a tight deadline and this is really holding me back.

Here is the file I was working in when the issue arose:

package com.renaissanceartsmedia.gradingapp.controllers.fragments;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.util.ArrayMap;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.renaissanceartsmedia.gradingapp.R;
import com.renaissanceartsmedia.gradingapp.controllers.activities.CourseActivity;
import com.renaissanceartsmedia.gradingapp.model.Course;
import com.renaissanceartsmedia.gradingapp.model.CourseStore;
import com.renaissanceartsmedia.gradingapp.model.GradingAppDatabaseHelper.CourseCursor;

public class CourseListFragment extends ListFragment {

    // DEBUG
    private static final String TAG = "CourseListFragment";

    // Create an ArrayList<String> to store flashcards
    ArrayList<Course> mCourses;
    ArrayMap<Long, Course> mCoursesById;

    // Cursor Object
    private CourseCursor mCursor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the Title of the Fragment's Activity
        getActivity().setTitle(R.string.course_list);

        // Load Cursor to DB
        mCursor = CourseStore.get(getActivity()).queryCourses();

        // Set the list of FlashcardListFragments
        mCourses = CourseStore.get(getActivity()).getCourses();
        mCoursesById  = CourseStore.get(getActivity()).getCoursesById();

        // Create an ArrayAdapter to use for displaying FlashcardListFragments in FlashcardListActivity
        /*
        ArrayAdapter<Flashcard> adapter = new ArrayAdapter<Flashcard>(
                                                                    getActivity(),
                                                                    android.R.layout.simple_list_item_1,
                                                                    mFlashcards);
        */

        // OLD METHOD
        //ListItemLayoutAdapter adapter = new ListItemLayoutAdapter(mCourses);
        CourseCursorAdapter adapter = new CourseCursorAdapter(getActivity(), mCursor);

        // Set the adapter for the list
        setListAdapter(adapter);
    }


    /* Handles a user selection of a FlashcardListFragment
     * 
     */
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        //Flashcard f = (Flashcard)(getListAdapter()).getItem(position);
        // OLD WAY
        //Course c = ((ListItemLayoutAdapter)getListAdapter()).getItem(position);

        /*
        Course c = ((CourseCursorAdapter))getListAdapter());
        Log.d(TAG, c.getCourseTitle() + " was clicked");


        // Start a new activity using an Intent
        Intent openFlashcardDetail = new Intent(getActivity(), CourseActivity.class);

        // Add EXTRAS to the intent
        //openFlashcardDetail.putExtra(FlashcardFragment.EXTRA_FLASHCARD_ID, f.getId());
        openFlashcardDetail.putExtra(Course.EXTRA_COURSE_ID, c.getId());

        startActivity(openFlashcardDetail);
        */

    }

    // OLD METHOD
    //class ListItemLayoutAdapter extends ArrayAdapter<Course> {
    class CourseCursorAdapter extends CursorAdapter {

        // Member Properties
        // OLD METHOD
        Course mCurrentListObject;
        CourseCursor mCourseCursor;

        // Constructor
        // OLD METHOD
        /*
        public ListItemLayoutAdapter(ArrayList<Course> itemContent) {
            super(getActivity(), 0, itemContent);

        }
        */

        public CourseCursorAdapter(Context context, CourseCursor cursor) {
            super(context, cursor, 0);
            mCourseCursor = cursor;
        }
        // OLD WAY
        /*
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //If we weren't given a view, inflate a new view
            if (convertView == null) {
                convertView = getActivity().getLayoutInflater()
                        .inflate(R.layout.list_item_layout, null);
            }

            // Configure the view for this object
            mCurrentListObject = getItem(position);

            // Make Connections from Layout to Java code
            TextView mainTextView = (TextView) convertView.findViewById(R.id.item_main_text);
            TextView subTextView = (TextView) convertView.findViewById(R.id.item_sub_text);

            // Set the Contents of the Views
            mainTextView.setText(setMainText());
            subTextView.setText(setSubText());

            return convertView;
        }
        */

        @Override 
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            // Use a layout Inflater to get a row view
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            // Get the course for the current row
            Course course = mCourseCursor.getCourse();

            // Setup a new TextView
            TextView courseTitleTextView = (TextView)view;
            String mainText = context.getString(R.string.course_title_hint, course.getCourseTitle());
            courseTitleTextView.setText(mainText);


        }



        public String setMainText() {
            return mCurrentListObject.getCourseTitle();
        }
        public String setSubText() {
            return mCurrentListObject.getSubject();
        }

    }
}

I have solution, I hope this will help you. !!

Step 1) Change Build Target

For Example, if you selected 2.2 as Build Target then select Maximum you have. (like 4.4)

And if you selected 4.4 then select 4.3 as target.

建立目标

Step 2) Clean Project

清洁项目

It will create R.java again

Thank you.

This usually happens because there is an issue somewhere in your XML or resource file names. Try restarting Eclipse, if it still does not indicate where the problem is, try retracing your steps the last changes you made to XML files before this problem occurred. You will find some kind of syntax error or spelling mistake or invalid file name somewhere. Fix it then clean again.

如果您使用的是Eclipse,请打开问题视图(“窗口”->“显示视图”->“问题”),然后在资源或清单中查找错误

Try restarting eclipse, sometimes it will do the trick. Check if you are using jdk 7, it only needs jdk 6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM