Can not extract resource error in Android Studio - java

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

Related

Why MediaPlayer can't be set in MainActivity?

Why when I create the MediaPlayer variable in the Main Activity, the app crashes?
( This code works fine )
public class MainActivity extends AppCompatActivity {
MediaPlayer audio = MediaPlayer.create(this, R.raw.my_audio);
public void play(View view){
audio.start();
}
public void pause(View view){
audio.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audio = MediaPlayer.create(this, R.raw.my_audio);
}
}
( But this code doesn't work; the app crashes)
public class MainActivity extends AppCompatActivity {
MediaPlayer audio = MediaPlayer.create(this, R.raw.my_audio);
public void play(View view){
audio.start();
}
public void pause(View view){
audio.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
PLEASE give me an explanation :(
in second case you havent created MediaPlayer instance at all
MediaPlayer audio;
so every audio.anyMethodCall() will throw NullPointerException
just keep audio = MediaPlayer.create(this, R.raw.my_audio); line in onCreate, thats proper place for initing player
and please learn some basics about programming...
private MediaPlayer audio;
oncreate method
audio= MediaPlayer.create(this, R.raw.shape_of_you);
audio.start();
You can do something like this
The Correct code should be:
public class MainActivity extends AppCompatActivity {
MediaPlayer audio;
public void play(View view){
audio.start();
}
public void pause(View view){
audio.pause();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
the only change I did was removing declaration before oncreate method:
Earlier:
MediaPlayer audio = MediaPlayer.create(this, R.raw.my_audio);
Correct Code:
MediaPlayer audio;
Explanation:
onCreate() method is for definition like adding audio to MediaPlayer, defining textView, etc, since onCreate() gets triggered after creation of activity(activity_main).
You added audio 2 times, which is not needed, you added an audio at first and then replace it with another audio in onCreate() method

Is it possible to include viewBinding in every project automatically?

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.

How to fix the message passing error between the activities

I had created a simple message passing application in android studio to include this code into my Project work.But I was not able to switch the activity by passing a string into another activity.please help me out to find the problem.
When the button is triggered the application got ended itself instead of switching the activity.I had tried many ways to figure it out.
First Activity(MainActivity)
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn;
EditText text;
btn = (Button)findViewById(R.id.button);
text = (EditText)findViewById(R.id.editText);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String mess = text.getText().toString();
Intent i= new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("EXTRA",mess);
startActivity(i);
}
});
}
}
Second Activity(Main2Activity)
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
EditText editText = (EditText)findViewById(R.id.editText2);
editText.setText(getIntent().getStringExtra("EXTRA"));
}
}
The expected output was to exchange the data from one activity to another,
but now the application gets stopped.

Why first activity does not move to another one,emulator shows "Unfortunately,app has stopped but my Logcat doesn't show any errors"?

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.

MyFirstApp tutorial Android Studio 2.3 Build Errors re EXTRA_MESSAGE and textview

MyFirstApp tutorial Android Studio Issue with receiving messages within the app.
Build Errors are as follows:
Error:(17, 60) error: cannot find symbol variable EXTRA_MESSAGE
Error:(20, 57) error: cannot find symbol variable textView
My code for the message receiver looks like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the Intent that started this activity and extract the
string
Intent intent = getIntent();
String message =
intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Capture the layout's TextView and set the string as its text
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(message);
}
Note: EXTRA_MESSAGE and textview are in RED.
The sending code looks like this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this,
DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Note: In this line in my code from above:
public void sendMessage(View view)
The first "View" has a light horizontal line through it and I get an error message something like sendMessage(View view) is depreciated.
I think the problem may relate to the
public void sendMessage(View view)
and its depreciated code message... (?)
Links to the tutorial where the problem arises:
https://developer.android.com/training/basics/firstapp/starting-activity.html
check this line on your MainActivity.java file, in your code is missing.
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
Not sure what worked, but played around with it and got it working. Checked import statements, added one or two to match documentation.

Categories