Salut dans mon application Je suis analyser l'URL json pour afficher l'image et le texte que cela fonctionne très bien.
Après que j'ajoute un bouton nommé comme ajouter à Listview
il affichage dans tous les endroits correctement. Mais après le si je clique sur le bouton Ajouter je veux passer à une autre activité. Après avoir cliqué sur le bouton Ajouter, il affiche malheureusement une erreur.ajouter le bouton à listview dans android
Quelqu'un peut-il s'il vous plaît me dire ce que j'ai fait erreur dans mon code?
classe ListViewAdapter
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context, ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView title;
ImageView thumb_url;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
title = (TextView) itemView.findViewById(R.id.rank);
// Locate the ImageView in listview_item.xml
thumb_url = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set results to the TextViews
title.setText(resultp.get(MainActivity.TITLE));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(MainActivity.THUMB_URL), thumb_url);
// Capture ListView item click
Button Add = (Button) itemView.findViewById(R.id.add);
Add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data rank
intent.putExtra("title", resultp.get(MainActivity.TITLE));
// Pass all data country
/*intent.putExtra("country", resultp.get(MainActivity.COUNTRY));
// Pass all data population
intent.putExtra("population",resultp.get(MainActivity.POPULATION));
*/// Pass all data flag
intent.putExtra("thumb_url", resultp.get(MainActivity.THUMB_URL));
// Start SingleItemView Class
context.startActivity(intent);
}
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
return itemView;
}
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
classe SingleItemView
public class SingleItemView extends Activity {
// Declare Variables
String title;
/*String country;
String population;
*/
String thumb_url;
ImageLoader imageLoader = new ImageLoader(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from singleitemview.xml
setContentView(R.layout.singleitemview);
Intent i = getIntent();
// Get the result of rank
title = i.getStringExtra("title");
// Get the result of country
/*country = i.getStringExtra("country");
// Get the result of population
population = i.getStringExtra("population");*/
// Get the result of flag
thumb_url = i.getStringExtra("thumb_url");
// Locate the TextViews in singleitemview.xml
TextView txtrank = (TextView) findViewById(R.id.rank);
/*TextView txtcountry = (TextView) findViewById(R.id.country);
TextView txtpopulation = (TextView) findViewById(R.id.population);
*/
// Locate the ImageView in singleitemview.xml
ImageView imgflag = (ImageView) findViewById(R.id.flag);
// Set results to the TextViews
txtrank.setText(title);
/*txtcountry.setText(country);
txtpopulation.setText(population);
*/
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(thumb_url, imgflag);
}
}
classe MainActivity
public class MainActivity extends Activity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String TITLE = "title";
/*static String COUNTRY = "country";
static String POPULATION = "population";*/
static String THUMB_URL = "thumb_url";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Loading...");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunction
.getJSONfromURL("http://indianpoliticalleadersmap.com/android/DemoSchool/json/json_item.php");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("veg_food");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("title", jsonobject.getString("title"));
/*map.put("country", jsonobject.getString("country"));
map.put("population", jsonobject.getString("population"));*/
map.put("thumb_url", jsonobject.getString("thumb_url"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
}
singleitemview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/flag"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="#444"
android:padding="3dp" />
<TextView
android:id="@+id/rank"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_toRightOf="@+id/flag"
android:maxLines="1"
android:textSize="15dip"
android:textStyle="bold" />
<!-- <TextView
android:id="@+id/ranklabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
-->
<!-- <ImageView
android:id="@+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#000000"
android:layout_toRightOf="@+id/flag"
android:padding="1dp" />
-->
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/rank"
android:text="Add" />
</RelativeLayout>
listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/flag"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="#444"
android:padding="3dp" />
<TextView
android:id="@+id/rank"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_toRightOf="@+id/flag"
android:maxLines="1"
android:textSize="15dip"
android:textStyle="bold" />
<!-- <TextView
android:id="@+id/ranklabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
-->
<!-- <ImageView
android:id="@+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#000000"
android:layout_toRightOf="@+id/flag"
android:padding="1dp" />
-->
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/add"
android:text="Add" />
</RelativeLayout>
logcat
06-17 18:22:10.146: E/AndroidRuntime(498): FATAL EXCEPTION: main
06-17 18:22:10.146: E/AndroidRuntime(498): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.androidhive/com.example.androidhive.SingleItemView}: java.lang.NullPointerException
06-17 18:22:10.146: E/AndroidRuntime(498): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
06-17 18:22:10.146: E/AndroidRuntime(498): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-17 18:22:10.146: E/AndroidRuntime(498): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-17 18:22:10.146: E/AndroidRuntime(498): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-17 18:22:10.146: E/AndroidRuntime(498): at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 18:22:10.146: E/AndroidRuntime(498): at android.os.Looper.loop(Looper.java:123)
06-17 18:22:10.146: E/AndroidRuntime(498): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-17 18:22:10.146: E/AndroidRuntime(498): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 18:22:10.146: E/AndroidRuntime(498): at java.lang.reflect.Method.invoke(Method.java:521)
06-17 18:22:10.146: E/AndroidRuntime(498): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-17 18:22:10.146: E/AndroidRuntime(498): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-17 18:22:10.146: E/AndroidRuntime(498): at dalvik.system.NativeStart.main(Native Method)
06-17 18:22:10.146: E/AndroidRuntime(498): Caused by: java.lang.NullPointerException
06-17 18:22:10.146: E/AndroidRuntime(498): at android.content.ContextWrapper.getCacheDir(ContextWrapper.java:188)
06-17 18:22:10.146: E/AndroidRuntime(498): at com.example.androidhive.FileCache.<init>(FileCache.java:15)
06-17 18:22:10.146: E/AndroidRuntime(498): at com.example.androidhive.ImageLoader.<init>(ImageLoader.java:32)
06-17 18:22:10.146: E/AndroidRuntime(498): at com.example.androidhive.SingleItemView.<init>(SingleItemView.java:19)
06-17 18:22:10.146: E/AndroidRuntime(498): at java.lang.Class.newInstanceImpl(Native Method)
06-17 18:22:10.146: E/AndroidRuntime(498): at java.lang.Class.newInstance(Class.java:1429)
06-17 18:22:10.146: E/AndroidRuntime(498): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-17 18:22:10.146: E/AndroidRuntime(498): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
06-17 18:22:10.146: E/AndroidRuntime(498): ... 11 more
Dans mon code ci-dessus si je clique sur le bouton Ajouter ne fonctionne pas tout le monde peut s'il vous plaît, résolvez-le et aidez-moi. Merci d'avance.
poster votre logcat –
quel type de paramètres vous avez mis à l'intention? –
salut je posté mon logcat pouvez-vous s'il vous plaît me aider – user3739863