在片段内部实现微调器

雅各布·塞莱斯廷(Jacob Celestine)

首先,我是android编程的新手,所以如果这是一些菜鸟的错误,我真的很抱歉。我实现了与活动相同的文件,并且可以正常工作。但是,当我将其放入片段中时,事实并非如此。我无法获得有关片段的不错的教程,因此我实现的内容主要来自理论和示例项目。错误日志中显示“ java.lang.NullPointerException:尝试调用虚拟方法'android.view.View android.view .View.findViewById(int)'引用为空对象”。

我检查了stackoverflow是否相同,并找到了有关错误ID的答案。但我认为,对于我来说,id指向正确。如果有人可以帮助我,那就太好了。哦,如果可以的话,请告诉我我是否已经在适当的函数中编写了代码。为了澄清起见,函数public void equalClicked(View v)是单击按钮时应该发生的计算部分。我已经为此工作了将近2天。-_-

这是我的PhotosFragment.java文件:

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

public class PhotosFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters
EditText age,weight,height;
TextView answer,answer2,answer3;
String selected,a,b,c;
RadioGroup gr;
RadioButton r;
double ans1,ans2,ans3;

// TODO: Rename and change types of parameters

private OnFragmentInteractionListener mListener;

public PhotosFragment() {
    // Required empty public constructor
}

// TODO: Rename and change types and number of parameters
public static PhotosFragment newInstance() {
    PhotosFragment fragment = new PhotosFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    Spinner myspinner=(Spinner) getView().findViewById(R.id.fragment_photos_spinner);
    ArrayAdapter<String> myAdapter =new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.act));
    myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    myspinner.setAdapter(myAdapter);
    age= (EditText) getView().findViewById (R.id.fragment_photos_age);
    weight= (EditText) getView().findViewById (R.id.fragment_photos_weight);
    height= (EditText) getView().findViewById (R.id.fragment_photos_height);
    answer= (TextView) getView().findViewById (R.id.fragment_photos_answer);
    answer2= (TextView) getView().findViewById (R.id.fragment_photos_answer2);
    answer3= (TextView) getView().findViewById (R.id.fragment_photos_answer3);
    gr=(RadioGroup) getView().findViewById(R.id.fragment_photos_rgroup);
    myspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
        @Override
        public void onItemSelected (AdapterView<?> parent,View view,int position,long id ){
            selected=(String)parent.getItemAtPosition(position);
        }
        @Override
        public void onNothingSelected (AdapterView<?> parent){

        }
    });
    return inflater.inflate(R.layout.fragment_photos, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

public void equalClicked(View v){
    double input1= Double.parseDouble(age.getText().toString());
    double input2= Double.parseDouble(weight.getText().toString());
    double input3= Double.parseDouble(height.getText().toString());
    int rid=gr.getCheckedRadioButtonId();
    r=(RadioButton) getView().findViewById(rid);
    String s=r.getText().toString();
    if(s.contentEquals("Male")) {
        ans1=((10 * input2) + (6.25 * input3) - (5 * input1) + 5 );
    }
    else
    {
        ans1=((10 * input2) + (6.25 * input3) - (5 * input1) - 161 );
    }
    if(selected.contentEquals("Little to No exercise")){
        ans1=ans1 * 1.2;
        ans2=ans1 + 1000;
        ans3=ans1 - 1000;
        a=Double.toString(ans1);
        b=Double.toString(ans2);
        c=Double.toString(ans3);
        answer.setText(a + "cal");
        answer2.setText(b + "cal to increase 1 kg in a week");
        answer3.setText(c + "cal to decrease 1 kg in a week");

    }
    else if(selected.contentEquals("Light exercise (1–3 days per week)")){
        ans1=ans1 * 1.375;
        ans2=ans1 + 1000;
        ans3=ans1 - 1000;
        a=Double.toString(ans1);
        b=Double.toString(ans2);
        c=Double.toString(ans3);
        answer.setText(a + "cal");
        answer2.setText(b + "cal to increase 1 kg in a week");
        answer3.setText(c + "cal to decrease 1 kg in a week");
    }
    else if(selected.contentEquals("Moderate exercise (3–5 days per week)")){
        ans1=ans1 * 1.55;
        ans2=ans1 + 1000;
        ans3=ans1 - 1000;
        a=Double.toString(ans1);
        b=Double.toString(ans2);
        c=Double.toString(ans3);
        answer.setText(a + "cal");
        answer2.setText(b + "cal to increase 1 kg in a week");
        answer3.setText(c + "cal to decrease 1 kg in a week");
    }
    else if(selected.contentEquals("Heavy exercise (6–7 days per week)")){
        ans1=ans1 * 1.725;
        ans2=ans1 + 1000;
        ans3=ans1 - 1000;
        a=Double.toString(ans1);
        b=Double.toString(ans2);
        c=Double.toString(ans3);
        answer.setText(a + "cal");
        answer2.setText(b + "cal to increase 1 kg in a week");
        answer3.setText(c + "cal to decrease 1 kg in a week");
    }
    else{
        ans1=ans1 * 1.9;
        ans2=ans1 + 1000;
        ans3=ans1 - 1000;
        a=Double.toString(ans1);
        b=Double.toString(ans2);
        c=Double.toString(ans3);
        answer.setText(a + "cal");
        answer2.setText(b + "cal to increase 1 kg in a week");
        answer3.setText(c + "cal to decrease 1 kg in a week");
    }

}
}

这是相应的xml文件:

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center"
    android:alpha="0.3"
    android:src="@drawable/ic_photo_library_black_24dp" />


<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <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="wrap_content"
                  android:paddingBottom="@dimen/activity_vertical_margin"
                  android:paddingLeft="@dimen/activity_horizontal_margin"
                  android:paddingRight="@dimen/activity_horizontal_margin"
                  android:paddingTop="@dimen/activity_vertical_margin"
                  android:orientation="vertical"
                  tools:context=".PhotosFragment">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lbl_age"
            android:textAppearance="?android:attr/textAppearanceLarge"/>
        <EditText
            android:id="@+id/fragment_photos_age"
            android:inputType="number"
            android:layout_width="178dp"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lbl_gender"
            android:textAppearance="?android:attr/textAppearanceLarge"/>
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/fragment_photos_rgroup"
            android:orientation="horizontal">
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lbl_female"
                android:id="@+id/fragment_photos_female"
                android:layout_gravity="center_horizontal" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lbl_male"
                android:id="@+id/fragment_photos_male" />

        </RadioGroup>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lbl_weight"
            android:textAppearance="?android:attr/textAppearanceLarge"/>

        <EditText
            android:id="@+id/fragment_photos_weight"
            android:inputType="number"
            android:layout_width="171dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lbl_height"
            android:textAppearance="?android:attr/textAppearanceLarge"/>

        <EditText
            android:id="@+id/fragment_photos_height"
            android:inputType="number"
            android:layout_width="176dp"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lbl_daily_activity"
            android:textAppearance="?android:attr/textAppearanceLarge"/>

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/fragment_photos_spinner"
            android:layout_gravity="center_horizontal" />

        <Button
            android:text="@string/btn_calculate"
            android:onClick="equalClicked"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/fragment_photos_answer"
            android:layout_width="400dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"/>
        <TextView
            android:id="@+id/fragment_photos_answer2"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"/>
        <TextView
            android:id="@+id/fragment_photos_answer3"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"/>

    </LinearLayout>
</ScrollView>

这是我的错误的日志: 尝试在空对象引用上调用虚拟方法'android.view.View android.view.View.findViewById(int)'

这是strings.xml文件:

<!-- Calorie -->
<string name="title_calorie">Calorie</string>
<string name="lbl_age">Age</string>
<string name="lbl_gender">Gender</string>
<string name="lbl_female">Female</string>
<string name="lbl_male">Male</string>
<string name="lbl_weight">Weight (in kg)</string>
<string name="lbl_height">Height (in cm)</string>
<string name="lbl_daily_activity">Daily Activity</string>
<string name="btn_calculate">Calculate</string>
<string-array name="act">
    <item>Little to No exercise</item>
    <item>Light exercise (1–3 days per week)</item>
    <item>Moderate exercise (3–5 days per week)</item>
    <item>Heavy exercise (6–7 days per week)</item>
    <item>Very Heavy exercise (twice per day, extra heavy workouts)</item>
</string-array>
穆拉特·卡拉戈斯(Murat Karagoz)

您必须先对Viewin进行充气Fragments然后才能在其中搜索ID。例如

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.yourLayoutId, container, false);
    Spinner myspinner=(Spinner) v.findViewById(R.id.fragment_photos_spinner);

    return v;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章