传递Drawables时生成错误

时间:2014-03-19 01:51:58

标签: java android android-intent adapter android-drawable

我正在尝试从CreateActivity中通过putInt()传递的资源ID引用drawable。我想使用与资源ID关联的drawable,并将其用作创建Event Object(带有其他参数)的一部分。

我是否错误地引用了drawable?或者我构建事件对象的方式有问题吗?

很抱歉这篇文章很长。

CreateActivity

public class CreateActivity extends Activity implements OnClickListener {

EditText etTitle;
Button btDate;
Button btTime;
Button btPic;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    //onclicklistener
    findViewById(R.id.btn_confirm).setOnClickListener(this);
    findViewById(R.id.btn_back).setOnClickListener(this);

    etTitle = (EditText) findViewById(R.id.editTextTitle);
    btDate = (Button) findViewById(R.id.btn_date);
    btTime = (Button) findViewById(R.id.btn_time);
    btPic = (Button) findViewById(R.id.btn_picture);

}

// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.btn_confirm:
        String title = etTitle.getText().toString();
        String time = btTime.getText().toString();
        String date = btDate.getText().toString();

        int resId = getResources().getInteger(R.id.btn_picture);


        Log.e("LOG", title);
        Log.e("LOG", time);
        Log.e("LOG", date);

        Bundle newBundle = new Bundle();
        newBundle.putString("TITLE", title);
        newBundle.putString("TIME", time);
        newBundle.putString("DATE", date);

        //Trying to pass a drawable from one activity to another
        newBundle.putInt("DRAWABLE", resId);

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtras(newBundle);

        setResult(RESULT_OK, intent);

        finish();

        break;

    case R.id.btn_back:
        finish();
        break;
    }

}

public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getFragmentManager(), "timePicker");
}

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
}

public void showPicturePickerDialog(View v) {
    DialogFragment newFragment = new PicturePickerFragment();
    newFragment.show(getFragmentManager(), "picturePicker");
}

}

MainActivity

public class MainActivity extends FragmentActivity implements OnClickListener {

ListView listView;
int lastIndex = -1;
ArrayList<Event> lstEvents = new ArrayList<Event>();

// detail view
TextView tvTitle, tvTime, tvDate;
ImageView ivPic;
View vw_master;

boolean _isBack = true;

ImageButton add;

String title;
String date;
String time;
int resId;

static final int PICK_CONTACT_REQUEST = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    // // get detail controls
    tvTitle = (TextView) findViewById(R.id.textViewTitle);
    tvDate = (TextView) findViewById(R.id.textViewDate);
    tvTime = (TextView) findViewById(R.id.textViewTime);
    ivPic = (ImageView) findViewById(R.id.imageView1);

    add = (ImageButton) findViewById(R.id.add);
    add.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.add:
        Intent intent = new Intent(this, CreateActivity.class);
        startActivityForResult(intent, 100);
        break;
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Create the adapter to convert the array to views
    EventAdapter adapter = new EventAdapter(this, lstEvents);

    // attach adapter to a list view
    listView = (ListView) findViewById(R.id.listViewFragment);
    listView.setAdapter(adapter);

    if (requestCode == 100) {
        if (resultCode == RESULT_OK) {
            Bundle b = data.getExtras();
            title = b.getString("TITLE");
            time = b.getString("TIME");
            date = b.getString("DATE");

            // retrieving resId, creating drawable

            resId = b.getInt("DRAWABLE");
            Drawable img = getResources().getDrawable(resId);

            /////

            Log.e("SWAGSWAG", title);
            Log.e("SWAGSWAG", time);
            Log.e("SWAGSWAG", date);

            Event newEvent = new Event();
            newEvent.set_date(date);
            newEvent.set_title(title);
            newEvent.set_time(time);

            // set drawable
            newEvent.set_drawable(img);

            lstEvents.add(newEvent);

            adapter.addAll(lstEvents);
            adapter.notifyDataSetChanged();
        }
    }

}

}

活动类

public class Event {
public int _id;
public String _title;
public String _time;
public String _date;

//drawable
public Drawable _drawable;


public Event() {
    super();
};

public Event(int id, String title, String time, String date, Drawable drawable) {
    this._id = id;
    this._title = title;
    this._time = time;
    this._date = date;
    this._drawable = drawable;
};

public Event(JSONObject object){
    try {
        this._id = object.getInt("id");
        this._title = object.getString("title");
        this._time = object.getString("time");
        this._date = object.getString("date");
   } catch (JSONException e) {
        e.printStackTrace();
   }
}

public int get_Id() {
    return _id;
}


public String get_title() {
    return _title;
}

public void set_title(String _title) {
    this._title = _title;
}

public Drawable get_drawable() {
    return _drawable;
}

public void set_drawable(Drawable _drawable) {
    this._drawable = _drawable;
}

public String get_time() {
    return _time;
}

public void set_time(String _time) {
    this._time = _time;
}

public String get_date() {
    return _date;
}

public void set_date(String _date) {
    this._date = _date;
}

}

EventAdapter

public class EventAdapter extends ArrayAdapter<Event> {

// View lookup cache
private static class ViewHolder {
    //adding drawable to imageview
    ImageView img;
    TextView title;
    TextView time;
    TextView date;
}

public EventAdapter(Context context, List<Event> objects) {
    super(context, R.layout.date_detail);
    // TODO Auto-generated constructor stub
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    Event event = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder;
    if (convertView == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.date_detail, null);
        viewHolder.title = (TextView) convertView
                .findViewById(R.id.textViewTitle);
        viewHolder.time = (TextView) convertView
                .findViewById(R.id.textViewTime);
        viewHolder.date = (TextView) convertView
                .findViewById(R.id.textViewDate);

        //adding drawable to imageview
        viewHolder.img = (ImageView) convertView
                .findViewById(R.id.imageView1);


        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    // Populate the data into the template view using the data object
    viewHolder.title.setText(event._title);
    viewHolder.time.setText(event._time);
    viewHolder.date.setText(event._date);

    //drawable
    viewHolder.img.setImageDrawable(event._drawable);
    // Return the completed view to render on screen
    return convertView;
}

}

logcat的

03-18 21:49:57.153:E / AndroidRuntime(6218):致命异常:主要 03-18 21:49:57.153:E / AndroidRuntime(6218):java.lang.RuntimeException:传递结果失败ResultInfo {who = null,request = 100,result = -1,data = Intent {cmp = com.example。 datetracker / .MainActivity(has extras)}} to activity {com.example.datetracker / com.example.datetracker.MainActivity}:android.content.res.Resources $ NotFoundException:Resource ID#0x0 03-18 21:49:57.153:E / AndroidRuntime(6218):在android.app.ActivityThread.deliverResults(ActivityThread.java:3351) 03-18 21:49:57.153:E / AndroidRuntime(6218):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3394) 03-18 21:49:57.153:E / AndroidRuntime(6218):在android.app.ActivityThread.access $ 1100(ActivityThread.java:146) 03-18 21:49:57.153:E / AndroidRuntime(6218):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1288)

1 个答案:

答案 0 :(得分:0)

更改

int resId = getResources().getInteger(R.id.btn_picture);

int resId = R.id.btn_picture;

因为R.id.btn_picture已经是视图的整数ID所以不需要使用getResources().getInteger;