我在我的android應用程序中使用列表視圖。列表視圖工作正常,但是當我嘗試導航到項目單擊另一個活動時,我的應用程序崩潰並在模擬器上顯示消息意外停止您的應用程序。活動之間的導航無法正常工作
這裏是我的onItemClick
代碼:
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);
}
我的第二個活動的代碼:第二個活動的
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代碼:
<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>
我的第一次的全部代碼活動:
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();*/
}
}
可能重複!我怎樣才能解決這個問題?](http://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this) – Selvin 2014-11-25 14:37:19
你有清單中聲明CityActivity嗎? – 2014-11-25 14:38:39