Android:Fragment Item上的findViewById返回null

时间:2017-08-14 20:50:01

标签: android listview fragment

我无法弄明白。

当我有时点击ListView中的Item时,我想打开一个Fragment并根据单击的列表项的值设置1 EditText和3 RadioButtons。

{
  "name": "Project 1",
  "version": "1.0.0",
  "description": "Project 1 apis",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "license": "BSD-2-Clause",
  "dependencies": {
    "body-parser": "^1.17.2",
    "connect-multiparty": "^2.0.0",
    "cookie-parser": "^1.4.3",
    "csvtojson": "^1.1.7",
    "express": "~4.14.0",
    "moment": "^2.18.1",
    "mongodb": "^2.2.31",
    "mongoose": "^4.11.6",
    "mongoose-double": "0.0.1",
    "morgan": "^1.8.2",
    "static-favicon": "^2.0.0-alpha",
    "uniqid": "^4.1.1"
  }
}

为什么我会得到空指针异常?我不明白。

1 个答案:

答案 0 :(得分:1)

您应该在Fragment类中处理您的实例。您可以将位置设置为参数,并从您需要的任何位置获取数据并显示视图。

您的 EditTaskFragment 应如下所示:

<div class="well">
<%= bootstrap_form_for @dup do |f| %>

<% if current_user.errors.any? %>
          <div id="error_explanation" class="errors">
            <ul>
            <% current_user.errors.full_messages.each do |message| %>
              <li><%= message %></li>
            <% end %>
            </ul>
          </div>
        <% end %>
    <%= f.text_field :title, placeholder: "Title Here...", label: "Title* (Required)" %>
    <%= f.text_field :company_name, placeholder: "Company Name Here...", label: "Company Name* (Required)" %>
    <%= f.text_area :description, placeholder: "Add a description Here...", label: "Description* (Required)" %>
    <%= f.text_field :location, placeholder: "Location Here...", label: "location* (Required)" %>
    <%= f.collection_select :bus_id, Bus.all, :id, :name, include_blank: 'Select A', label: "A* (required)"%>
    <%= f.label "Needed" %>
    <%= f.collection_check_boxes :dup_ids, Dup.all, :id, :name, label: (fa_icon 'cogs-o 2x') %>
    <%= f.fields_for :dups, @g.dups.build do |dups_fields| %>
      <%= skills_fields.text_field :name, label: "add here (Optional)", placeholder: "add here..." %>
    <% end %>
    <%= f.url_field :url, placeholder: "http://...", label: "Url (Optional) - Must start with http://" %>
    <%=  f.hidden_field :company_id, :value => current_user.id unless @job.company_id %>

    <%= form_tag charges_path do %>
      <article>
        <% if flash[:error].present? %>
          <div id="error_explanation">
            <p><%= flash[:error] %></p>
          </div>
        <% end %>
        <label class="amount">
          <span>Amount: <%= pretty_amount(@amount) %></span>
        </label>
      </article>

      <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
              data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
              data-description="A month's subscription"
              data-amount="<%= @amount %>"
              data-email="<%= current_user.email %>"
              data-locale="auto"></script>
    <% end %>

    <%= f.submit class: "btn btn-primary btn-outline btn-sm" %>
  <% end %>
</div>

然后,您的 openEditTask 函数应为:

public class EditTaskFragment extends Fragment {

    private static final String POSITION_PARAM = "positionParam";

    private int position;

    public EditTaskFragment() {
    }

    public static EditTaskFragment newInstance(int position) {
        EditTaskFragment fragment = new EditTaskFragment();
        Bundle args = new Bundle();
        args.putInt(POSITION_PARAM, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            position = getArguments().getInt(POSITION_PARAM);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.yourfragmentlayout, container, false);
        EditText editTaskName = (EditText) v.findViewById(R.id.edit_task_name_xml);
        RadioButton easyButton = (RadioButton) v.findViewById(R.id.edit_radio_button_easy_xml);
        RadioButton mediumButton = (RadioButton) v.findViewById(R.id.edit_radio_button_medium_xml);
        RadioButton hardButton = (RadioButton) v.findViewById(R.id.edit_radio_button_hard_xml);
        //Edit Text
        editTaskName.setText(mTaskList.get(position).getName());
        //Radio Buttons
        if (mTaskList.get(position).getDifficultyNumber() == 1) {
            easyButton.isChecked();
        } else if (mTaskList.get(position).getDifficultyNumber() == 2) {
            mediumButton.isChecked();
        } else
            hardButton.isChecked();

        return v;
      }    
}

因此,您必须根据用户选择的位置在Fragment类中查找数据。

我希望这能回答你的问题