I have defined viewbinding in the build.gradle app section, but I am getting an error in the codes. my app is up to date.
MainActivity extends AppCompatActivity {
ActivityMainBinding
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(binding.root);
}
}
build gradle:
buildFeatures
{
viewBinding true;
}
i tried uptaded and rebuild project but it doesnt work
Related
I am trying to make a video(movie.mp4) play when I click the imageButton but it keeps saying "Can not extract resource from com.android.aaptcompiler.ParsedResource#7282664d."
this is my code:
`
public class Activity2 extends AppCompatActivity {
VideoView VideoView2;
Uri uri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
VideoView2=(VideoView) findViewById(R.id.videoView);
uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.movie);
VideoView2.setVideoURI(uri);
}
public void play(View view) {
VideoView2.start();
}
}
`
I tried changing the source video(movie.mp4) in "raw" directory multiple time, didn't work.
There is a "?" symbol next to the video
I'm refactoring a lot my app activities and layouts (e.g. ButterKnife annotation -> viewBinding, *Layout --> ConstraintLayout). For a lot of them there were no problem, but at the moment I'm stuck in this situation of my Activity:
Original code:
...
#BindView(R.id.logged_user)
TextView mUserLoggedView;
#BindView(R.id.patient_code)
EditText mPatientCodeView;
#BindView(R.id.search_form)
ScrollView mFormView;
#BindView(R.id.search_progress_layout)
LinearLayout mProgressLayout;
#BindView(R.id.search_progress_text)
TextView mProgressTextView;
App application;
SearchActivityMapper mapper;
CompositeDisposable mCompositeDisposable;
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_search_patient);
ButterKnife.bind(this);
application = (App) getApplication();
application.getAppComponent().inject(this);
...
Code after review:
...
App application;
SearchActivityMapper mapper;
CompositeDisposable mCompositeDisposable;
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
viewBinding = ActivitySearchPatientBinding.inflate(getLayoutInflater());
setContentView(viewBinding.getRoot());
ButterKnife.bind(this);
application = (App) getApplication();
application.getAppComponent().inject(this);
...
I did the same in all the previous Activity and everything went well; when the latter Activity is launched I got this error
java.lang.NoSuchFieldError: No field mUserLoggedView of type Landroid/widget/TextView; in class Lit/company/etmhome/activities/search/SearchActivity; or its superclasses (declaration of 'it.company.etmhome.activities.search.SearchActivity' appears in /data/data/it.company.etmhome.debug/code_cache/.overlay/base.apk/classes27.dex)
I can't understand why it can't find that field that I removed and it's not present in the whole project.
Am I missing something???
Thanks in advance
It's quite long to change the code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
to:
public class MainActivity extends AppCompatActivity {
ActivityMainBinding mainBinding;
#Override
protected void onCreate(Bundle savedInstanceState) {
mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
super.onCreate(savedInstanceState);
setContentView(mainBinding.getRoot());
}
}
every time I create a new project or an activity. Is it possible to automate this process?
You can not automate the process since we have to provide the layoutId ourself. Activity is not gonna bind to a layout automatically. What you can do is Create a BaseActivity and inherit it from all your Activites. Below is a template with binding .
public abstract class BaseActivity<B extends ViewDataBinding> extends AppCompatActivity {
protected abstract int getContentViewId();
protected B binding;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, getContentViewId())
}
}
class MainActivity extends BaseActivity<ActivityMainBinding> {
#Override
public int getContentViewId() {
return R.layout.activity_main;
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// now you can directly access binding here
}
}
This is just for Binding you can Also add some reusable method in BaseActivity and use them in Any Activity without writing them again. and super.onCreate(savedInstanceState) should be first line for call .
No, I think it's not possible but you can make Live Template to get it frequently.
Or Simple that you can make a copy of that folder as a template. Whenever you need to create a new project it will be easy to copy/paste and just change the name of the project.
Thank you.
Android studio does not return me any errors but when I run this code emulator I receive a message Unfortunately, the app has stopped
I am connecting two activities in the android studio using OnClick and Intent. I tried to find errors through Logcat but even there, there are no any errors.
First page is std_home.java and second is std_registration.java
public class std_home extends AppCompatActivity {
private Button b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_std_home);
b2 = (Button) findViewById(R.id.std_sign_in);
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
startActivity(new Intent(std_home.this, std_registration.class));
}
});
public class std_registration extends AppCompatActivity {
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_std_registration);
t = (TextView) findViewById(R.id.reg_title);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/AdobeMing-Light.ttf");
t.setTypeface(myCustomFont);
}
}
The expected result is after button Sign in is clicked app should open registration form but it stops the process with the message Unfortunately, the app has stopped.
I am trying to build an Android Wear app. When I start new project with APK 23 (Marshmallow) and above, the default code I get with the new project are as follows;
public class MainActivity extends Activity {
private TextView mTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener()
{
#Override
public void onLayoutInflated(WatchViewStub stub)
{
mTextView = (TextView) stub.findViewById(R.id.textView);
}
});
}
}
But according to a Android blog, WatchViewStub class is deprecated from API 23 (Marshmallow). According to the blog files rect_activity_main.xml and round_activity_main.xml are replaced by one layout file. But my problem is even when I start a new project with newer APIs I still get the deprecated classes and files (XMLs) in my project. According to the blog, this is how the new default code should look like in main_activity.java;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text);
}
How do I fix this? Please comment before down voting. I have tried a lot of things around the web; Asking at stack overflow was my last resort.
I solved this problem by using one of Android studio's skeleton projects. Then amend the code as needed. I am not still sure how to do this by default when we open a new project.