I am using list view in my android app. list view is working fine, but when I try to navigate to another activity on item click, my app crashes and shows message on emulator that your app has stopped unexpectedly.
Here is my onItemClick
code :
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Intent i=new Intent(MainActivity.this,CityActivity.class);
i.putExtra("me","hello");
startActivity(i);
}
The code of my second activity:
public class CityActivity extends Activity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cityname);
text= (TextView)findViewById(R.id.textView1);
Intent intent = getIntent();
String message=intent.getStringExtra("me");
text.setText(message);
}
@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;
}
}
XML code of second activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
The full code of my 1st activity:
public class MainActivity extends Activity implements OnItemClickListener {
ListView listview;
List<Country_Name> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView)findViewById(R.id.list);
JSONObject main_obj=getjson();
items=new ArrayList<Country_Name>();
try {
JSONArray array=main_obj.getJSONArray("Country");
for(int i=0;i<array.length();i++) {
JSONObject obj=array.getJSONObject(i);
String name=obj.getString("Name");
String image_url=obj.getString("Image");
Country_Name country=new Country_Name(name,image_url);
JSONArray array_city=obj.getJSONArray("City");
for(int j=0;j<array_city.length();j++)
{
JSONObject obj2=array_city.getJSONObject(i);
country.items.add(new City_Name(obj2.getString("CName"),obj2.getString("CImage")));
}
this.items.add(country);
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
CustomeBaseAdapter adapter=new CustomeBaseAdapter(this,this.items);
this.listview.setAdapter(adapter);
listview.setOnItemClickListener(this);
}
@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;
}
public JSONObject getjson() {
String json="";
StringBuilder builder=new StringBuilder();
JSONObject obj=null;
try {
BufferedReader reader=new BufferedReader(new InputStreamReader(getAssets().open("Country.txt")));
String line="";
while((line=reader.readLine())!=null)
{
builder.append(line);
}
json=builder.toString();
obj=new JSONObject(json);
}
catch(Exception ex) {
ex.toString();
}
return obj;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Intent i=new Intent(MainActivity.this,CityActivity.class);
i.putExtra("me","hello");
startActivity(i);
/*Toast toast = Toast.makeText(getApplicationContext(),"hello",
Toast.LENGTH_SHORT);
toast.show();*/
}
}