我的 SherlockActivity,它使用 EditText、Button 和 ListView 来扩展布局。
<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 设置为按钮,以将用户的输入添加到 ListView 中,该 ListView 填充在放入 ArrayAdapter 的 ArrayList 中。
这是主要活动:
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