How To Access and Modify activity outside OnCreate class

157 Views Asked by At

I want to modify the TextField "Mitte" in the class GUI but if i try i get an Error:

Im new to Android so i struggle with the concepts of intends, activities and OnCreate(), but i understand Java and Objects;

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidstudioshit/com.example.androidstudioshit.MAIN}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference

How My APP is Designed (OOP):

Main (just generates Steuerung):

public class MAIN extends AppCompatActivity
{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Steuerung();
        setContentView(R.layout.activity_load);
    }

}

Steuerung (Has a bidirectional Association with GUI):

And also A few Associations with other Classes (but i dont think they matter in this context)

public class Steuerung
{

    //Attribute
    private GUI DieGUI;
    private BT BlT;
    private Funktionen Fkt;

    public Steuerung()
    {
        DieGUI = new GUI(this);
        Str();
    }

    public void Str()
    {
        DieGUI.createGUI();
    }

}

GUI:

public class GUI extends AppCompatActivity{private Steuerung steuerung;
public GUI()
{}

public GUI(Steuerung steuerung)
{
    this.steuerung = steuerung;
}

public void createGUI()
{
    TextView txtfeld = (TextView) findViewById(R.id.TextMitte);
    txtfeld.setText("Loaded"); //As soon as i Use this Code i get the Error
}
}

Main Activity (named "activity_load")



<androidx.constraintlayout.widget.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:layout_width="match_parent"android:layout_height="match_parent"tools:context=".GUI">
<TextView
    android:id="@+id/TextMitte"
    android:layout_width="136dp"
    android:layout_height="72dp"
    android:text="Loading..."
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView"
    android:layout_width="166dp"
    android:layout_height="23dp"
    android:text="Bluetoth App Arduino V1"
    app:layout_constraintBottom_toTopOf="@+id/TextMitte"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.497"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.234" />
</androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.androidstudioshit"><uses-permission android:name="andriod.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<uses-feature android:name="android.hardware.bluetooth" android:required="true"/>


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="BT APP"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AndroidStudioShit">

    <activity
        android:name = ".MAIN"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".GUI" />
</application>

I tried to fix it by: -putting setContentView(R.layout.activity_load); into GUI -creating Intends (but they might just have been wrong) -Playing with the references in xml files.

Later in the Programming I dont just want to change the text but also recieve ONClick() from Buttons in GUI.

I thought about just moving GUI MAIN is also not really an Option for me.

I would be happy if you could help me.

0

There are 0 best solutions below