settext上的空指针异常

时间:2013-06-21 07:23:33

标签: android

我希望使用包含inflater的tablerow在警告对话框中显示我的表sql 在score.xml中手动制作表格 main.java

 //table3
                Cursor gethighscorealter=highscoreDB.rawQuery("SELECT*FROM HIGHSCORE3"+
                           " ORDER BY TIME asc, MOVE asc ;"
                           , null);
                    if(gethighscorealter.getCount()>0){
                        gethighscorealter.moveToFirst();
                        timer= gethighscorealter.getLong(gethighscorealter.getColumnIndex("TIME")); 
                        move = gethighscorealter.getInt(gethighscorealter.getColumnIndex("MOVE"));
                        grid = gethighscorealter.getInt(gethighscorealter.getColumnIndex("GRID"));
                        textView31.setText(""+timer);
                        textView32.setText(""+move);
                        textView33.setText(""+grid);
                    }else{
                        //textView31.setText("-");textView32.setText("-");textView33.setText("-");
                    }

score.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/table_score"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:stretchColumns="*" >
<TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:selectable="false"
        android:focusable="false"
             >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left|center_vertical"
            android:singleLine="true"
            android:text="TIME"
            android:textAppearance="?android:attr/textAppearanceMedium"
            />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:singleLine="true"
            android:text="MOVE"
            android:textAppearance="?android:attr/textAppearanceMedium"
            />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:singleLine="true"
            android:text="GRID"
            android:textAppearance="?android:attr/textAppearanceMedium"
            />

    </TableRow>
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:selectable="false"
        android:focusable="false"
             >

        <TextView
            android:id="@+id/textView31"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left|center_vertical"
            android:singleLine="true"
            android:text="TIME"
            android:textAppearance="?android:attr/textAppearanceMedium"
            />

        <TextView
            android:id="@+id/textView32"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:singleLine="true"
            android:text="MOVE"
            android:textAppearance="?android:attr/textAppearanceMedium"
            />

        <TextView
            android:id="@+id/textView33"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:singleLine="true"
            android:text="GRID"
            android:textAppearance="?android:attr/textAppearanceMedium"
            />

    </TableRow>

logcat的

06-21 14:05:03.169: E/AndroidRuntime(1504): FATAL EXCEPTION: main
06-21 14:05:03.169: E/AndroidRuntime(1504): java.lang.NullPointerException
06-21 14:05:03.169: E/AndroidRuntime(1504):     at skripsi.slidame.PuzzleActivity.highscore(PuzzleActivity.java:280)
06-21 14:05:03.169: E/AndroidRuntime(1504):     at skripsi.slidame.PuzzleActivity.onOptionsItemSelected(PuzzleActivity.java:143)
06-21 14:05:03.169: E/AndroidRuntime(1504):     at android.app.Activity.onMenuItemSelected(Activity.java:2548)

java:280应为textView31.setText(""+timer); 当我用吐司检查计时器时我得到了值

它认为我从textview

获得了无效感

oncreate方法

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      //db
      String DB_PATH= getApplicationContext().getFilesDir().getPath();
      highscoreDB = SQLiteDatabase.openDatabase(DB_PATH+slidameBoard.DB_NAME,null,SQLiteDatabase.CREATE_IF_NECESSARY);
      new countleft(START_DELAY, INTERVAL);
      //sensor
      sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
      sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
      //lebar
      DisplayMetrics displayMetrics = new DisplayMetrics();
      WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
      wm.getDefaultDisplay().getMetrics(displayMetrics);
      lebar = displayMetrics.widthPixels;
      setContentView(R.layout.activity_slidame4);
    textView31 = (TextView)findViewById(R.id.textView31);
    textView32 = (TextView)findViewById(R.id.textView32);
    textView33 = (TextView)findViewById(R.id.textView33);
    textView41 = (TextView)findViewById(R.id.textView41);
    textView42 = (TextView)findViewById(R.id.textView42);
    textView43 = (TextView)findViewById(R.id.textView43);
   }   

膨胀方法

AlertDialog.Builder showhighscore =new AlertDialog.Builder(this);
            LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.score, null);//score
            showhighscore.setCancelable(false);
            showhighscore.setTitle(" ");
            showhighscore.setView(layout);
            showhighscore.setIcon(R.drawable.menu_icon);
            showhighscore.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.cancel();
                    onResume();
                }
            });
               showhighscore.show();

2 个答案:

答案 0 :(得分:2)

您必须在onCreate方法中初始化您的视图,以便将布局元素与您的活动元素相关联:

TextView time,...;

<强>的onCreate

setContentView(R.layout.yourLayout);
time = (TextView)findViewById(R.id.textView1);
...

修改 观看您的编辑后,我看到您使用R.layout.activity_slidame4设置了contentView,但您的xml布局文件名为score.xml。尝试:

setContentView(R.layout.score);

而不是

setContentView(R.layout.activity_slidame4);

答案 1 :(得分:0)

定时器为空,或者没有R.id.textView31。尝试在异常之前将它们打印出来,看看哪一个给出异常。

编辑:

你确定R.id.textView31存在吗?

如果是这样,请尝试查看textview中的值:

if (textView31 != null)
    Log.d("me", textView31.getText());
else 
    Log.d("me", "textview is null!!!");