我遇到了编程片段的问题。我在微调器上发现了null异常。我不知道为什么微调器是null :(我尝试evrything我使用view,getActivity()但allweys是null。我需要建议..帮助我:))
/*
import java.util.ArrayList;
import com.calculator.mycalculator.R;
import com.google.android.gms.wearable.NodeApi.GetConnectedNodesResult;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class ArticleFragment extends Fragment implements
OnItemSelectedListener, TextWatcher, OnClickListener {
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
// notri so zapisane vse enote
private ArrayList<Enota> enote;
private ArrayList<String> e;
private ArrayAdapter enoteAdapter;
// Spiner za vhodne podatke
private Spinner inputSpinner;
// Spinner za izhodne podatke
private Spinner outputSpinner;
private double inputAmount;
private boolean inputValid;
// indeksa ki povesta katera je trenutna stvar izbrana
private int unitInputIndex1;
private int unitInputIndex2;
// normalizirana mera ki je trenutno prikazana
private double inputRate1;
private double inputRate2;
// rabimo tudi ime kot so moÄŤ, kot itd..
String category;
View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
v = inflater.inflate(R.layout.article_view, container, false);
e = new ArrayList<String>();
category = "kot";
this.enote = EnoteUpravljalec
.getUnits(this.category, v.getContext(), 1);
String[] unitNames = new String[this.enote.size()];
for (int i = 0; i < this.enote.size(); i++)
unitNames[i] = enote.get(i).getLocalizedName();
enoteAdapter = new ArrayAdapter(v.getContext(),
android.R.layout.simple_spinner_item, unitNames);
enoteAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
inputSpinner = (Spinner) v.findViewById(R.id.unitInput1);
outputSpinner = (Spinner) v.findViewById(R.id.unitInput2);
try {
inputSpinner.setAdapter(enoteAdapter);
outputSpinner.setAdapter(enoteAdapter);
inputSpinner.setOnItemSelectedListener(this);
outputSpinner.setOnItemSelectedListener(this);
EditText edit_text = (EditText) v.findViewById(R.id.enotaVrednost);
edit_text.addTextChangedListener(this);
this.inputValid = false;
setConversionOutput("");
} catch (Exception ex) {
}
this.unitInputIndex1 = -1;
this.unitInputIndex2 = -1;
this.inputRate1 = -1.0;
this.inputRate2 = -1.0;
// If activity recreated (such as from screen rotate), restore
// the previous article selection set by onSaveInstanceState().
// This is primarily necessary when in the two-pane layout.
// Inflate the layout for this fragment
return v;
}
@Override
public void onStart() {
super.onStart();
// During startup, check if there are arguments passed to the fragment.
// onStart is a good place to do this because the layout has already
// been
// applied to the fragment at this point so we can safely call the
// method
// below that sets the article text.
Bundle args = getArguments();
if (args != null) {
// Set article based on argument passed in
updateArticleView(args.getInt(ARG_POSITION));
} else if (mCurrentPosition != -1) {
// Set article based on saved instance state defined during
// onCreateView
updateArticleView(mCurrentPosition);
}
}
public void updateArticleView(int position) {
mCurrentPosition = position;
switch (position) {
case 0:
category = "kot";
break;
case 1:
category = "povrsina";
break;
case 2:
category = "energija";
break;
case 3:
category = "moc";
break;
case 4:
category = "dolzina";
break;
case 5:
category = "masa";
break;
case 6:
category = "tlak";
break;
case 7:
category = "hitrost";
break;
case 8:
category = "temperatura";
break;
case 9:
category = "cas";
break;
case 10:
category = "volumen";
break;
}
e = new ArrayList<String>();
// dobime ime kategorije
// this.category = getIntent().getStringExtra("category");
// dobimo enoto
this.enote = EnoteUpravljalec.getUnits(this.category, getActivity(), 1);
String[] unitNames = new String[this.enote.size()];
for (int i = 0; i < this.enote.size(); i++)
unitNames[i] = enote.get(i).getLocalizedName();
enoteAdapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_spinner_item, unitNames);
enoteAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
inputSpinner = (Spinner) v.findViewById(R.id.unitInput1);
outputSpinner = (Spinner) v.findViewById(R.id.unitInput2);
try {
inputSpinner.setAdapter(enoteAdapter);
outputSpinner.setAdapter(enoteAdapter);
inputSpinner.setOnItemSelectedListener(this);
outputSpinner.setOnItemSelectedListener(this);
setConversionOutput("");
} catch (Exception ex) {
}
// TextView article = (TextView)
// getActivity().findViewById(R.id.article);
// article.setText(Ipsum.Articles[position]);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save the current article selection in case we need to recreate the
// fragment
outState.putInt(ARG_POSITION, mCurrentPosition);
}
private void setConversionOutput(String s) {
TextView v = (TextView) getView().findViewById(
R.id.unit_conversion_output);
v.setText(s);
}
@Override
public void afterTextChanged(Editable amount) {
if (amount.length() == 0) {
// If no text is entered, the input is invalid.
this.inputValid = false;
} else {
String text = amount.toString();
try {
this.inputAmount = Double.parseDouble(text);
this.inputValid = true;
NarediPretvorbo();
} catch (NumberFormatException e) {
this.inputValid = false;
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
private Enota getUnit(int index) {
return enote.get(index);
}
@Override
public void onClick(View v) {
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
switch (parent.getId()) {
case R.id.unitInput1:
// The "from" unit has been changed.
unitInputIndex1 = position;
inputRate1 = getUnit(position).getNormalizedValue();
break;
case R.id.unitInput2:
// The "to" unit has been changed.
unitInputIndex2 = position;
inputRate2 = getUnit(unitInputIndex2).getNormalizedValue();
break;
}
// If something is selected in both spinners, make a unit conversion.
NarediPretvorbo();
}
public void NarediPretvorbo() {
if (this.unitInputIndex1 == -1 || this.unitInputIndex2 == -1)
return;
double vred;
if (this.inputValid)
vred = this.inputAmount;
else
vred = 1.0;
String LokalnaEnota1 = getUnit(unitInputIndex1).getLocalizedName();
String LokalnaEnota2 = getUnit(unitInputIndex2).getLocalizedName();
double resultAmount = 0.0;
if (this.category.equalsIgnoreCase("temperatura")) {
if (LokalnaEnota1.equalsIgnoreCase(LokalnaEnota2)) {
resultAmount = vred;
} else if (LokalnaEnota1.equalsIgnoreCase("fahrenheit")) {
if (LokalnaEnota2.equalsIgnoreCase("celsius")) {
resultAmount = (vred - 32) * (5 / 9.0);
} else if (LokalnaEnota2.equalsIgnoreCase("kelvin")) {
resultAmount = (vred - 32) * (5 / 9.0) + 273.15;
}
} else if (LokalnaEnota1.equalsIgnoreCase("celsius")) {
if (LokalnaEnota2.equalsIgnoreCase("fahrenheit")) {
resultAmount = (vred * (9 / 5.0)) + 32;
} else if (LokalnaEnota2.equalsIgnoreCase("kelvin")) {
resultAmount = vred + 273.15;
}
} else if (LokalnaEnota1.equalsIgnoreCase("kelvin")) {
if (LokalnaEnota2.equalsIgnoreCase("fahrenheit")) {
resultAmount = ((vred - 273.15) * 1.8) + 32;
} else if (LokalnaEnota2.equalsIgnoreCase("celsius")) {
resultAmount = vred - 273.15;
}
e.add(category
+ " "
+ vred
+ " "
+ getUnit(unitInputIndex1).getLocalizedName()
.toLowerCase()
+ " "
+ resultAmount
+ " "
+ getUnit(unitInputIndex2).getLocalizedName()
.toLowerCase());
}
} else {
// Actually make a conversion!
resultAmount = vred * (this.inputRate1 / this.inputRate2);
if (vred > 1.0 && !this.category.equalsIgnoreCase("temperature"))
e.add(category
+ " iz "
+ vred
+ " "
+ getUnit(unitInputIndex1).getLocalizedName()
.toLowerCase()
+ " v "
+ resultAmount
+ " "
+ getUnit(unitInputIndex2).getLocalizedName()
.toLowerCase());
}
// Set the result of the conversion.
String result = Double.toString(resultAmount);
setConversionOutput(result);
}
public void onNothingSelected(AdapterView<?> parent) {
switch (parent.getId()) {
case R.id.unitInput1:
this.unitInputIndex1 = -1;
this.inputRate1 = -1.0;
break;
case R.id.unitInput2:
this.unitInputIndex2 = -1;
this.inputRate2 = -1.0;
break;
}
}
}
文章观点:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="24dp"
android:layout_gravity="left"
android:text="@+string/from_currency_amount_label"
android:textSize="20sp" />
<EditText
android:id="@+id/currency_converter_edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number|numberSigned|numberDecimal"
android:padding="12dp"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="@+string/from_currency_label"
android:textSize="20sp" />
<Spinner
android:id="@+id/from_currency_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="12dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="@+string/to_currency_label"
android:textSize="20sp" />
<Spinner
android:id="@+id/to_currency_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="12dp" />
<TextView
android:id="@+id/currency_conversion_output"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:paddingBottom="12dp"
android:text=""
android:textSize="25sp" />
<TextView
android:id="@+id/last_updated"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:paddingBottom="12dp"
android:text=""
android:textSize="18sp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
有例外:
08-24 17:04:02.091: E/AndroidRuntime(30141): FATAL EXCEPTION: main
08-24 17:04:02.091: E/AndroidRuntime(30141): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.calculator.mycalculator/com.Converter.myConverter.Unit.EnotaMainHendler}: android.view.InflateException: Binary XML file line #29: Error inflating class fragment
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.os.Looper.loop(Looper.java:137)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-24 17:04:02.091: E/AndroidRuntime(30141): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 17:04:02.091: E/AndroidRuntime(30141): at java.lang.reflect.Method.invoke(Method.java:511)
08-24 17:04:02.091: E/AndroidRuntime(30141): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-24 17:04:02.091: E/AndroidRuntime(30141): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-24 17:04:02.091: E/AndroidRuntime(30141): at dalvik.system.NativeStart.main(Native Method)
08-24 17:04:02.091: E/AndroidRuntime(30141): Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class fragment
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-24 17:04:02.091: E/AndroidRuntime(30141): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.app.Activity.setContentView(Activity.java:1881)
08-24 17:04:02.091: E/AndroidRuntime(30141): at com.Converter.myConverter.Unit.EnotaMainHendler.onCreate(EnotaMainHendler.java:21)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.app.Activity.performCreate(Activity.java:5104)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-24 17:04:02.091: E/AndroidRuntime(30141): ... 11 more
08-24 17:04:02.091: E/AndroidRuntime(30141): Caused by: java.lang.NullPointerException
08-24 17:04:02.091: E/AndroidRuntime(30141): at com.Converter.myConverter.Unit.ArticleFragment.onCreateView(ArticleFragment.java:95)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1099)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1201)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:292)
08-24 17:04:02.091: E/AndroidRuntime(30141): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
08-24 17:04:02.091: E/AndroidRuntime(30141): ... 21 more
答案 0 :(得分:0)
您提供的xml中不存在标识为unitInput1
的微调器。替换你的这些行 -
inputSpinner = (Spinner) v.findViewById(R.id.unitInput1);
outputSpinner = (Spinner) v.findViewById(R.id.unitInput2);
这些行 -
inputSpinner = (Spinner) v.findViewById(R.id.from_currency_spinner);
outputSpinner = (Spinner) v.findViewById(R.id.to_currency_spinner);