As a beginner java and AndroidStudio user, I've created a java file (in same package) called objects and decided to define the items(such as buttons,views etc) there instead of defining them into void createOn().
package com.example.turref;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.tabs.TabItem;
public class Object extends AppCompatActivity {
Tabitem settingsPanel = findViewById(R.id.settingsPanel);
}
However, I also want to use triggers in Mainactivity and make sth visible/invisible once they triggered.
package com.example.turref;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.google.android.material.tabs.TabItem;
public class MainActivity extends AppCompatActivity {
public class Main2 extends Object {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* some kind of trigger (can be button)
calls shower() when triggered */
}
void shower() {
settingsPanel.setVisibility(View.VISIBLE);
}
}
}
When I do sth like that it kinda works but I get a black screen once I run the emulator. Can someone help me solve this?
(I`m open to all kind of ways, the important point is to be able to access settingsPanel in show() method )
First of all, you getting a black screen could be because you didnt turn the emulator on;) There's a power button you need to press before you can use it.
I really like your idea to extract all the view elements into their own file. However, as of now, your app would crash. Your settings panel will be null
, because your Object
Activity can't access the view items, resulting in a NullPointerException
. There just isnt a computed layout for Object
to use, because the view items don't exist yet! setContentView()
needs to be called before accessing view.
So, either you place setContentView()
before the super
-call OR in the onCreate()
of the Object
class itself. This is also where you would place the findView
-calls:
// Possibility 1
public class MainActivity extends Object{
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main); // <---
super.onCreate(savedInstanceState);
}
}
// Possibility 2
public class Object extends AppCompatActivity {
Tabitem settingsPanel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsPanel= findViewById(R.id.SettingsPanel);
}
}
Also, don't nest Activities
into one another. MainActivity can also directly extend Object
Still, I wouldn't recommend doing it like this. If you are really bothered by all the findViewById
calls, you can try ViewBinding , it's the recommended way by google.
Concerning your trigger, why not use a button?
findViewById(R.id.Button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shower()
}
});
(A little side note to the end: Since you are new to android and java and you say you are open for different ways, if this is gonna be a big project with lots of dynamic view changes, don't use java. Kotlin, the other official android language with long-time support from google, has a feature called jetpack compose. It's basically declarative view building -> less code, more dynamic. You can read about it here . If it's a small project, stick with java.
Also, to learn android, here are two good tutorial resources/sites: developers.android andfree code camp .)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.