我的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设置为按钮,将用户的输入添加到填充到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
答案 0 :(得分:0)
findViewById()
和onCreate()
之前调用 setContentView()
,因为没有要搜索的上下文或布局。
在onCreate()
之后将这些行移至setContentView()
。
addItem = (Button)findViewById(R.id.button1);
input = (EditText)findViewById(R.id.editText1);
courseList = (ListView)findViewById(R.id.listView1);