启动活动时出错

error when startActivity

祝大家新年快乐。 我在使用 onItemClickListener 方法时遇到问题,因为当我尝试在我的第二个 activity 中对位置和 id 进行敬酒时,我得到了零。 这是代码,我依赖于图像在数组中的位置,这就是我需要准确获取 position/id 的原因。 + 我在这两个活动中都复制我的数组,因为我不知道如何从第二个活动中访问它activity?

MainActivity.java

    package swe.trial;
import android.content.Context;
import android.widget.ImageView;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.AdapterView;
import android.widget.Toast;



public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        GridView items = (GridView) findViewById(R.id.itemsList);
        items.setAdapter(new item_adapter(this));
        items.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //if an item is clicked or selceted,then go to the activity itemDetails:
                Intent i= new Intent (MainActivity.this, itemDetails.class);
                i.putExtra("position", position);
                i.putExtra("id", id);
                startActivity(i);
            }
        });

    }

}


class item_adapter extends BaseAdapter {
    Integer[] picsId = {
            R.drawable.pic1,
            R.drawable.pic2,
            R.drawable.pic3,
            R.drawable.pic4,
            R.drawable.pic5,
            R.drawable.pic6,
            R.drawable.pic7};
    private Context context;


    public item_adapter(Context context) {
        this.context = context;
    }


    public Object getItem(int position) {
        return null;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imgview= new ImageView(context);
        imgview.setLayoutParams(new GridView.LayoutParams(250,250));
        imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imgview.setPadding(20,20,20,20);

        imgview.setImageResource(picsId[position]);
        return imgview;
    }


    public long getItemId(int position) {
        return position;
    }
    public int getCount (){
        return picsId.length;
    }
}

itemDetails.java

    package swe.trial;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import android.content.Intent;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.net.Uri;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by good on 1/01/17.
 */

public class itemDetails extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item);
        ArrayList<item> items = new ArrayList<item>();
//        items item1=
//        Bundle d = getIntent().getExtras();
        int id=0;getIntent().getIntExtra("id",id);
        int position=0;
        getIntent().getIntExtra("position", position);
        Toast.makeText( this, "id= " +id + " .  and posistion=  "+ position, Toast.LENGTH_LONG).show();

        Integer[] picsId = {
                R.drawable.pic1,
                R.drawable.pic2,
                R.drawable.pic3,
                R.drawable.pic4,
                R.drawable.pic5,
                R.drawable.pic6,
                R.drawable.pic7
        };

        ImageView imgview = (ImageView) findViewById(R.id.item_image);
        imgview.setImageResource(picsId[id+ 1]);
        TextView txt= (TextView) findViewById(R.id.pricetxtview);
        txt.setText("This is the description provided." + id);
        //(id);


//        item item1 = {"Red rose", "@/drawable/", 1, 1.25};
//        items.add(item1);
        // now i will search for the array that holds the given id. and i will retrieve its info and display it again
        // in the new layout.

    }
}

您必须按照以下方式取值,

int id=0;
 int position=0;
 i=getIntent().getIntExtra("id",id);
 position= getIntent().getIntExtra("position", position);
 Toast.makeText( this, "id= " +id + " .  and posistion=  "+ position, Toast.LENGTH_LONG).show();

您误解了应该使用 Intent 的方式。 正确的形式是这样的:

 int id =getIntent().getIntExtra("id",0);
 int position = getIntent().getIntExtra("position", 0);

当你使用getIntExtra时,第二个参数是默认值。如果您的意图中该标签没有这样的值,它将 return 该默认值。 有关详细信息,请参阅 Google docs