我不知道为什么我会得到空指针异常。这是我第一次发帖,这将意味着世界得到任何反馈。我一直在互联网上研究一段时间,但不能指出什么是错的。我是面向对象编程的新手,所以我真的很感激任何帮助。提前谢谢。
这是全彩网格类
package au.edu.holmesglen.mswemmer.gridviewdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.Random;
public class ColorGrid extends AppCompatActivity {
GridView gridview;
Item[] gridArray = new Item[16];
ImageAdapter iAdapter;
int columncount = 4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_grid);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// generate 4x4 array with all items in the grid set to the grey image
for(int i = 0; i< 16; i++){
gridArray[i] = new Item(R.drawable.grey, "grey");
Log.v("MyApp","GRID CREATED!!!!!");
}
//use the ImageAdapter to pass the array to the GridView object
GridView grid = (GridView) findViewById(R.id.gridview);
iAdapter = new ImageAdapter(this, gridArray);
grid.setAdapter(iAdapter);
//everytime an Item is clicked call the nextColor method to change to a different image
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int x = position / columncount;
int y = position % columncount;
int c = gridArray[position].nextColor();
checkForWin();
((ImageView)v).setImageResource(c);
Toast.makeText(getApplicationContext(), x + " " + y,
Toast.LENGTH_SHORT).show();
}
});
}
private void checkForWin() {
Log.d("MyApp","HELLO!!!!!");
for (int i = 0; i < 16; i += 4) {
// Check for blue
if ((gridArray[i].getColor() == R.drawable.blue
&& gridArray[i + 1].getColor() == R.drawable.blue
&& gridArray[i + 2].getColor() == R.drawable.blue)
|| (gridArray[i + 1].getColor() == R.drawable.blue
&& gridArray[i + 2].getColor() == R.drawable.blue
&& gridArray[i + 3].getColor() == R.drawable.blue))
{
Toast.makeText(getApplicationContext(),
"THREE WHITE IN A ROW HORIZONTALLY", Toast.LENGTH_SHORT)
.show();
Log.d("MyApp", "3 in row horizontally BLUE");
}
// Check for green
if ((gridArray[i].getColor() == R.drawable.green
&& gridArray[i + 1].getColor() == R.drawable.green
&& gridArray[i + 2].getColor() == R.drawable.green)
|| (gridArray[i + 1].getColor() == R.drawable.green
&& gridArray[i + 2].getColor() == R.drawable.green
&& gridArray[i + 3].getColor() == R.drawable.green))
{
Toast.makeText(getApplicationContext(),
"THREE RED IN A ROW HORIZONTALLY", Toast.LENGTH_SHORT)
.show();
Log.d("MyApp", "3 in row horizontally GREEN");
}
}
// Check for vertical loss
for (int i = 0; i <= 7; i++) {
/*
* This for loop is looping through each column and checking if 3 drawables are in a row
*/
//Check for blue loss
if (gridArray[i].getColor() == R.drawable.blue
&& gridArray[i + 4].getColor() == R.drawable.blue
&& gridArray[i + 8].getColor() == R.drawable.blue)
{
Toast.makeText(getApplicationContext(),
"THREE WHITE IN A ROW VERTICALLY", Toast.LENGTH_SHORT).show();
Log.d("MyApp", "3 in row vertically BLUE");
}
//Check for green loss
if (gridArray[i].getColor() == R.drawable.green
&& gridArray[i + 4].getColor() == R.drawable.green
&& gridArray[i + 8].getColor() == R.drawable.green) {
Toast.makeText(getApplicationContext(),
"THREE RED IN A ROW VERTICALLY", Toast.LENGTH_SHORT).show();
Log.d("MyApp", "3 in row vertically GREEN");
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_color_grid, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的日志
08-23 18:00:46.219 7772-7772/au.edu.holmesglen.mswemmer.gridviewdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: au.edu.holmesglen.mswemmer.gridviewdemo, PID: 7772
java.lang.RuntimeException: Unable to start activity ComponentInfo{au.edu.holmesglen.mswemmer.gridviewdemo/au.edu.holmesglen.mswemmer.gridviewdemo.ColorGrid}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2439)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$800(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at au.edu.holmesglen.mswemmer.gridviewdemo.ColorGrid.onCreate(ColorGrid.java:45)
at android.app.Activity.performCreate(Activity.java:5312)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
at android.app.ActivityThread.access$800(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
<强>被修改强>
@MrSwemmer你仔细检查过gridview的id与放在这里的相同吗?.it可以是另一个gridview的id(如果存在于另一个布局中)
错误来自活动ColorGrid.class
,请参阅第45行的代码。
或在此处发布活动代码
答案 1 :(得分:0)
根据@MrSwemmer的评论,有问题的行是percent_rank().over(Window.orderBy("x"))
。
请检查t x perc_rank_win
0 1 0.0
1 3 1.0 # since 3 is largest from [1, 3]
2 5 1.0 # since 5 is largest from [1, 3, 5]
3 4 0.66 # since values are [1, 3, 4!, 5]
4 2 0.25 # since [1, 2!, 3, 4, 5]
并确保视图grid.setAdapter(iAdapter)
实际存在。
在第45行之前添加以下调试代码以进行验证:
GridView grid = (GridView) findViewById(R.id.gridview);