J'essaie de créer une application de liste d'achat basée sur les champs de saisie, ArrayList
et ListView
. L'application sera basée sur Fragments
. Cependant, j'ai rencontré un problème et je ne sais pas comment le résoudre.
J'ai regardé autour de moi à la fois sur Google et StackOverflow et j'ai trouvé des informations. Cependant, je ne l'ai pas obtenu au travail. Maintenant, j'espère pouvoir obtenir de l'aide pour mon code.
Je suis relativement inexpérimenté en développement Android.
Merci d'avance
Main.Java
import Java.util.ArrayList;
import Android.app.Fragment;
import Android.app.FragmentManager;
import Android.app.FragmentTransaction;
import Android.os.Bundle;
import Android.support.v4.app.FragmentActivity;
import Android.view.Menu;
import Android.view.MenuItem;
public class Main extends FragmentActivity {
ArrayList<Detail> items = new ArrayList<Detail>();
FragmentManager FM;
FragmentTransaction FT;
Input input = new Input();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
replaceFragment(R.id.container, input);
}
public void replaceFragment(int container, Fragment fragment){
FM = getFragmentManager();
FT = FM.beginTransaction();
FT.replace(container, fragment);
FT.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ListAdapter.Java
public class ListAdapter extends BaseAdapter {
Context context;
ArrayList<Detail> items;
public ListAdapter(Context context, ArrayList<Detail> items) {
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return items.indexOf(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater lInflater = (LayoutInflater)context.getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
convertView = lInflater.inflate(R.layout.list_row, null);
}
TextView title = (TextView)convertView.findViewById(R.id.itemTitle);
CheckBox done = (CheckBox)convertView.findViewById(R.id.itemDone);
Detail detail_position = items.get(position);
title.setText(detail_position.getTitle());
done.setChecked(detail_position.isDone());
return null;
}
}
List.Java
import Java.util.ArrayList;
import Android.app.ListFragment;
import Android.os.Bundle;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;
import Android.widget.AdapterView;
import Android.widget.AdapterView.OnItemClickListener;
public class List extends ListFragment implements OnItemClickListener{
ArrayList<Detail> items;
ListAdapter adapter;
private onClickListener OCL;
interface onClickListener{
public void onItemClick(int id);
}
public List(ArrayList<Detail> items) {
this.items = items;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.list_layout, container, false);
return root;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(items != null){
adapter = new ListAdapter(getActivity(), items);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if(OCL != null){
OCL.onItemClick(position);
}
}
}
list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:id="@+id/LinearLayout1"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical" >
<ListView
Android:id="@+id/Android:list"
Android:layout_width="match_parent"
Android:layout_height="match_parent" >
</ListView>
</LinearLayout>
list_row
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="horizontal"
Android:padding="10dp" >
<TextView
Android:id="@+id/itemTitle"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_weight="0.96"
Android:text="Medium Text"
Android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
Android:id="@+id/itemDone"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="Done" />
</LinearLayout>
activity_main.xml
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:id="@+id/container"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:paddingBottom="@dimen/activity_vertical_margin"
Android:paddingLeft="@dimen/activity_horizontal_margin"
Android:paddingRight="@dimen/activity_horizontal_margin"
Android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.com.hermods.lab2_2.Main" >
</RelativeLayout>
Journal
02-19 09:10:47.421: E/AndroidRuntime(12745): Process: com.example.com.hermods.lab2_2, PID: 12745
02-19 09:10:47.421: E/AndroidRuntime(12745): Java.lang.NullPointerException: Attempt to invoke virtual method 'int Android.view.View.getImportantForAccessibility()' on a null object reference
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.AbsListView.obtainView(AbsListView.Java:2360)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.ListView.makeAndAddView(ListView.Java:1864)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.ListView.fillDown(ListView.Java:698)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.ListView.fillFromTop(ListView.Java:759)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.ListView.layoutChildren(ListView.Java:1673)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.AbsListView.onLayout(AbsListView.Java:2148)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.View.layout(View.Java:15596)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.ViewGroup.layout(ViewGroup.Java:4966)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.LinearLayout.setChildFrame(LinearLayout.Java:1703)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.LinearLayout.layoutVertical(LinearLayout.Java:1557)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.LinearLayout.onLayout(LinearLayout.Java:1466)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.View.layout(View.Java:15596)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.ViewGroup.layout(ViewGroup.Java:4966)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.RelativeLayout.onLayout(RelativeLayout.Java:1076)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.View.layout(View.Java:15596)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.ViewGroup.layout(ViewGroup.Java:4966)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.FrameLayout.layoutChildren(FrameLayout.Java:573)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.FrameLayout.onLayout(FrameLayout.Java:508)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.View.layout(View.Java:15596)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.ViewGroup.layout(ViewGroup.Java:4966)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.LinearLayout.setChildFrame(LinearLayout.Java:1703)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.LinearLayout.layoutVertical(LinearLayout.Java:1557)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.LinearLayout.onLayout(LinearLayout.Java:1466)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.View.layout(View.Java:15596)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.ViewGroup.layout(ViewGroup.Java:4966)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.FrameLayout.layoutChildren(FrameLayout.Java:573)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.widget.FrameLayout.onLayout(FrameLayout.Java:508)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.View.layout(View.Java:15596)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.ViewGroup.layout(ViewGroup.Java:4966)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.ViewRootImpl.performLayout(ViewRootImpl.Java:2072)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.ViewRootImpl.performTraversals(ViewRootImpl.Java:1829)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.ViewRootImpl.doTraversal(ViewRootImpl.Java:1054)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.Java:5779)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.Choreographer$CallbackRecord.run(Choreographer.Java:767)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.Choreographer.doCallbacks(Choreographer.Java:580)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.Choreographer.doFrame(Choreographer.Java:550)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.Java:753)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.os.Handler.handleCallback(Handler.Java:739)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.os.Handler.dispatchMessage(Handler.Java:95)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.os.Looper.loop(Looper.Java:135)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Android.app.ActivityThread.main(ActivityThread.Java:5221)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Java.lang.reflect.Method.invoke(Native Method)
02-19 09:10:47.421: E/AndroidRuntime(12745): at Java.lang.reflect.Method.invoke(Method.Java:372)
02-19 09:10:47.421: E/AndroidRuntime(12745): at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:899)
02-19 09:10:47.421: E/AndroidRuntime(12745): at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
Dans votre méthode public View getView
, changez return null;
en return convertView;
.
#use return convertView;
Code:
public View getView(final int position, View convertView, ViewGroup parent) {
//convertView = null;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item, null);
TextView tv = (TextView) convertView.findViewById(R.id.name);
Button rm_btn = (Button) convertView.findViewById(R.id.rm_btn);
Model m = modelList.get(position);
tv.setText(m.getName());
// click listener for remove button คลิกลบปุ่ม
rm_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
modelList.remove(position);
notifyDataSetChanged();
}
});
}
///#use return convertView;
return convertView;
}
dans votre baseadapter class constructeur essayez d'initialiser LayoutInflater , normalement, j'ai préféré de cette façon,
public ClassBaseAdapter(Context context,ArrayList<Integer> listLoanAmount) {
this.context = context;
this.listLoanAmount = listLoanAmount;
this.layoutInflater = LayoutInflater.from(context);
}
en haut de la classe, créez LayoutInflater variable, espérons que cela vous aidera
cela se produit parfois lorsque nous utilisons un adaptateur personnalisé dans toute activité de fragment. et nous retournons l'objet null, c'est-à-dire la vue null, de sorte que l'activité soit confondue avec la vue à charger.