Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java v、 onClickListener中OnClick中的getParent导致运行时失败_Java_Android_Onclicklistener - Fatal编程技术网

Java v、 onClickListener中OnClick中的getParent导致运行时失败

Java v、 onClickListener中OnClick中的getParent导致运行时失败,java,android,onclicklistener,Java,Android,Onclicklistener,我正在开发一个android应用程序,使用EclipseIDE,其中一部分通过远程网关读取数据,然后将信息显示为一个表——使用Java动态的TableLayout和TableRow。我希望能够显示数据,然后选择一个元素,并在此基础上执行一些其他操作 数据提取、屏幕和列表的生成工作正常。我已将其设置为可单击,并设置了适当的OnClick侦听器。我能够在onClick Listener中使用onClick方法(通过使用v.getId()进行输出得到验证)-(见EclipseIDE的LogCat)。但

我正在开发一个android应用程序,使用EclipseIDE,其中一部分通过远程网关读取数据,然后将信息显示为一个表——使用Java动态的TableLayout和TableRow。我希望能够显示数据,然后选择一个元素,并在此基础上执行一些其他操作

数据提取、屏幕和列表的生成工作正常。我已将其设置为可单击,并设置了适当的OnClick侦听器。我能够在onClick Listener中使用onClick方法(通过使用v.getId()进行输出得到验证)-(见EclipseIDE的LogCat)。但是,当尝试在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);
    }
public void readNodeKeys(int[]值){
setContentView(R.layout.testrz_节点);
TabHost节点_host=(TabHost)findviewbyd(R.id.TabHost);
node_host.setup();
TabSpec nodeTab=node_host.newTabSpec(“node_tabs”);
nodeTab.setIndicator(getResources().getString(R.string.node_信息),
getResources().getDrawable(android.R.drawable.star_on));
nodeTab.setContent(R.id.node_ScrollView2);
node_host.addTab(nodeTab);
node_host.setCurrentTabByTag(“node_选项卡”);
TableLayout list\u table=(TableLayout)findViewById(R.id.node\u TableLayout2);
初始化HeaderRow(列表表);
试一试{
过程分数(列表、表格、值);
}捕获(例外e){
Log.e(调试标签,“加载列表状态数据失败”,e);
}
}
public void initializeHeaderRow(表布局状态表){
TableRow headerRow=新的TableRow(本);
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_节点列表_模块_类型)、textColor、textSize);
statusTable.addView(headerRow);
}
public void addTextToRowWithValues(最终表格行、表格行、字符串文本、int textColor、浮点文本大小){
TextView TextView=新的TextView(此);
textView.setTextSize(textSize);
setTextColor(textColor);
textView.setText(text);
tableRow.setClickable(真);
setOnClickListener(tableRowOnClickListener);
tableRow.addView(textView);
}
公共OnClickListener表RowOnClickListener=新OnClickListener(){
公共void onClick(视图v){
System.out.println(“单击的行:+v.getId());
TableRow tr=(TableRow)v.getParent();
System.out.println(“我们已经到了TableRow tr”);
TextView tv=(TextView)tr.getChildAt(索引);
System.out.println(“我们要看TableView电视”);
字符串结果=tv.getText().toString();
System.out.println(“单击时字符串为:“+result”);
}
};
public void processScores(最终表格布局状态表,int[]值)抛出
IOException{
int计数器=0;
System.out.println(“mtestrz_Node_Count:+Integer.toHexString(mtestrz_Node_Count));
用于(计数器=0;计数器<?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>
 View parent = (View)v.getParent();
    if (parent != null) {
        TableRow tr = parent.findViewById(R.id.parent);//your xml resource

    }
TableLayout tablelayout= (TableLayout )v.getParent();