My method from another class named Model.
public void coba(){
Log.i("sdsds","sdsds")
}
My activity
public class ViewInformasiSiswa extends AppCompatActivity {
Model mod;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mod = new Model();
mod.coba();
String a = "5";
Log.i("coba",a);
}
My question is why does Log.i("coba",a); this run before my mod.coba();
Messages I get:
coba 5
sdsds sdsds
I tried this code and this work perfect. method execute first and then the log appear.
public class Model {
public void coba()
{
Log.i("coba","sdsds");
}
}
public class MainActivity extends AppCompatActivity {
Model mod;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mod = new Model();
mod.coba();
String a = "5";
Log.i("coba",a);
}
}
I think you give the wrong name when you are setting Logcat .Let me know it is helpful for you.
Related
I instantiate a static property at the beginning of Activity's onCreate(), but some of my users get NullpointerException when I try to get it in MainActivity.
In my opinion, you code should be
public class TestActivity extends AppCompatActivity {
public static String mValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mValue = "my_value";
}
}
The value TestActivity.mValue should be null util the onCreate executes.You can init the mValue like this
public class TestActivity extends AppCompatActivity {
public static String mValue;
static {
mValue = "my_value";
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
I have an issue , need your help , my question is : I have a multiple activity , so i have created one abstract class which hold the view . Now my question i am able to pass the view to the Base Activity but how to finds view id in Child Activity Class.
I Tried like this :
public abstract class BaseActivity extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
}
public abstract int getLayoutResourceId();
}
My Activity Class:
public class ServiceActivity extends BaseActivity {
Button startSerice_btn, stopService_btn;
MyService myService;
#Override
public int getLayoutResourceId() {
return R.layout.service_xml;
}
}
You can access your views after the super.onCreate() call in the ServiceActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Access them here
}
I think you no need to set content view in BaseActivity like below :
public abstract class MyAppBaseActivity extends AppCompatActivity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
And you just extends Activity like below :
// for MyCustomActivity1
public class MyCustomActivity1 extends MyAppBaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onClick(View view) {
}
}
// for MyCustomActivity2
public class MyCustomActivity2 extends MyAppBaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onClick(View view) {
}
}
Im interested if i can to set some common listeners inside main activity class? For my project i use FirebaseAuth, so i would like to init it in MainActivity onCreate(), setup needed listeners in onStart() and onStop(), and then inherit that class in every other activity class.
Some code to please you :]
MainActivity class [parent]:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
protected FirebaseAuthentication firebaseAuthentication;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
firebaseAuthentication = new FirebaseAuthentication(FirebaseAuth.getInstance(), FirebaseDatabase.getInstance());
}
#Override
protected void onStart() {
super.onStart();
firebaseAuthentication.addAuthStateListener();
}
#Override
public void onStop() {
super.onStop();
firebaseAuthentication.removeAuthStateListener();
}
}
AuthActivity class [child]:
public class AuthActivity extends MainActivity implements FirebaseAuthentication.OnUserAuthListener {
#BindView(R.id.viewPager) LockableViewPager viewPager;
private String userUID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_market);
ButterKnife.bind(this);
firebaseAuthentication.setOnUserAuthListener(this);
firebaseAuthentication.isSingedIn(); // check if user is singed in
}
#Override
// response for firebaseAuthentication.isSingedIn() above
public void onAuthSuccess(String userUID) {
this.userUID = userUID;
}
#Override
// response for firebaseAuthentication.isSingedIn() above
public void onAuthFailure(String message) {
snackbar(message);
Intent intent = new Intent(this, AuthActivity.class);
startActivity(intent);
finish(); // TODO mb should to delete it
}
}
Can this implementations bring me errors (maybe NullPointerExeption or what unexpectedly in future)?
Would be great if you provide me some sources to read/watch.
Thank you.
Perfect example of abstraction, but not really a question.
You will not get any nullpointers or other errors by implementing it like this.
i write simple android application with two method:
i define a simple string array in this line:
public class breakactivity extends Activity {
String[] id;
protected void onCreate(Bundle savedInstanceState) {
and into this method:
public void fill(){
id=new String[30];
id[0]="x1";
id[1]="x2";
}
and i want fetch the id array data into android onCreate method for example:
protected void onCreate(Bundle savedInstanceState) {
string temp=id[0];
}
How can solve that?thanks for all.
You created a method called fill() now you have to call it in onCreate()
public class breakactivity extends Activity {
String[] id;
protected void onCreate(Bundle savedInstanceState) {
fill();
String temp = id[0];
}
public void fill(){
id = new String[30];
id[0] = "x1";
id[1] = "x2";
}
}
I have an app which reads .txt file and displays contents in table layout.
here is my MainActivity.java file:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TableLayout(this,0));
}
}
Inside TableLayout class when adding first row (headers), i also add on click listeners.
Here is the code that gets executed when click happens:
public class MyOnClickListener extends MainActivity implements OnClickListener {
int rowNumber;
public MyOnClickListener(int rowNumber) {
this.rowNumber = rowNumber;
}
#Override
public void onClick(View v) {
setContentView(new TableLayout(context,rowNumber));
}
};
context is saved from when activity is first started, but i get nullpointexception error with this as an argument.
What i would like to do when header is clicked is to recreate table with header number argument.
So my question is what should i do to restart table creation within onClick method?
Edit: this is how context is saved
Context context;
public TableLayout(Context context, int rowNr) {
super(context);
this.context = context;
I would probably do something like this
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TableLayout(this, getRowNumber()));
}
protected int getRowNumber(){
return 0;
}
}
and then you do something like
public class MyOnClickListener extends MainActivity implements OnClickListener {
// here you initialize rowNumber
static int rowNumber = 0;
#Override
protected void getRowNumber(){
return rowNumber;
}
#Override
public void onClick(View v) {
// here you set your rowNumber
rowNumber = some_value;
recreate();
}
};
P.S: I haven't compiled this but you can get the idea