java - 错误:“系统服务在 onCreate() 之前对活动不可用”,即使我有一个 onCreate() 函数
问题描述
我尝试通过单击导航从 EditText-Element 添加文本。我试图在 Java 类中排除对 ListView-Element 的访问,其中应该添加文本,并在我的主 Activity 中对其进行了初始化。如果我现在尝试单击发送按钮,它会崩溃,因为错误:“系统服务在 onCreate() 之前对活动不可用”。我怎样才能解决这个问题?我尝试通过查看类似的问题来解决它,但无法弄清楚......我还尝试将 onCreate-Method 添加到 ListViewSDK-Class。
主要活动:
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();
}
}
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>
导航.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>
解决方案
ListViewSDK
该问题是由onCreate()
您的MainActivity
.
在 Android 中,永远不要调用 Android 组件 ( , , , , )的构造函数Activities
Fragments
Service
ContentProvider
BroadcastReceiver
。
它们有自己的生命周期,由 Android 系统实例化。使用 anIntent
来召唤它们或使用Context
(取决于您需要什么)而不是调用构造函数。
延伸阅读:
1.介绍Activities
2.开始新的Activities
此外,您可以拥有一个独立的ListView
. 您不要使用Lifecycle
具有组件(如Activity
)对其进行子类化。简单地得到一个[Recycler View][3]
,你应该没问题。
推荐阅读
- python - 使用熊猫转换嵌套数组/列表中的excel列
- sql - 使用 Microsoft Query 和 SQL 过程返回多行数据
- html - 如何在本地工作时在 HTML 文件中显示 SVG Sprite 图标?
- javascript - Discord.js 自定义表情符号错误看起来很简单,但我该如何解决?
- javascript - 将行号添加到预格式化文本
- django - React 和 Django 如何真正协同工作
- c - Windows10平台_execl()函数的使用
- google-bigquery - BigQuery 中是否有一种方法可以保存来自区域内多区域的查询
- java - Android Exif 数据未存储在图片文件中
- asp.net-core - ConfigureWebHostDefaults 中的 UseSerilog 未知引用