クラスの下で、リスト内の行をクリックしてアクティビティを起動しようとしています....しかし、行をクリックするとエミュレータが閉じます
- employee1 は、リストビューの行をクリックして起動しようとしている新しいアクティビティです
- 以下のクラスMainActivityでどのような変更を加える必要がありますか
public class MainActivity extends Activity {
private static String url="https://dl.dropboxusercontent.com/s/rhk01nqlyj5gixl/jsonparsing.txt?token_hash=AAHpfauVxJaC9Rkx_5abNtJnPFG04os7TZky1AhOuC5jEw";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create a JSON parser Instance ----- Used JSON parser from Android
JSONParser jParser=new JSONParser();
//Getting JSON string from URL ------ Used JSON Array from Android
JSONArray json=jParser.getJSONFromUrl(url);
List<Item> yourData = new ArrayList<Item>();
try {
for(int i=0;i<json.length();i++)
{
JSONObject c=json.getJSONObject(i);// Used JSON Object from Android
//Storing each Json in a string variable
int AGE=c.getInt("age");
String NAME=c.getString("name");
String CITY=c.getString("city");
String GENDER=c.getString("Gender");
String BIRTHDATE=c.getString("birthdate");
yourData.add(new Item(AGE, NAME, CITY, GENDER, BIRTHDATE));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListView yourListView = (ListView) findViewById(R.id.listViewID);
ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, yourData);
yourListView.setAdapter(customAdapter);
yourListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
if(position == 1)
{
//code specific to first list item
Intent myIntent = new Intent(MainActivity.this,Employee1.class);
startActivityForResult(myIntent, 0);
}
}
});
}
}
以下のように、マニフェストにも新しいアクティビティについて追加しました
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testjson.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".employee1"
android:label="@string/app_name" >
</activity>
</application>
エラーログ::
08-04 13:05:37.059: E/AndroidRuntime(719): FATAL EXCEPTION: main
08-04 13:05:37.059: E/AndroidRuntime(719): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.testjson/com.example.testjson.Employee1}; have you declared this activity in your AndroidManifest.xml?
08-04 13:05:37.059: E/AndroidRuntime(719): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
08-04 13:05:37.059: E/AndroidRuntime(719): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
08-04 13:05:37.059: E/AndroidRuntime(719): at android.app.Activity.startActivityForResult(Activity.java:2827)
08-04 13:05:37.059: E/AndroidRuntime(719): at com.example.testjson.MainActivity$1.onItemClick(MainActivity.java:69)
08-04 13:05:37.059: E/AndroidRuntime(719): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
08-04 13:05:37.059: E/AndroidRuntime(719): at android.widget.ListView.performItemClick(ListView.java:3513)
08-04 13:05:37.059: E/AndroidRuntime(719): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
08-04 13:05:37.059: E/AndroidRuntime(719): at android.os.Handler.handleCallback(Handler.java:587)
08-04 13:05:37.059: E/AndroidRuntime(719): at android.os.Handler.dispatchMessage(Handler.java:92)
08-04 13:05:37.059: E/AndroidRuntime(719): at android.os.Looper.loop(Looper.java:123)
08-04 13:05:37.059: E/AndroidRuntime(719): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-04 13:05:37.059: E/AndroidRuntime(719): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 13:05:37.059: E/AndroidRuntime(719): at java.lang.reflect.Method.invoke(Method.java:507)
08-04 13:05:37.059: E/AndroidRuntime(719): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-04 13:05:37.059: E/AndroidRuntime(719): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-04 13:05:37.059: E/AndroidRuntime(719): at dalvik.system.NativeStart.main(Native Method)