EditText、Button、および ListView を使用してレイアウトを拡張する My SherlockActivity。
<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=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1" >
</ListView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Add"
tools:ignore="HardcodedText" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/button1"
android:ems="10"
android:hint="Click add!"
android:inputType="text"
tools:ignore="HardcodedText" >
</EditText>
私の MainActivity は、次のメニュー レイアウト ファイルを拡張する BaseActivity を拡張します。
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/menu_settings"/>
<item
android:id="@+id/add_item"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="@string/add"/>
<item
android:id="@+id/calendar"
android:orderInCategory="2"
android:showAsAction="ifRoom"
android:title="@string/show_cal"/>
</menu>
SherlockActivity を拡張する BaseActivity を作成します。
package com.courseorganizer;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
public class BaseActivity extends SherlockActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MainActivity は、私が問題を抱えている場所です。BaseActivity を拡張し、onOptionsItemSelected() を設定して、[追加] タブが選択された場合に入力フィールドが呼び出されるようにします。次に、OnClickListener をボタンに設定して、ArrayAdapter に配置された ArrayList に取り込まれた ListView にユーザーの入力を追加します。
MainActivity は次のとおりです。
package com.courseorganizer;
import java.util.ArrayList;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import com.actionbarsherlock.view.MenuItem;
public class MainActivity extends BaseActivity {
private final String TAG = "Main Activity";
// LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems = new ArrayList<String>();
// DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItems);
Button addItem = (Button)findViewById(R.id.button1);
EditText input = (EditText)findViewById(R.id.editText1);
ListView courseList = (ListView)findViewById(R.id.listView1);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addItem.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
listItems.add(input.getText().toString());
courseList.setAdapter(adapter);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_item:
input.requestFocus();
Log.i(TAG, "Add Clicked");
return true;
case R.id.calendar:
Log.i(TAG, "Calendar Clicked");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
私はあなたの助けに感謝します!
編集:これが私のlogcat出力です:
11-04 19:16:24.517: D/AndroidRuntime(305): Shutting down VM
11-04 19:16:24.517: W/dalvikvm(305): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-04 19:16:24.569: E/AndroidRuntime(305): FATAL EXCEPTION: main
11-04 19:16:24.569: E/AndroidRuntime(305): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.courseorganizer/com.courseorganizer.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
11-04 19:16:24.569: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
11-04 19:16:24.569: E/AndroidRuntime(305): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-04 19:16:24.569: E/AndroidRuntime(305): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-04 19:16:24.569: E/AndroidRuntime(305): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-04 19:16:24.569: E/AndroidRuntime(305): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 19:16:24.569: E/AndroidRuntime(305): at android.os.Looper.loop(Looper.java:123)
11-04 19:16:24.569: E/AndroidRuntime(305): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-04 19:16:24.569: E/AndroidRuntime(305): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 19:16:24.569: E/AndroidRuntime(305): at java.lang.reflect.Method.invoke(Method.java:521)
11-04 19:16:24.569: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-04 19:16:24.569: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-04 19:16:24.569: E/AndroidRuntime(305): at dalvik.system.NativeStart.main(Native Method)
11-04 19:16:24.569: E/AndroidRuntime(305): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
11-04 19:16:24.569: E/AndroidRuntime(305): at android.app.Activity.getSystemService(Activity.java:3526)
11-04 19:16:24.569: E/AndroidRuntime(305): at android.widget.ArrayAdapter.init(ArrayAdapter.java:271)
11-04 19:16:24.569: E/AndroidRuntime(305): at android.widget.ArrayAdapter.<init> (ArrayAdapter.java:150)
11-04 19:16:24.569: E/AndroidRuntime(305): at com.courseorganizer.MainActivity.<init> (MainActivity.java:24)
11-04 19:16:24.569: E/AndroidRuntime(305): at java.lang.Class.newInstanceImpl(Native Method)
11-04 19:16:24.569: E/AndroidRuntime(305): at java.lang.Class.newInstance(Class.java:1429)
11-04 19:16:24.569: E/AndroidRuntime(305): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-04 19:16:24.569: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
11-04 19:16:24.569: E/AndroidRuntime(305): ... 11 more
11-04 19:16:26.998: I/Process(305): Sending signal. PID: 305 SIG: 9