反向搜索的findViewById不起作用

时间:2018-11-25 15:42:55

标签: android android-studio android-layout

我现在很困惑。我希望某种方法也可以逆向工作。但显然不是……我正在PlayerView类中以编程方式创建视图。在方法player_table中,我分配了一个onclick侦听器。而且效果很好。但是,在类PlayerLifePoints_Functions中,我创建了onclick方法。该方法本身可以正常工作。问题如下:我正在手动分配ID(以使我在以后的处理步骤中更容易工作)。现在,我可以从单击的元素中读取ID。但是,如果我想通过ID(例如400)准确地找到此元素,则确实会返回错误:

  

错误:java.lang.NullPointerException:尝试在... PlayerLifePoints_Functions.onClick(PlayerLifePoints_Functions.java)的空对象引用上调用虚拟方法“ android.view.Window $ Callback android.view.Window.getCallback()” :21)->使用log命令的行

我不知道为什么。我也尝试过使用getParent(),但这也不起作用。有什么想法,问题可能在哪里?

  

Class PlayerView

public class PlayerView extends View {

    private RelativeLayout relativeLayout;
    private TableLayout tableLayout;
    private int player_loop;
    private int player_count;

    public PlayerView(Context context, int player_count){
        super(context);
        setPlayer_count(player_count);
    }

    public ScrollView create_scrollView(){

        ScrollView scrollView = new ScrollView(getContext());
        ScrollView.LayoutParams scroll_params = new ScrollView.LayoutParams(
                ScrollView.LayoutParams.MATCH_PARENT,
                ScrollView.LayoutParams.MATCH_PARENT
        );
        scrollView.setLayoutParams(scroll_params);
        scrollView.addView(create_relativeLayout());

        return scrollView;
    }

    public HorizontalScrollView create_relativeLayout(){

        HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());

        relativeLayout = new RelativeLayout(getContext());
        RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT
        );
        relativeLayout.setLayoutParams(relativeParams);
        create_Player_Table_Layout();

        horizontalScrollView.addView(relativeLayout);
        return horizontalScrollView;

    }

    public void create_Player_Table_Layout(){

        for(player_loop = 1; player_loop <= player_count; player_loop++){

            relativeLayout.addView(player_table(getResources().getString(R.string.dummy_player_name) + player_loop, player_loop));

        }
    }

    public TableLayout player_table(String playername, int playernumber){

        tableLayout = new TableLayout(getContext());
        tableLayout.setId(playernumber * 1000);

        if (playernumber > 1) {

            //TABLE PLACEMENT
            RelativeLayout.LayoutParams tbl_params_New = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            tbl_params_New.addRule(RelativeLayout.RIGHT_OF, (playernumber - 1) * 1000);
            tableLayout.setLayoutParams(tbl_params_New);
        }

        //Add Playername
        TableRow row_playername = new TableRow(getContext());
        TextView view_name = new TextView(getContext());

        TableRow.LayoutParams view_name_params = new TableRow.LayoutParams();
        view_name_params.setMargins(20,20,20,20);

        view_name.setLayoutParams(view_name_params);
        view_name.setGravity(Gravity.CENTER);
        view_name.setText(playername);
        view_name.setTextSize(20);
        view_name.setId(playernumber * 100);

        //OnClickListener
        PlayerName_Functions listener_name = new PlayerName_Functions();
        view_name.setOnClickListener(listener_name);

        row_playername.addView(view_name);
        tableLayout.addView(row_playername);

        //Add Lifepoints
        TableRow row_lifepoints = new TableRow(getContext());
        TextView view_lifepoints = new TextView(getContext());

        TableRow.LayoutParams view_lifepoints_params = new TableRow.LayoutParams();
        view_lifepoints_params.setMargins(20, 0, 20, 20);

        view_lifepoints.setText("40");
        view_lifepoints.setTextSize(40);
        view_lifepoints.setGravity(Gravity.CENTER);
        view_lifepoints.setId(playernumber * 100 + 10);
        view_lifepoints.setLayoutParams(view_lifepoints_params);

        //OnClickListener
        PlayerLifePoints_Functions listener_lifepoints = new PlayerLifePoints_Functions();
        view_lifepoints.setOnClickListener(listener_lifepoints);

        row_lifepoints.addView(view_lifepoints);
        tableLayout.addView(row_lifepoints);

        for(int opponent_loop = 1; opponent_loop <= player_count; opponent_loop++){
            tableLayout.addView(commander_damage_from_player(player_loop, opponent_loop));
        }

        return tableLayout;
    }
  

PlayerLifePoints类(包含在上面的类中实现的OnClick函数)

public class PlayerLifePoints_Functions extends AppCompatActivity implements View.OnClickListener {

    @Override
    public void onClick(View v) {

        TextView textView = (TextView) v;

        String lp = textView.getText().toString();
        int id = textView.getId();
        int playernumber =  Character.getNumericValue(String.valueOf(id).charAt(0));

        Log.d("Test", "ID: " + findViewById(v.getId()).getId());

//        Intent intent = new Intent(v.getContext(), Update_LifePoints.class);
//        intent.putExtra( "Update Reason", "Test");
////        intent.putExtra("Player Name", playername);
//        intent.putExtra("Lifepoints", lp);
//
//        v.getContext().startActivity(intent);


    }

1 个答案:

答案 0 :(得分:1)

您的课程PlayerLifePoints_Functions无法使用findViewById,因为它没有内容。通常,活动以Intents开始,它们通过覆盖onCreate方法来扩大布局。

如果只想使其工作,可以使用:

v.getRootView().findViewById(v.getId()).getId());

在这种情况下,您不需要从AppCompatActivity扩展到

或更佳的选择是放弃课程:

view_lifepoints.setOnClickListener( View.OnClickListener {

  @Override
  public void onClick(View v) {

      TextView textView = (TextView) v;

      String lp = textView.getText().toString();
      int id = textView.getId();
      int playernumber =  Character.getNumericValue(String.valueOf(id).charAt(0));

      Log.d("Test", "ID: " + findViewById(v.getId()).getId());
  }

);

通常: 我建议您不要将整个逻辑放在视图中,而应放在更底层。在活动中使用它已经是原型开发的一种改进。如果您的代码增长了,则切换到MVPMVVM可能很有意义。

Google提供了一些不错的资源来帮助您入门: https://developer.android.com/guide/components/activities/intro-activities https://developer.android.com/topic/libraries/architecture/