私はエクリプスを使用しています。私のアプリケーションは、Android のリスト ビューから値を選択し、その値を別のアクティビティに表示する必要がありますが、それは起こっていません。
リストをクリックするとすぐに、エラーアクティビティが機能しなくなりました
すべてのアプリケーションでこのようなエラーが発生します
このチュートリアルに基づいて別のアプリケーションを作成しました
http://www.vogella.com/articles/AndroidSQLite/article.html
私のコードは 2 つのアクティビティで構成されています。
MainActivity.java
package event.scheduler;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.app.ListActivity;
import android.content.Intent;
import android.widget.ArrayAdapter;
//import android.widget.Toast;
import android.widget.ListView;
public class MainActivity extends ListActivity {
public final static String EXTRA_MESSAGE = "event.scheduler.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
try
{
super.onCreate(savedInstanceState);
String[] values = new String[]{"Conatus","Horizon","Orja","Phoniex"};
ArrayAdapter <String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values);
setListAdapter(adapter);
}
catch(RuntimeException e){
Log.i("sdv","dsvg");
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
try
{
String item = (String)getListAdapter().getItem(position);
Intent intent = new Intent(this,Display.class);
intent.putExtra(EXTRA_MESSAGE,item);
startActivity(intent);
}
catch(RuntimeException e)
{
Log.i("log","view");
}
} // Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
Display.java
package event.scheduler;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
public class Display extends Activity {
TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
//try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
textview= (TextView)findViewById(R.id.your_texbox);
textview.setTextSize(40);
textview.setText(message);
setContentView(textview);
}
//catch(RuntimeException e)
{
// Log.i("sfs", "message");
}
}
}
私のxmlファイルは
activity_display.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Display" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
私のマニフェストファイルは
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="event.scheduler"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="event.scheduler.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="event.scheduler.Display"
android:label="@string/title_activity_display" >
</activity>
</application>
私のlogcatは
01-20 14:49:01.171: D/AndroidRuntime(1278): Shutting down VM
01-20 14:49:01.181: W/dalvikvm(1278): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-20 14:49:01.301: E/AndroidRuntime(1278): FATAL EXCEPTION: main
01-20 14:49:01.301: E/AndroidRuntime(1278): java.lang.RuntimeException: Unable to start activity ComponentInfo{event.scheduler/event.scheduler.Display}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.os.Looper.loop(Looper.java:137)
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-20 14:49:01.301: E/AndroidRuntime(1278): at java.lang.reflect.Method.invokeNative(Native Method)
01-20 14:49:01.301: E/AndroidRuntime(1278): at java.lang.reflect.Method.invoke(Method.java:511)
01-20 14:49:01.301: E/AndroidRuntime(1278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-20 14:49:01.301: E/AndroidRuntime(1278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-20 14:49:01.301: E/AndroidRuntime(1278): at dalvik.system.NativeStart.main(Native Method)
01-20 14:49:01.301: E/AndroidRuntime(1278): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.view.ViewGroup.addViewInner(ViewGroup.java:3339)
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.view.ViewGroup.addView(ViewGroup.java:3210)
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.view.ViewGroup.addView(ViewGroup.java:3186)
01-20 14:49:01.301: E/AndroidRuntime(1278): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:289)
01-20 14:49:01.301: E/AndroidRuntime(1278): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:279)
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.app.Activity.setContentView(Activity.java:1901)
01-20 14:49:01.301: E/AndroidRuntime(1278): at event.scheduler.Display.onCreate(Display.java:26)
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.app.Activity.performCreate(Activity.java:5104)
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-20 14:49:01.301: E/AndroidRuntime(1278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-20 14:49:01.301: E/AndroidRuntime(1278): ... 11 more