내 안드로이드 앱에서 목록보기를 사용하고 있습니다. 목록보기가 정상적으로 작동하지만 항목을 클릭 할 때 다른 활동으로 이동하려고하면 내 앱이 충돌하고 앱이 예기치 않게 중지되었다는 에뮬레이터의 메시지가 표시됩니다. 여기 활동 간 이동이 작동하지 않습니다.
내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>
제 1의 전체 코드 활동 :
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/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Selvin
매니페스트에 CityActivity를 선언하셨습니까? –