我不知道为什么我得到空指针异常,我的代码似乎很简单,但不知道为什么,这里是我的代码,
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_macro_requirement);
text_gaol=(TextView)findViewById(R.id.macro_goal_calories_calculated);
txtview_percent_macro_protein=(TextView)findViewById(R.id.macro_protein_percentage_calculated);
txtview_percent_maccro_fats=(TextView)findViewById(R.id.macro_fat_percentage_calculated);
txtview_percent_maccro_carbs=(TextView)findViewById(R.id.macro_carb_percentage_calculated);
txt_macro_protein_percentage_manual=(TextView)findViewById(R.id.macro_protein_percentage_manual);
txt_macro_fats_percentage_manual=(TextView)findViewById(R.id.macro_fat_percentage_manual);
txt_macro_carbs_percentage_manual=(TextView)findViewById(R.id.macro_carb_percentage_manual);
Spinner goal_spinner = (Spinner)findViewById(R.id.macro_goal_spinner);
List<String> setactivity = new ArrayList<String>();
setactivity.add("Choose your Goal");
setactivity.add("Gain Weight");
setactivity.add("Loss Weight");
setactivity.add("Maintain Weight");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, setactivity);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
goal_spinner.setAdapter(dataAdapter);
在上面的行中获取NullPointerException。
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/macro"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:padding="5.0dip" >
<View
android:id="@id/view1"
android:layout_width="fill_parent"
android:layout_height="2.0dip"
android:layout_alignTop="@id/text_current_playback_title"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="@id/textView"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center_horizontal"
android:text="@string/label_tdee"
android:textAppearance="?android:textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="@+id/txt_result"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center_horizontal"
android:text="0"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@color/result_color" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text=" "
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="@id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/label_goal"
android:textAppearance="?android:textAppearanceMedium" />
<Spinner
android:id="@id/macro_goal_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:id="@id/macro_goal_section_calculated"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:padding="5.0dip" >
<TextView
android:id="@id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/label_goal_calories"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="@id/macro_goal_calories_calculated"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="0"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="@color/result_color" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:padding="5.0dip" >
<Button
android:id="@+id/btn_nutrtional_plan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#635fd0"
android:padding="5.0dip"
android:text="@string/label_choose_nutrition_plan"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white" />
<View
android:id="@id/view1"
android:layout_width="fill_parent"
android:layout_height="2.0dip"
android:layout_alignTop="@id/text_current_playback_title"
android:background="@android:color/darker_gray" />
</LinearLayout>
<Button
android:id="@+id/btn_back_profile"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#635fd0"
android:padding="5.0dip"
android:text="Back To Profile"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white" />
</LinearLayout>
</ScrollView>
一切看起来都不错......
答案 0 :(得分:0)
删除setactivity并在那里添加你的arraylist ,
out
到
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, setactivity);
YOURARRAYLISTOBJECT是您在微调器中获取数据的地方。