setOnItemClickListener(新的OnItemClickListener()返回java.lang.NullPointerException
我正在尝试用android创建一个简单的记忆游戏。 当我处于纵向模式时,游戏可以完美运行,但是当我切换到横向模式时,我从内部类Listener得到一个NullPointerException。 我对这个类进行了调试,在那里引发了异常。 我检查了所有变量,以确保没有可以为null的变量。 此外,相同的代码在纵向模式下工作,没有解除安全。 以下是主要活动代码:setOnItemClickListener(新的OnItemClickListener()返回java.lang.NullPointerException,java,android,gridview,nullpointerexception,Java,Android,Gridview,Nullpointerexception,我正在尝试用android创建一个简单的记忆游戏。 当我处于纵向模式时,游戏可以完美运行,但是当我切换到横向模式时,我从内部类Listener得到一个NullPointerException。 我对这个类进行了调试,在那里引发了异常。 我检查了所有变量,以确保没有可以为null的变量。 此外,相同的代码在纵向模式下工作,没有解除安全。 以下是主要活动代码: import android.app.Activity; import android.content.Context; import an
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());
}
}
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.content.res.Configuration;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.GridView;
导入android.widget.ImageView;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
如果(savedInstanceState==null)
{//第一次创建游戏
CreateNewGame(savedInstanceState);
}
其他的
{//不是第一次了
CreateNewGame(savedInstanceState);
}
}
@凌驾
public void onBackPressed(){
//TODO自动生成的方法存根
super.onBackPressed();
Context=getApplicationContext();
CharSequence text=“后退!”;
int duration=Toast.LENGTH\u SHORT;
Toast Toast=Toast.makeText(上下文、文本、持续时间);
toast.show();
}
@凌驾
打开的公共布尔值(int featureId,菜单){
//TODO自动生成的方法存根
Context=getApplicationContext();
CharSequence text=“选项!”;
int duration=Toast.LENGTH\u SHORT;
Toast Toast=Toast.makeText(上下文、文本、持续时间);
toast.show();
返回super.onmenuOpen(featureId,菜单);
}
@凌驾
ActivityResult上的受保护无效(int arg0、int arg1、Intent arg2){
//TODO自动生成的方法存根
super.onActivityResult(arg0、arg1、arg2);
如果(arg1==结果_确定)
{
如果(arg0==1)
{
字符串s1=arg2.getExtras().getString(“MoreGame”);
如果(s1等于(“1”))
{
CreateNewGame(空);
}
else如果(s1等于(“0”))
{
完成();
}
}
}
}
public void CreateNewGame(Bundle bn)
{
最终文本视图Tv=(文本视图)findViewById(R.id.tvId);
Tv.setText(“0”);
GridView GridView=(GridView)findViewById(R.id.GridView);
最终图像适配器板=新图像适配器(此,bn);
gridView.setAdapter(板);
setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
ImageView Im=(ImageView)v;
int来源=板。设置右侧图片(位置,Im);
如果(资源!=0)
{
如果(资源>0)
{
意向意向=新意向(MainActivity.this,SecondActivity.class);
意图。putExtra(“分数”,来源);
//星触觉(意向);
startActivityForResult(意向,1);
}
其他的
{//以负数显示来自适配器的分数
Tv.setText(“+资源*-1”);
}
}
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@凌驾
公共无效OnConfiguration已更改(配置newConfig){
//TODO自动生成的方法存根
super.onConfigurationChanged(newConfig);
if(newConfig.orientation==Configuration.orientation\u横向)
{
setContentView(R.layout.activity_main);
//下面是我们想要做的所有更改。
//如果我们保存数据,我们需要将其加载到landscare选项。
}
if(newConfig.orientation==Configuration.orientation\u纵向)
{
setContentView(R.layout.activity_main);
}
}
@凌驾
SaveInstanceState上受保护的无效(束超出状态){
//TODO自动生成的方法存根
super.onSaveInstanceState(超出状态);
//在这里,我们将所有数据发送到其他模式
outState.putInt(“我的int”,3);
GridView GridView=(GridView)findViewById(R.id.GridView);
ImageAdapter lA=(ImageAdapter)gridView.getAdapter();
/*Context=getApplicationContext();
CharSequence text=“score=“+lA.GetCorrentScore();
int duration=Toast.LENGTH\u SHORT;
Toast Toast=Toast.makeText(上下文、文本、持续时间);
toast.show()*/
“什么