MediaRecorder setAudioSource failed -- App Crashes (possibly due to CM13) - java

I followed a video tutorial to write this code. I get a java.lang.RuntimeException: setAudioSource failed error that causes Unable to start activity
I am debugging on a 1st Gen Moto G with Cyanogenmod 13 installed.
My assumption is that my custom ROM has a conflict with MediaRecorder. Any ideas will be greatly appreciated!
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I should also mention that I have the correct permissions in AndroidManifest
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
MediaRecorder myAudioRecorder;
private String outputFile = null;
private Button start, stop, play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.button1);
stop = (Button) findViewById(R.id.button2);
play = (Button) findViewById(R.id.button3);
stop.setEnabled(false);
play.setEnabled(false);
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myrec.3pg";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
}
public void start(View v) {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch(IllegalStateException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
start.setEnabled(false);
stop.setEnabled(true);
Toast.makeText(this, "Recording Started", Toast.LENGTH_SHORT).show();
}
public void stop(View v) {
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;
stop.setEnabled(false);
play.setEnabled(true);
Toast.makeText(this, "Audio recorded successfully", Toast.LENGTH_SHORT).show();
}
public void play(View v) throws IOException {
MediaPlayer m = new MediaPlayer();
m.setDataSource(outputFile);
m.prepare();
m.start();
Toast.makeText(this, "Playing Audio", Toast.LENGTH_SHORT).show();
}
UI / XML Editor
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.m4db0x.freestylerapdisorder.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="#string/recording_title"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:scaleType="fitXY"
android:src="#android:drawable/presence_audio_online"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView1"
android:layout_marginTop="67dp"
android:onClick="start"
android:text="#string/start" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/button1"
android:layout_below="#id/button1"
android:layout_alignRight="#id/textView1"
android:layout_marginTop="40dp"
android:onClick="stop"
android:text="#string/stop" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/button2"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:onClick="play"
android:text="#string/play" />
</RelativeLayout>

May be its too late but hope it would be helpful for someone...
Try to Change in your device
settings > privacy and safety > App Permission.
Try giving the required permission there too. Permission given in the device takes higher ranking than in the code.
That helped me out.

Related

Android Studio Beginner: Keep Getting NoSuchMethodException for android.graphics.FontFamily. <init> []

I keep getting this error whenever I run the application, which crashes every time I interact with it. The problem first occurred after I moved the original TextView around and altered the color and Font Style. I'm not sure why it's happening now, but it didn't happen before. I'm not exactly sure why this error is occurring either. Please help.
Below is the error message:
E/TypefaceCompatApi26Impl: Unable to collect necessary methods for class java.lang.NoSuchMethodException
java.lang.NoSuchMethodException: android.graphics.FontFamily.<init> []
This is the MainActivity class:
package com.example.exploreroptimizer;
import android.app.ActivityManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
final Cleaner cleaner = new Cleaner();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button cleanButton = findViewById(R.id.cleanButton);
final ProgressBar cleanBar = findViewById(R.id.progressBar);
final TextView progressUpdate = findViewById(R.id.progressUpdate);
System.out.println("success");
cleanButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Thread thread = new Thread() {
#Override
public void run() {
try {
final ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
final double a = mi.availMem / 0x100000L;
cleanBar.setProgress(20);
progressUpdate.setText("Cleaning Ram!");
Thread.sleep(1500);
cleanBar.setProgress(40);
progressUpdate.setText("Closing Unused Apps!");
Thread.sleep(1500);
cleanBar.setProgress(100);
progressUpdate.setText("Clearing Cache!");
Thread.sleep(1500);
Cleaner.deleteCache(getApplicationContext());
cleaner.killBackgroundProcesses(getApplicationContext(), getApplicationContext().getPackageManager());
cleaner.clearRAM();
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "I have closed " + Cleaner.i + " apps, ", Toast.LENGTH_SHORT).show();
}
});
} catch (InterruptedException e) {
}
}
};
thread.start();
}
});
}
}
And this is the xml file code for the UI:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/cleanButton"
android:layout_width="172dp"
android:layout_height="166dp"
android:background="#drawable/cleanbutton"
android:fontFamily="sans-serif"
android:text="Clean"
android:textColor="#FFFFFF"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="312dp"
android:layout_height="382dp"
android:indeterminate="false"
android:max="0"
android:progress="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/progressUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:text="TextView"
android:textColor="#0DB344"
android:textSize="18sp"
android:textStyle="normal|bold"
android:typeface="sans"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/progressBar" />
</androidx.constraintlayout.widget.ConstraintLayout>

App keeps crashing every time i click the Log in Button

I am new to android, i was following some tutorial and at this point, app keeps crashing when i click the login button.
When i run the app and click the login button, the app crashes and in the logcat, it shows this error of java RuntimeException.
This is my logcat, loginactivity.xml and loginactivity.java.
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.ugtunda/com.example.ugtunda.LoginActivity}:
java.lang.ClassCastException:
androidx.appcompat.widget.AppCompatTextView cannot be cast to
com.rey.material.widget.TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2806)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2884)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1614)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6524)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:451)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatTextView cannot be cast to
com.rey.material.widget.TextView
at com.example.ugtunda.LoginActivity.onCreate(LoginActivity.java:48)
at android.app.Activity.performCreate(Activity.java:7044)
at android.app.Activity.performCreate(Activity.java:7035)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2759)
Here is my login_activity_XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/login"
tools:context=".LoginActivity">
<ImageView
android:id="#+id/login_applogo"
android:layout_width="300dp"
android:layout_height="100dp"
android:src="#drawable/applogo"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
/>
<EditText
android:id="#+id/login_phone_number_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/login_applogo"
android:background="#drawable/input_design"
android:padding="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:hint="Phone Number"
android:inputType="number"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="17sp"
android:textStyle="bold"
/>
<EditText
android:id="#+id/login_password_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/login_phone_number_input"
android:background="#drawable/input_design"
android:padding="20dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="6dp"
android:hint="Password"
android:inputType="textPassword"
android:textColor="#color/colorPrimaryDark"
android:textColorHint="#color/colorPrimaryDark"
android:textSize="17sp"
android:textStyle="bold"
/>
<LinearLayout
android:id="#+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#id/login_password_input"
android:layout_marginTop="5dp"
>
<com.rey.material.widget.CheckBox
android:id="#+id/remember_me_chkb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Material.Drawable.CheckBox"
android:text="Remember Me"
android:textColor="#color/colorPrimaryDark"
app:cbd_strokeColor="#color/colorPrimaryDark"
android:gravity="center_vertical"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginLeft="17dp"
/>
<TextView
android:id="#+id/forget_password_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password"
android:textColor="#color/colorPrimaryDark"
android:textSize="15sp"
android:textStyle="bold"
android:layout_marginLeft="80dp"
/>
</LinearLayout>
<Button
android:id="#+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linear_layout_1"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#drawable/buttons"
android:padding="17dp"
android:text="Login"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="18sp" />
<TextView
android:id="#+id/admin_panel_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/login_button"
android:layout_alignParentEnd="true"
android:layout_marginLeft="80dp"
android:layout_marginEnd="23dp"
android:text="I'm an Admin"
android:textAppearance="#android:style/TextAppearance.Material.Small"
android:textColor="#color/colorPrimaryDark"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/not_admin_panel_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm not an Admin"
android:layout_alignParentStart="true"
android:layout_below="#+id/login_button"
android:textColor="#color/colorPrimaryDark"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="80dp"
android:layout_marginEnd="23dp"
android:layout_marginStart="25dp"
android:visibility="invisible"
/>
</RelativeLayout>
And lastly my loginactivity.java
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.ugtunda.Models.Users;
import com.example.ugtunda.Prevalent.Prevalent;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rey.material.widget.CheckBox;
import com.rey.material.widget.TextView;
import io.paperdb.Paper;
public class LoginActivity extends AppCompatActivity
{
private EditText InputPhoneNumber, InputPassword;
private Button LoginButton;
private ProgressDialog loadingBar;
private TextView AdminLink, NotAdminLink;
private String parentDbName = "Users";
private CheckBox chkBoxRememberMe;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LoginButton = findViewById(R.id.login_button);
InputPassword = findViewById(R.id.login_password_input);
InputPhoneNumber = findViewById(R.id.login_phone_number_input);
AdminLink = findViewById(R.id.admin_panel_link);
NotAdminLink = findViewById(R.id.not_admin_panel_link);
loadingBar = new ProgressDialog(this);
chkBoxRememberMe = findViewById(R.id.remember_me_chkb);
Paper.init(this);
LoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
LoginUser();
}
});
AdminLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
LoginButton.setText("Login Admin");
AdminLink.setVisibility(View.INVISIBLE);
NotAdminLink.setVisibility(View.VISIBLE);
parentDbName = "Admins";
}
});
NotAdminLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
LoginButton.setText("Login");
AdminLink.setVisibility(View.VISIBLE);
NotAdminLink.setVisibility(View.INVISIBLE);
parentDbName = "Users";
}
});
}
private void LoginUser()
{
String phone = InputPhoneNumber.getText().toString();
String password = InputPassword.getText().toString();
if (TextUtils.isEmpty(phone))
{
Toast.makeText(this, "Please write your phone number...", Toast.LENGTH_SHORT).show();
}
else if (TextUtils.isEmpty(password))
{
Toast.makeText(this, "Please write your password...", Toast.LENGTH_SHORT).show();
}
else
{
loadingBar.setTitle("Login Account");
loadingBar.setMessage("Please wait, while we are checking the credentials.");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
AllowAccessToAccount(phone, password);
}
}
private void AllowAccessToAccount(final String phone, final String password)
{
if(chkBoxRememberMe.isChecked())
{
Paper.book().write(Prevalent.USER_PHONE_KEY, phone);
Paper.book().write(Prevalent.USER_PASSWORD_KEY, password);
}
final DatabaseReference RootRef;
RootRef = FirebaseDatabase.getInstance().getReference();
RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.child(parentDbName).child(phone).exists())
{
Users usersData = dataSnapshot.child(parentDbName).child(phone).getValue(Users.class);
if (usersData.getPhone().equals(phone))
{
if (usersData.getPassword().equals(password))
{
if (parentDbName.equals("Admins"))
{
Toast.makeText(LoginActivity.this, "Welcome Admin, you are logged in Successfully...", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
Intent intent = new Intent(LoginActivity.this, AdminCategoryActivity.class);
startActivity(intent);
}
else if (parentDbName.equals("Users"))
{
Toast.makeText(LoginActivity.this, "logged in Successfully...", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
Prevalent.currentOnlineUser = usersData;
startActivity(intent);
}
}
else
{
loadingBar.dismiss();
Toast.makeText(LoginActivity.this, "Password is incorrect.", Toast.LENGTH_SHORT).show();
}
}
}
else
{
Toast.makeText(LoginActivity.this, "Account with this " + phone + " number do not exists.", Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
It looks like you're defining standard Android components in the XML layout, then attempting to inflate them into custom components (the com.rey.material package). Try going into your imports and deleting the com.rey.material lines. Then when your IDE prompts you to import the TextView and Checkbox, make sure you're importing the versions from android.widget, not com.rey.material. Alternatively, you could change your XML file to specify the com.rey.material components if you wanted to be using those instead of the default ones.
You used the wrong import so you need to
replace this import
import com.rey.material.widget.TextView;
with
import android.widget.TextView;
OR
You can use the corresponding view in the XML
Replace
<TextView
android:id="#+id/forget_password_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password"
android:textColor="#color/colorPrimaryDark"
android:textSize="15sp"
android:textStyle="bold"
android:layout_marginLeft="80dp"
/>
with
<com.rey.material.widget.TextView
android:id="#+id/forget_password_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password"
android:textColor="#color/colorPrimaryDark"
android:textSize="15sp"
android:textStyle="bold"
android:layout_marginLeft="80dp" />
in your XML file
Looks like you are using the AppCompat Button in your XML, but using the material button in your java code.
So they need to be the same.
First you need to use the right import for whatever view you're using, if you want to use the AppCompat button then import like this
import android.widget.Button;
If you want to use the Material button, then use this import:
import com.rey.material.widget.Button;
Same thing goes for the TextView.
Hope this helps!
androidx.appcompat.widget.AppCompatTextView cannot be cast to
com.rey.material.widget.TextView
In your xml, you're using the SDK TextView but in your code, you want the com.rey.material.widget one.
--> Change one of these

I am trying to implement a camera feature onto my application but "app wont run"

So I have a functional application that has many pages and this is the registration 2 page that is supposed to open a standard android camera and then store it using the code at the bottom of the registration java page.
Here is my code:
package com.example.mockups;
import android.graphics.drawable.Drawable;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import java.io.File;
public class Register2 extends AppCompatActivity {
Button button;
ImageView imageView;
static final int CAM_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register2);
button = (Button) findViewById(R.id.buttonContinue);
imageView = (ImageView) findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent camera_intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri. fromFile(file));
startActivityForResult(camera_intent, CAM_REQUEST);
}
});
}
private File getFile()
{
File folder = new File("sdcard/camera");
if (!folder.exists())
{
folder.mkdir();
}
File image_file = new File(folder, "cam_image.jpg");
return image_file;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/camera/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}
}
_________________________________
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_register2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.strongholdmockups.Register2">
<TextView
android:text="Build Profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buildProTitle"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:text="Upload Profile Picture:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:id="#+id/textView46"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:layout_below="#+id/buildProTitle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="36dp"
android:layout_marginStart="36dp" />
<Button
android:text="Upload from Camera Roll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:id="#+id/buttonContinue"
style="#android:style/Widget.Holo.Light.Button"
android:layout_below="#+id/textView46"
android:layout_alignRight="#+id/buildProTitle"
android:layout_alignEnd="#+id/buildProTitle" />
<ImageView
android:id="#+id/image_view"
android:layout_width="350dp"
android:layout_height="250dp"
android:layout_above="#+id/continueBtn1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal" />
<Button
android:text="Continue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="23dp"
android:layout_marginEnd="23dp"
android:layout_marginBottom="14dp"
android:id="#+id/continueBtn1"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:background="#color/cardview_light_background"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/image_view"
android:layout_alignEnd="#+id/image_view" />
</RelativeLayout>
__________________
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
You have to check if permissions are granted for API version >= 23. Add something like this to your code before using the camera/external storage.
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
}else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}

Error:(41) Error parsing XML: not well-formed (invalid token) + cannot resolve the symbol R

I can't find it anywhere. I have looked for over an hour now and am having zero luck. Do you see it?
I am also getting this error message:
Error:Execution failed for task ':app:processDebugResources'.
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Users\danle\AppData\Local\Android\Sdk\build-tools\24.0.0\aapt.exe'' finished with non-zero exit value 1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.musicplayer.MainActivity">
<TextView android:text="#string/music_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textview"
android:textSize="35sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dana_higz"
android:id="#+id/textView"
android:layout_below="#+id/textview"
android:layout_centerHorizontal="true"
android:textColor="#ff7aff24"
android:textSize="35sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:src="#drawable/abc"
android:contentDescription="#string/player_icon" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|<<"
android:id="#+id/reset"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/play"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">>"
android:id="#+id/play"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="||"
android:id="#+id/pause"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/play"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
package com.example.musicplayer;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(this, R.raw.gorillaz);
Button play = (Button) findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.start();
}
});
Button pause = (Button) findViewById(R.id.pause);
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.pause();
}
});
Button reset = (Button) findViewById(R.id.reset);
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.reset();
}
});
}
}
You can't use < in XML literally:
android:text="|<<"
Try replacing it with
android:text="|<<"

Unfortunately, your app stopped working

I honestly do not know what is wrong with my code. I have searched for answers here and have done everything and i simply cannot tell what is wrong, Please help?
This is my MainActivity.java,
package com.example.myname.easternmusic;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
TimerTask task=new TimerTask() {
#Override
public void run() {
finish();
startActivity(new Intent(MainActivity.this, Main.class));
}
};
Timer opening=new Timer();
opening.schedule(task,5000);
}
}
My splash.xml which is the layout for the MainActivity.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sounds of the East"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:textSize="20dp"
android:textColor="#000000"
android:textStyle="bold"
android:gravity="center_horizontal"
android:background="#drawable/bell"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
My Main.java which is the extra class I created for the main_activity.xml file
package com.example.myname.easternmusic;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Main extends Activity {
Button btBamboo, btPalace;
MediaPlayer mpBamboo, mpPalace;
int playing;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView=(ImageView)findViewById(R.id.imageView);
ImageView imageView1=(ImageView)findViewById(R.id.imageView2);
btBamboo=(Button)findViewById(R.id.button1);
btPalace=(Button)findViewById(R.id.button2);
btBamboo.setOnClickListener(bBamboo);
btPalace.setOnClickListener(bPalace);
mpBamboo=new MediaPlayer();
mpBamboo=MediaPlayer.create(this,R.raw.bamboo);
mpPalace=new MediaPlayer();
mpPalace=MediaPlayer.create(this,R.raw.palace);
playing=0;
}
Button.OnClickListener bBamboo=new Button.OnClickListener(){
#Override
public void onClick(View v) {
switch (playing){
case 0:
mpBamboo.start();
playing=1;
btBamboo.setText("Pause Bamboo Song");
btPalace.setVisibility(View.INVISIBLE);
break;
case 1:
mpBamboo.pause();
playing=0;
btBamboo.setText("Play Bamboo Song");
btPalace.setVisibility(View.VISIBLE);
break;
}
}
};
Button.OnClickListener bPalace=new Button.OnClickListener(){
#Override
public void onClick(View v) {
switch (playing){
case 0:
mpPalace.start();
playing=1;
btPalace.setText("Pause Palace Song");
btBamboo.setVisibility(View.INVISIBLE);
break;
case 1:
mpPalace.pause();
playing=0;
btPalace.setText("Play Palace Song");
btBamboo.setVisibility(View.VISIBLE);
break;
}
}
};
}
and finally, the activity_main.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Splash">
<ImageView
android:layout_width="320dp"
android:layout_height="150dp"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="#drawable/band" />
<Button
android:layout_width="320dp"
android:layout_height="wrap_content"
android:text="Play Bamboo Song"
android:id="#+id/button1"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:textSize="22dp"
android:layout_marginBottom="10dp" />
<ImageView
android:layout_width="320dp"
android:layout_height="150dp"
android:id="#+id/imageView2"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="#drawable/drums" />
<Button
android:layout_width="320dp"
android:layout_height="wrap_content"
android:text="Play Palace Song"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textSize="22dp"
android:layout_marginBottom="10dp"
android:layout_alignParentStart="false" />
When the app is run, it should pull up a splash screen with the splash layout for five seconds, and then display the activity_main layout, but it stops right after it switches screens. Please help.
write
startActivity(new Intent(MainActivity.this, Main.class));
then write
finish();
here You first finish your activity and after that you are try to call that activity. That is not possible.

Categories