0

我想使用 tablerow 在警报对话框中显示我的表 sql,并在 main.javainflater 中手动创建表score.xml

 //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("-");
                    }

分数.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>

日志猫

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); 当我用 toast 检查计时器时我得到了值

它认为我得到了 nullexceptiontextview

创建方法

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();
4

2 回答 2

2

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

TextView time,...;

创建

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

编辑 观看您的编辑,我看到您设置了 contentView,R.layout.activity_slidame4但您的 xml 布局文件被命名为score.xml. 尝试:

setContentView(R.layout.score);

反而

setContentView(R.layout.activity_slidame4);
于 2013-06-21T07:26:36.723 回答
0

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

编辑:

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

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

if (textView31 != null)
    Log.d("me", textView31.getText());
else 
    Log.d("me", "textview is null!!!");
于 2013-06-21T07:34:29.303 回答