如何访问和修改 OnCreate 类之外的活动

发布于 2025-01-16 08:40:47 字数 4059 浏览 0 评论 0原文

我想修改类 GUI 中的 TextField“Mitte”,但如果我尝试,我会得到一个错误:

我是 Android 新手,所以我对意图、活动和 OnCreate() 的概念很挣扎,但我了解 Java 和对象;

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

我的应用程序是如何设计的(OOP):

主要(仅生成 Steuerung):

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

}

Steuerung(与 GUI 具有双向关联):

还有一些与其他类的关联(但我认为它们在这种情况下并不重要)

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
}
}

主要活动(名为“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>

我尝试通过以下方式修复它: -放置setContentView(R.layout.activity_load);进入图形用户界面 -创造意图(但他们可能只是错了) -使用 xml 文件中的引用。

在稍后的编程中,我不仅想更改文本,还想从 GUI 中的按钮接收 ONClick()。

我认为仅仅移动 GUI MAIN 对我来说也不是真正的选择。

如果您能帮助我,我会很高兴。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文