我喜欢用2个两个微调器和2个文本输入和textview来编程一个8个listchoose片段但是我得到了膨胀异常我从android教程中做到了
这是主要课程:
import com.calculator.mycalculator.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.ListFragment;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class EnotaMainHendler extends FragmentActivity
implements HeadlinesFragment.OnHeadlineSelectedListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create an instance of ExampleFragment
HeadlinesFragment firstFragment = new HeadlinesFragment();
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Capture the article fragment from the activity layout
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// If the frag is not available, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
}
这是片段类:
package com.Converter.myConverter.Unit;
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;
/// Context context;
/*
String[] values = new String[] { getString(R.string.angleP),
getString(R.string.surface),
getString(R.string.energy),
getString(R.string.angleP),
getString(R.string.lenght),
getString(R.string.mass),
getString(R.string.pressure),
getString(R.string.speed),
getString(R.string.temperature),
getString(R.string.time),
getString(R.string.volume),
};
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
View view=inflater.inflate(R.layout.article_view, container, false);
e=new ArrayList<String>();
//dobime ime kategorije
// this.category = getIntent().getStringExtra("category");
// dobimo enoto
this.enote = EnoteUpravljalec.getUnits(this.category, getView().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(getView().getContext(), android.R.layout.simple_spinner_item, unitNames);
enoteAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
inputSpinner = (Spinner) getView().findViewById(R.id.unitInput1);
outputSpinner = (Spinner) getView().findViewById(R.id.unitInput2);
inputSpinner.setAdapter(enoteAdapter);
outputSpinner.setAdapter(enoteAdapter);
inputSpinner.setOnItemSelectedListener(this);
outputSpinner.setOnItemSelectedListener(this);
EditText edit_text = (EditText)getView().findViewById(R.id.enotaVrednost);
edit_text.addTextChangedListener(this);
this.inputValid = false;
setConversionOutput("");
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 view;
}
@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) {
// TextView article = (TextView) getActivity().findViewById(R.id.article);
// article.setText(Ipsum.Articles[position]);
mCurrentPosition = 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;
}
}
/*
@Override
protected void onPause() {
super.onPause(); //To change body of overridden methods use File | Settings | File Templates.
SaveAllObjects();
}
@Override
protected void onStop() {
super.onStop(); //To change body of overridden methods use File | Settings | File Templates.
// SaveAllObjects();
}
//Shranimo podatke v paint4fun file in notri v file txt o zadnji uprabi
public void SaveAllObjects() {
try {
FileOutputStream fos = openFileOutput("sez.txt", MODE_APPEND);
BufferedOutputStream buf = new BufferedOutputStream(fos);
OutputStreamWriter osw = new OutputStreamWriter(buf);
BufferedWriter writer = new BufferedWriter(osw);
writer.newLine();
if(e.size()>0)
for (String line :e) //preberemo vse objekte string po string
writer.write(line+"\n");
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
*/
}
这是listfragment类:
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
/* String[] values = new String[] { getString(R.string.angleP),
getString(R.string.surface),
getString(R.string.energy),
getString(R.string.angleP),
getString(R.string.lenght),
getString(R.string.mass),
getString(R.string.pressure),
getString(R.string.speed),
getString(R.string.temperature),
getString(R.string.time),
getString(R.string.volume),
};
*/
// The container Activity must implement this interface so the frag can deliver messages
public interface OnHeadlineSelectedListener {
/** Called by HeadlinesFragment when a list item is selected */
public void onArticleSelected(int position);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We need to use a different list item layout for devices older than Honeycomb
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
// Create an array adapter for the list view, using the Ipsum headlines array
setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
}
@Override
public void onStart() {
super.onStart();
// When in two-pane layout, set the listview to highlight the selected list item
// (We do this during onStart because at the point the listview is available.)
if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception.
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Notify the parent activity of selected item
mCallback.onArticleSelected(position);
// Set the item as checked to be highlighted when in two-pane layout
getListView().setItemChecked(position, true);
}
}
并且有文章视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="24dp"
android:text="@+string/from_currency_amount_label"
android:textSize="20sp"
android:layout_gravity="left"/>
<EditText
android:id="@+id/currency_converter_edit_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:padding="12dp"
android:inputType="number|numberSigned|numberDecimal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+string/from_currency_label"
android:textSize="20sp"
android:layout_gravity="left"
/>
<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:text="@+string/to_currency_label"
android:textSize="20sp"
android:layout_gravity="left"
/>
<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:text=""
android:textSize="25sp"
android:padding="12dp"
android:paddingBottom="12dp"/>
<TextView
android:id="@+id/last_updated"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
android:padding="12dp"
android:paddingBottom="12dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
和新闻报道
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
和新篇文章:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.Converter.myConverter.Unit.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.Converter.myConverter.Unit.ArticleFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
但是我得到了例外我很伤心:
08-24 10:13:24.891: E/AndroidRuntime(22932): FATAL EXCEPTION: main
08-24 10:13:24.891: E/AndroidRuntime(22932): 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 10:13:24.891: E/AndroidRuntime(22932): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.os.Looper.loop(Looper.java:137)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-24 10:13:24.891: E/AndroidRuntime(22932): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 10:13:24.891: E/AndroidRuntime(22932): at java.lang.reflect.Method.invoke(Method.java:511)
08-24 10:13:24.891: E/AndroidRuntime(22932): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-24 10:13:24.891: E/AndroidRuntime(22932): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-24 10:13:24.891: E/AndroidRuntime(22932): at dalvik.system.NativeStart.main(Native Method)
08-24 10:13:24.891: E/AndroidRuntime(22932): Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class fragment
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-24 10:13:24.891: E/AndroidRuntime(22932): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.app.Activity.setContentView(Activity.java:1881)
08-24 10:13:24.891: E/AndroidRuntime(22932): at com.Converter.myConverter.Unit.EnotaMainHendler.onCreate(EnotaMainHendler.java:38)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.app.Activity.performCreate(Activity.java:5104)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-24 10:13:24.891: E/AndroidRuntime(22932): ... 11 more
08-24 10:13:24.891: E/AndroidRuntime(22932): Caused by: java.lang.NullPointerException
08-24 10:13:24.891: E/AndroidRuntime(22932): at com.Converter.myConverter.Unit.ArticleFragment.onCreateView(ArticleFragment.java:105)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1099)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1201)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:292)
08-24 10:13:24.891: E/AndroidRuntime(22932): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
08-24 10:13:24.891: E/AndroidRuntime(22932): ... 21 more
答案 0 :(得分:0)
从EnotaMainHendler
更改您的导入:
import android.app.Fragment;
import android.app.ListFragment;
为:
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
此外,您可能希望使用class
属性而不是name
:
<fragment android:class="com.Converter.myConverter.Unit.HeadlinesFragment"
android:id="@+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:class="com.Converter.myConverter.Unit.ArticleFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
答案 1 :(得分:0)
this.enote = EnoteUpravljalec.getUnits(this.category, getView().getContext(), 1);
getView()
可能会在这里返回null
。
此外,手动调用getVew()
是不正确的。相反,您应该使用getActivity()
:
this.enote = EnoteUpravljalec.getUnits(this.category, getActivity(), 1);