Java 错误:";onCreate()之前的活动无法使用系统服务;即使我有一个onCreate()函数
我试图通过单击导航添加EditText元素中的文本。 我试图在Java类中排除对ListView元素的访问,在该元素中应该添加文本,并在我的主活动中初始化它。 如果我现在尝试单击“发送”按钮,它会导致错误:“系统服务不可用于onCreate()之前的活动”。 我怎样才能解决这个问题?我试着通过研究类似的问题来解决这个问题,但是我想不出来。。。 我还尝试向ListViewSDK类添加onCreate方法 主要活动:Java 错误:";onCreate()之前的活动无法使用系统服务;即使我有一个onCreate()函数,java,android,android-activity,Java,Android,Android Activity,我试图通过单击导航添加EditText元素中的文本。 我试图在Java类中排除对ListView元素的访问,在该元素中应该添加文本,并在我的主活动中初始化它。 如果我现在尝试单击“发送”按钮,它会导致错误:“系统服务不可用于onCreate()之前的活动”。 我怎样才能解决这个问题?我试着通过研究类似的问题来解决这个问题,但是我想不出来。。。 我还尝试向ListViewSDK类添加onCreate方法 主要活动: public class MainActivity extends AppComp
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
ListViewSDK LISTVIEW;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
//LISTVIEW instanz erstellen
LISTVIEW=new ListViewSDK();
//Download Last messages
//Add messages to MessagesList
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.send_button:
//Send Message to ServerScript
//Initialize Inputfield
EditText INPUTFIELD = (EditText) findViewById(R.id.inputfield);
String MESSAGE = INPUTFIELD.getText().toString();
LISTVIEW.ADD_ITEM(MESSAGE);
return true;
}
return false;
}
};
}
ListViewSDK:
public class ListViewSDK extends ListActivity {
//ArrayList mit Strings für die Chatanzeige
ArrayList<String> ITEMS=new ArrayList<String>();
//Adapter der die Daten der ListView verwaltet
ArrayAdapter<String> ADAPTER;
public void onCreate(Bundle icicle){
super.onCreate(icicle);
ADAPTER=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ITEMS);
setListAdapter(ADAPTER);
}
public void ADD_ITEM(String ITEM){
ITEMS.add(ITEM);
ADAPTER.notifyDataSetChanged();
}
}
公共类ListViewSDK扩展ListActivity{
//ArrayList mit Strings für die Chatanzeige
ArrayList ITEMS=新建ArrayList();
//ListView Verwalet的模具日期适配器
阵列适配器;
创建公共空间(捆绑冰柱){
超级冰柱;
ADAPTER=newarrayadapter(这是android.R.layout.simple\u list\u item\u 1,ITEMS);
setListAdapter(适配器);
}
公共无效添加项(字符串项){
项目。添加(项目);
ADAPTER.notifyDataSetChanged();
}
}
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/inputfield"
tools:layout_editor_absoluteX="8dp" app:layout_constraintBottom_toBottomOf="parent">
</ListView>
<EditText android:id="@+id/inputfield" app:layout_constraintBottom_toTopOf="@+id/navigation" android:layout_width="match_parent" android:layout_height="48dp" android:background="@android:color/darker_gray"></EditText>
</android.support.constraint.ConstraintLayout>
navigation.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/send_button"
android:icon="@drawable/ic_paper_plane"
android:title="Send"/>
</menu>
问题是由
main活动的onCreate()
中的ListViewSDK
实例创建引起的
在Android中,永远不要调用Android组件的构造函数(活动
,片段
,服务
,内容提供者
,广播接收器
)
它们有自己的生命周期,并由Android系统实例化。使用意图
调用它们,或者使用上下文
(取决于您的需要),而不是调用构造函数
进一步阅读:
1.
2.
此外,您还可以拥有一个独立的列表视图
。您不需要使用具有组件的生命周期
对其进行子类化(如活动
)。只需获得一个[Recycler View][3]
,您的手机就可以了