context.contentWrapper.getpackageName空指针异常

时间:2016-01-26 06:47:07

标签: java android

在解决一个错误的过程中,我厌倦了很多个小时。当我启动意图程序崩溃并给出

context.contentWrapper.getpackageName空指针异常

这是我的主要活动

public class SelectCategory extends Activity {
    private static Context context1;
    private LayoutInflater inflater;
    private View v3;
    public static ArrayList<Bitmap> imageArray = new ArrayList<Bitmap>();
    private static ArrayList<String> priceArray = new ArrayList<String>();
    private MoverAdapter mover;







    private GridView gridView;


    public SelectCategory(Context context, ViewFlipper viewFlipper) {
        context1=context;

        inflater=(LayoutInflater)context1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v3=(inflater.inflate(R.layout.category,null));
        viewFlipper.addView(v3);

        gridView=(GridView)v3.findViewById(R.id.gridView);

        gridView.setAdapter(new MoverAdapter(context1));

        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long id) {
                // TODO Auto-generated method stub

                Intent i = new Intent(SelectCategory.this, description_class.class);
                i.putExtra("id", position);
                startActivity(i);

            //  Toast.makeText(context1, "YEs", Toast.LENGTH_SHORT).show();

            }
        });

    }

这是适配器

public static class MoverAdapter extends BaseAdapter {

        private Context context;
    //  public ArrayList<Bitmap> imageArray;

        public MoverAdapter(Context c ) {

            context = c;
        }

        public int getCount() {
            return imageArray.size();
        }

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

        public long getItemId(int position) {
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            LayoutInflater layoutInflater = (LayoutInflater) context1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = layoutInflater.inflate(R.layout.category_adapter, null);

            ImageView imageView = (ImageView) v.findViewById(R.id.app_img);
            TextView p_Price = (TextView) v.findViewById(R.id.app_size);
            //CheckBox c_box =(CheckBox)v.findViewById(R.id.checkBox1);

            p_Price.setTextColor(Color.BLUE);

            imageView.setImageBitmap(imageArray.get(position));
            //c_box.setId(position);

            if(priceArray.get(position)!=null)
                p_Price.setText(priceArray.get(position));




            return v;

        }
    } 

这是开始的活动

public class description_class extends Activity {

        //private Context context;
        //private ArrayList<Bitmap> imageArray;


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



            Intent i = getIntent();

            int pos = i.getExtras().getInt("id");

            //MoverAdapter mover_adapter = new MoverAdapter(getApplicationContext());


            ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
            imageView.setImageBitmap(imageArray.get(pos));

    }
    }

这是我的宣言

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mooremachine"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mooremachine.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.mooremachine.description_class">


        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

试试这个:

使用此代码替换MainActivity中的构造函数

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

        gridView=(GridView) findViewById(R.id.gridView);

        gridView.setAdapter(new MoverAdapter(this));

        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long id) {
                // TODO Auto-generated method stub

                Intent i = new Intent(SelectCategory.this, description_class.class);
                i.putExtra("id", position);
                startActivity(i);

            //  Toast.makeText(context1, "YEs", Toast.LENGTH_SHORT).show();

            }
        });

    }

Manifest.xml中,声明Application标记内的第二个活动。

<activity
   android:name=".description_class"
   android:label="Description Activity"
   />