I try to add Text from an EditText-Element by clicking on an navigation. I tried to exclude the Access to a ListView-Element, in which the Text should get added, in a Java-Class and also initialized it in my main Activity. If i now try to click on the Send-Button it crashes do to the Error: "System services not available to Activities before onCreate()". How can I fix this? I tried solving it by looking into similar Questions but cant figure it out... I also tried adding an onCreate-Method to the ListViewSDK-Class.
MainActivity:
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>
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>
The problem is caused by the ListViewSDK
instance creation in the onCreate()
of your MainActivity
.
In Android, Never Call The Constructors Of Android Components (Activities
, Fragments
, Service
, ContentProvider
, BroadcastReceiver
).
They have their own lifecycles and they are instantiated by the Android system. Use an Intent
to summon them or use the Context
(depending on what you need) instead of calling the constructor.
Further Reading:
1. Intro to Activities
2. Starting new Activities
Also, you can have an independent ListView
. You do not to subclass it with a Lifecycle
having component (like Activity
). Simply get a [Recycler View][3]
and your should be okay.