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);
}
Related
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.
Can Butterknife work with Espresso? I have a test sample, there are 2 activities. In the first activity I print the text, than click the button and send text to another activity. After that I created MainActivityTest and everything worked well. But when I started to use Butterknife I caught the error NoMatchingViewException.
My first activity:
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
#BindView(R.id.first_text)
EditText mEditText;
#OnClick(R.id.button_one)
public void click(){
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("TEXT",mEditText.getText().toString());
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The second activity:
public class SecondActivity extends AppCompatActivity {
public static final String TAG = SecondActivity.class.getSimpleName();
#BindView(R.id.second_text)
TextView mTextView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
#Override
protected void onResume() {
super.onResume();
if(getIntent().getStringExtra("TEXT")!=null) {
mTextView.setText(getIntent().getStringExtra("TEXT"));
}
}
}
My MainActivityTest:
public class MainActivityTest {
#Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
MainActivity.class);
#Before
public void setUp() throws Exception {
}
#Test
public void onCreate() throws Exception {
onView(withId(R.id.first_text)).perform(typeText("Test"));
onView(withId(R.id.button_one)).perform(click());
onView(withId(R.id.second_text)).check(matches(withText("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) {
}
}
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";
}
}
public class MyClassActivity<T> extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// todo..
}
It is possible?
How startActivity with T object?