首先,我是Android编程的新手,所以如果这是一些noob错误,我真的很抱歉。我实现了与活动相同的文件,但它确实有效。但是当我把它放入一个片段时,它却没有。我无法得到关于片段的体面教程,所以我实现的主要来自理论和示例项目。错误日志说,“java.lang.NullPointerException:尝试调用虚方法'android.view.View关于空对象引用的android.view.View.findViewById(int)'。
我检查了stackoverflow的相同内容并找到了总是关于错误ID的答案。但在我的情况下,我认为id正确指出。如果有人可以帮助我,那将是很棒的。哦,如果可以的话,请告诉我是否在相应的函数中编写了代码。只是为了澄清,函数 public void equalClicked(View v)是单击按钮时应该发生的计算部分。我已经做了差不多两天了。 -_-
这是我的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>
这是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>
答案 0 :(得分:1)
您需要在View
中对Fragments
进行充气,然后才能搜索其中的ID。 e.g。
@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;
}