我開發一個機器人的應用程序,使用eclipse IDE,其中一部分通過遠程網關讀取數據,然後將信息被呈現爲一個表 - 使用TableLayout和的TableRow,動態在Java中。我希望能夠顯示數據,然後選擇一個元素,並在此基礎上執行一些其他操作。v.getParent導致運行時故障
數據的提取,並在屏幕的產生和列表工作正常。我已將其設置爲可點擊,並設置適當的onClickListener。我能夠進入的onClick方法(由具有v.getId輸出成熟的()) - (看到在Eclipse IDE的logcat中),在onClickListener內。但是,在onClick方法中嘗試v.getParent時,Eclipse模擬器失敗。 VM關閉後顯示的錯誤消息顯示:
java.lang.ClassCastException:android.widget.TableLayout無法轉換爲android.widget.TableRow。
我花了幾天試圖找出有問題的,但顯然不明白我有什麼錯,所以我伸手求助。無論這是一個正確的方向,還是具體告訴我什麼是錯誤的,任何幫助將不勝感激。
最終我想以提取在的TableRow(節點)中的一個字段 - 這是一個獨特的整數地址,並使用此以供進一步處理。
Java代碼:
public void readNodeKeys(int[] value) {
setContentView(R.layout.testrz_nodes);
TabHost node_host = (TabHost) findViewById(R.id.tabhost);
node_host.setup();
TabSpec nodeTab = node_host.newTabSpec("node_tabs");
nodeTab.setIndicator(getResources().getString(R.string.node_information),
getResources().getDrawable(android.R.drawable.star_on));
nodeTab.setContent(R.id.node_ScrollView2);
node_host.addTab(nodeTab);
node_host.setCurrentTabByTag("node_tabs");
TableLayout list_table = (TableLayout) findViewById(R.id.node_TableLayout2);
initializeHeaderRow(list_table);
try {
processScores(list_table, value);
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to load list status data", e);
}
}
public void initializeHeaderRow(TableLayout statusTable) {
TableRow headerRow = new TableRow(this);
int textColor = getResources().getColor(R.color.testrz_logo_color);
float textSize = getResources().getDimension(R.dimen.testrz_nodelist_text_size_6);
addTextToRowWithValues(headerRow, getResources().getString(R.string.testrz_nodelist_node), textColor, textSize);
addTextToRowWithValues(headerRow, getResources().getString(R.string.testrz_nodelist_data_index), textColor, textSize);
addTextToRowWithValues(headerRow, getResources().getString(R.string.testrz_nodelist_zone_A), textColor, textSize);
addTextToRowWithValues(headerRow, getResources().getString(R.string.testrz_nodelist_zone_B), textColor, textSize);
addTextToRowWithValues(headerRow, getResources().getString(R.string.testrz_nodelist_zone_C), textColor, textSize);
addTextToRowWithValues(headerRow, getResources().getString(R.string.testrz_nodelist_module_type), textColor, textSize);
statusTable.addView(headerRow);
}
public void addTextToRowWithValues(final TableRow tableRow, String text, int textColor, float textSize) {
TextView textView = new TextView(this);
textView.setTextSize(textSize);
textView.setTextColor(textColor);
textView.setText(text);
tableRow.setClickable(true);
tableRow.setOnClickListener(tableRowOnClickListener);
tableRow.addView(textView);
}
public OnClickListener tableRowOnClickListener = new OnClickListener() {
public void onClick(View v) {
System.out.println("Row Clicked : " + v.getId());
TableRow tr = (TableRow)v.getParent();
System.out.println("We've got to the TableRow tr");
TextView tv = (TextView) tr.getChildAt(index);
System.out.println("We've got to the TableView tv");
String result = tv.getText().toString();
System.out.println("On Click string is : " + result);
}
};
public void processScores(final TableLayout statusTable, int[] value) throws
IOException {
int counter = 0;
System.out.println("mtestrz_Node_Count : " + Integer.toHexString(mtestrz_Node_Count));
for (counter = 0; counter < mtestrz_Node_Count; counter++) {
testrz_Node_Key_Values RNKV = new testrz_Node_Key_Values();
offset = 0x10;
while (value[(counter * offset)+18] != 0xff) {
int eventType = -1;
boolean bFoundScores = false;
// Find list
nodelist[mtestrz_Node_Count][0] = value[(counter * offset)+18];
nodelist[mtestrz_Node_Count][1] = value[(counter * offset)+19];
nodelist[mtestrz_Node_Count][2] = value[(counter * offset)+20];
nodelist[mtestrz_Node_Count][3] = value[(counter * offset)+21];
nodelist[mtestrz_Node_Count][4] = value[(counter * offset)+22];
nodelist[mtestrz_Node_Count][5] = value[(counter * offset)+23];
nodelist[mtestrz_Node_Count][6] = value[(counter * offset)+24];
nodelist[mtestrz_Node_Count][7] = value[(counter * offset)+25];
nodelist[mtestrz_Node_Count][8] = value[(counter * offset)+26];
nodelist[mtestrz_Node_Count][9] = value[(counter * offset)+27];
nodelist[mtestrz_Node_Count][10] = value[(counter * offset)+28];
nodelist[mtestrz_Node_Count][11] = value[(counter * offset)+29];
nodelist[mtestrz_Node_Count][12] = value[(counter * offset)+30];
nodelist[mtestrz_Node_Count][13] = value[(counter * offset)+31];
nodelist[mtestrz_Node_Count][14] = value[(counter * offset)+32];
nodelist[mtestrz_Node_Count][15] = value[(counter * offset)+33];
RNKV.Node_add = nodelist[mtestrz_Node_Count][1] * 0x100 + nodelist[mtestrz_Node_Count][0];
RNKV.Code_Control = nodelist[mtestrz_Node_Count][3] * 0x100 + nodelist[mtestrz_Node_Count][2];
RNKV.Data_index = nodelist[mtestrz_Node_Count][5] * 0x100 + nodelist[mtestrz_Node_Count][4];
RNKV.Zone_A = nodelist[mtestrz_Node_Count][7] * 0x100 + nodelist[mtestrz_Node_Count][6];
RNKV.Zone_B = nodelist[mtestrz_Node_Count][9] * 0x100 + nodelist[mtestrz_Node_Count][8];
RNKV.Zone_C = nodelist[mtestrz_Node_Count][11] * 0x100 + nodelist[mtestrz_Node_Count][10];
RNKV.Module_type = nodelist[mtestrz_Node_Count][12];
Module_Type module_str;
module_str = (module_type_str[RNKV.Module_type]);
String module_string;
module_string = module_str.toString();
bFoundScores = true;
String node = Integer.toHexString(RNKV.Node_add);
String data_index = Integer.toHexString(RNKV.Data_index);
String Zone_A = Integer.toHexString(RNKV.Zone_A);
String Zone_B = Integer.toHexString(RNKV.Zone_B);
String Zone_C = Integer.toHexString(RNKV.Zone_C);
String module_type = module_string;
insertStatusRow(statusTable, node, data_index, Zone_A, Zone_B, Zone_C, module_type);
// Handle no scores available
if (bFoundScores == false) {
final TableRow newRow = new TableRow(this);
TextView noResults = new TextView(this);
noResults.setText(getResources().getString(R.string.testrz_no_data));
newRow.addView(noResults);
statusTable.addView(newRow);
}
counter = counter + 1;
}
}
}
public void insertStatusRow(final TableLayout statusTable, String node, String data_index, String Zone_A, String Zone_B, String Zone_C, String module_type) {
final TableRow newRow = new TableRow(this);
int textColor = getResources().getColor(R.color.testrz_title_color);
float textSize = getResources().getDimension(R.dimen.testrz_nodelist_text_size_6);
addTextToRowWithValues(newRow, node, textColor, textSize);
addTextToRowWithValues(newRow, data_index, textColor, textSize);
addTextToRowWithValues(newRow, Zone_A, textColor, textSize);
addTextToRowWithValues(newRow, Zone_B, textColor, textSize);
addTextToRowWithValues(newRow, Zone_C, textColor, textSize);
addTextToRowWithValues(newRow, module_type, textColor, textSize);
statusTable.addView(newRow);
}
XML代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/rayzig_nodes_RelativeLayout02"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/rayzig_nodes_Textview02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/nodes" />
<DigitalClock
android:id="@+id/rayzig_nodes_digitalClock2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_below="@+id/rayzig_nodes_Textview02"
android:layout_marginTop="17dp"
android:text="@string/clock" />
</RelativeLayout>
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="@+id/node_ScrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/node_TableLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*" >
</TableLayout>
</ScrollView>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
你試過任何答案? – dd619
我嘗試了這些想法之後,對每個迴應都進行了評論。謝謝 – user2504929