gridView.setOnItemClickListener(new OnItemClickListener()返回java.lang.NullPointerException

时间:2014-05-11 13:07:41

标签: java android gridview nullpointerexception

我正在尝试在android中创建一个简单的记忆游戏。 当我处于PORTRAIT模式时游戏工作完美,但是当我切换到LANDSCAPE模式时,我从内部类OnItemClickListener获取NullPointerException。 我在类上进行了调试,这就是抛出异常的地方。 我确实检查了所有变量,以确保没有任何东西可以无效。 同样,它在PORTRAIT模式下工作的代码没有缺陷。 这是主要的活动代码:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

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


        if (savedInstanceState == null )
        { // first time to create game 
            CreateNewGame(savedInstanceState);
        }
        else
        { // not the first time 
            CreateNewGame(savedInstanceState);
        }



    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        Context context = getApplicationContext();
        CharSequence text = "back!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show(); 
    }

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        // TODO Auto-generated method stub

        Context context = getApplicationContext();
        CharSequence text = "options!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show(); 
        return super.onMenuOpened(featureId, menu);

    }
    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        // TODO Auto-generated method stub
        super.onActivityResult(arg0, arg1, arg2);

        if( arg1== RESULT_OK)
        {
            if(arg0  == 1)
            {
                String s1 =     arg2.getExtras().getString("MoreGame");
                if(s1.equals("1"))
                {

                    CreateNewGame(null);
                }
                else if(s1.equals("0"))
                {
                    finish();
                }


            }
        }
    }

    public void CreateNewGame(Bundle bn)
    {
        final TextView Tv = (TextView) findViewById(R.id.tvId);
        Tv.setText("0");

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


            final ImageAdapter Plate = new ImageAdapter(this,bn  );
            gridView.setAdapter(Plate);




        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                ImageView Im = (ImageView)v;
                int theSourceInt = Plate.SetTheRightPicture(position, Im);
                if (theSourceInt != 0) 
                {

                    if(theSourceInt>0)
                    {   




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

                        intent.putExtra("score", theSourceInt);
                        //startActivity(intent);
                        startActivityForResult(intent, 1);
                    }
                    else
                    { // show score that come from adapter in a negative number 

                        Tv.setText( "" + theSourceInt * -1 );
                    }

                }

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            setContentView(R.layout.activity_main);
            //here will be all the changes we will want to make .
            // if we save the data we need to load it here to a landscare option.
        }

        if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            setContentView(R.layout.activity_main);
        }


    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        // here we send all data to the other mode 
        outState.putInt("my int", 3);

        GridView gridView = (GridView) findViewById(R.id.gridview);
        ImageAdapter lA =   (ImageAdapter) gridView.getAdapter();

        /*Context context = getApplicationContext();
        CharSequence text = "score = " + lA.GetCorrentScore(); ;
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();*/


        outState.putIntArray("WhatHasBeenPressedVector" , lA.GetWhatHasBeenPressedVector());
        outState.putInt("CorrentScore" , lA.GetCorrentScore() );
        outState.putIntArray("PicturesIdFromList", lA.PicturesIdFromList());
        outState.putBoolean("OnePicReveal",     lA.getOnePicReveal());






    }

}

0 个答案:

没有答案