Android Studio App Crashes When Opening Second Activity - java

I am trying to code an app where it connects to Bluetooth on the Main Activity Page and then on the Second Activity page it will turn on the silent ringer mode. Every time I click on the button to open up the next activity, the app closes down. There are no errors according to Android Studio.
Here is my SecondActivity.java file
package fonephree.fonephreeconnecttobluetooth;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends Activity {
Button button;
AudioManager audiomanager;
Context context;
TextView textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button = (Button)findViewById(R.id.button6);
textview = (TextView)findViewById(R.id.textView2);
context = getApplicationContext();
audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
textview.setText("Silent Mode Enable");
}
});
}
}
and here is my activity_second.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:background="#android:color/holo_orange_dark"
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="fonephree.fonephreeconnecttobluetooth.SecondActivity">
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Silent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button6"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:text="Silent Mode"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/phree"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
And just in case the error is in my AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fonephree.fonephreeconnecttobluetooth">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/title_activity_second"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="fonephree.fonephreeconnecttobluetooth.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
EDIT
I fixed the code but now the app only shuts off when I press the button and it wasn't in silent mode before opening the app. The button just reinforces silent mode, it doesn't actually turn it on.

From the information what you have given, you are setting your XML to be activity_main, while the button6 and textview2 are defined in activity_second.xml
So your onCreate should like below to solve the problem
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button = (Button)findViewById(R.id.button6);
textview = (TextView)findViewById(R.id.textView2);
context = getApplicationContext();
audiomanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
textview.setText("Silent Mode Enable");
}
});
}

Related

My app crashes when I clicked the Imagebutton instead of taking me to another Activity

I'm making an android app on Android Studio. And I come out with a problem . When I click my ImageButton the app crashes instead of going to a new activity (Slide1)...
I dont know where I got it wrong .
Here is my MainActivity.java
package android.example.ptreinas;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
private ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
openSlide1();
}
});
}
public void openSlide1(){
Intent intent = new Intent(this, Slide1.class);
startActivity(intent);
}
}
and my activity_main.xml
<?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:gravity="center"
tools:context=".MainActivity"
android:background="#color/red">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="400px"
android:layout_gravity="center"
android:src="#drawable/gym" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Programa de treinos "
android:layout_gravity="center"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="80px"
android:layout_marginTop="50px"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Escolha o programa de treino mais adequado aos seus objetivos."
android:layout_gravity="center"
android:textAlignment="center"
android:layout_marginTop="50px"
android:textSize="50px"
android:textColor="#ffffff"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:src="#drawable/ic_right"
android:background="#drawable/roundcorner"
android:padding="50px"
android:layout_gravity="center"
android:layout_marginTop="150px"
/>
</LinearLayout>
</RelativeLayout>
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.example.ptreinas">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I dont know if you need the second activity (slide1.xml , Slide1.java) let me know
I need your help, thanks in advance .
replace this :
Intent intent = new Intent(this, Slide1.class);
by this:
Intent intent = new Intent(MainActivity.this, Slide1.class);
add your second activity in manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.example.ptreinas">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//add this line
<activity android:name=".Slide1"/>
</application>

I arrive on a white screen when I try to switch activities on my android app

I just want to switch activity with a simple button
Button test = findViewById(R.id.please);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("MainActivity","I pass there ");
Intent intent = new Intent(MainActivity.this, TestActivity.class);
startActivity(intent);
Log.d("MainActivity","I pass there too");
}
});
I don't have an error, I just arrive on a white screen and not on my new activity. Logs appear in my console. I also have a log when I create my second activity but this one doesn't appear.
for more code,
This is my manifest :
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HomeActivity">
</activity>
<activity android:name=".ScheduleActivity"></activity>
<activity android:name=".MapActivity"></activity>
<activity android:name=".TestActivity"
android:theme="#style/YourTheme">
</activity>
</application>
TestActivity :
public class TestActivity extends AppCompatActivity {
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_test);
Log.d("TestActivity","I arrived in Test Activity !!");
}
}
the button in activity_main :
</android.support.design.widget.AppBarLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="wtf"
android:background="#color/colorAccent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/please"
/>
Activity_test :
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_map_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello there! :D"/>
You are Overriding wrong onCreate method in TestActivity. You have to override this method:
protected void onCreate(#Nullable Bundle savedInstanceState) {}

OnClickListener not responding

I can't get my click listeners working. The ImageButton is retrieved correctly.
Listener (Extending my custom Activity which is setting the view):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("UserActivity", "Loading imagebutton...");
ImageButton iButton = (ImageButton) findViewById(R.id.user_button_ratings);
Log.d("UserActivity", "Button " + iButton);
iButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Button", "Image was clicked");
}
});
}
BaseActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout,
R.drawable.ic_drawer, 0, 0) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.menu);
}
};
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
layers = getResources().getStringArray(R.array.layers_array);
drawerList = (ListView) findViewById(R.id.left_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.adapter_navigation, R.id.navigation_text, layers));
final BaseActivity activity = this;
drawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// Do something
}
});
drawerList.setBackgroundColor(Color.LTGRAY);
}
Layout:
<ImageButton
android:id="#+id/user_button_ratings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/mmb_rating_big" />
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.panic.xenira.mmb"
android:versionCode="2"
android:versionName="0.1.1" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SearchActivity"
android:label="#string/title_activity_search" >
</activity>
<activity
android:name=".BaseActivity"
android:label="#string/title_activity_base" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name=".UserActivity"
android:label="#string/title_activity_user" >
</activity>
<activity
android:name=".UserViewerActivity"
android:label="#string/title_activity_user_viewer" >
</activity>
</application>
</manifest>
There is no error in the LogCat and the onClick method is not caled.
I've tryed different methods of using the listener but never got any response. I also tryed a code that worked for me befor but no response.
Would be great if you culd help me out ;)
When using a DrawerLayout, there should be only one main content View, with the drawer View - in this case, your ListView - listed after it. Using a DrawerLayout in any other way will result in incorrect, unpredictable behavior, often preventing normal interaction with other layout elements.
A tutorial with links to a sample and docs can be found on this developer page.
Here I am giving an example of Image button. If it is also giving error then clean projects of eclipse and try again.
<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=".MainActivity" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginRight="35dp"
android:layout_marginTop="32dp"
android:contentDescription="image"
android:src="#drawable/ic_launcher" />
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
ImageButton imgButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
private void addListenerOnButton() {
imgButton = (ImageButton) findViewById(R.id.imageButton1);
imgButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT)
.show();
}
});
}
}

Android Eclipse: trouble with button click code

I started off by trying to make a simple app on Eclipse.
the target for me was to create a button which simply calls a number.
however, after many tutorials and such, I still couldn't figure out where to write the code and what code was the best.
here is my Manifest . I inserted uses permission CALL_PHONE since it is required (As I learned from some tutorials)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.projectrandomfox.randomfox"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.projectrandomfox.randomfox.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
here is my activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select VAS"
android:textSize="40dp"
android:layout_gravity="center" />
<Button
android:id="#+id/balance"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Check Balance"
android:layout_gravity="center"
/> </LinearLayout>
here is my activity java
package com.projectrandomfox.randomfox;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
THATS ALL.
please help me figure out where to write code in activity, and what code to write
it would mean a lot to me :)
Add this inside your onCreate method:
Button button = (Button) findViewById(R.id.balance);
myButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + telephoneNumber));
startActivity(intent);
}
}
All you need to do is set the telephoneNumber variable
First you have to get a reference your Button.
Button myButton = findViewById(R.id.balance);
Then add the onClickListener.
myButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
// Do whatever
}
}

error emulate player developed in android

I am a beginner in programming for Android, and am making a music and video player, which already programmed much of the program, but when the emulator executes I get the following error:
Unfortunately, FlagPlayer has stopped
I have done many researches on the subject but I can not find anything that solves my problem. I hope you can help me.
here is my main class:
package tcc.etec.FlagPlayer;
import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btMusica;
private Button btArtista;
private Button btAlbum;
private Button btVideo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acaobutton();
}
public void acaobutton(){
btMusica = (Button)findViewById(R.id.btmusica);
btMusica.setOnClickListener((OnClickListener) this);
btArtista = (Button)findViewById(R.id.btartista);
btArtista.setOnClickListener((OnClickListener) this);
btAlbum = (Button)findViewById(R.id.btalbuns);
btAlbum.setOnClickListener((OnClickListener) this);
btVideo= (Button)findViewById(R.id.btvideo);
btVideo.setOnClickListener((OnClickListener) this);
}
public void onClick(View v){
if (v.getId()==R.id.btmusica){
Intent intent = new Intent(MainActivity.this, ListaMusicaActivity.class);
}else if (v.getId()==R.id.btartista){
Intent intent = new Intent(MainActivity.this, ListaArtistaActivity.class);
}else if (v.getId()==R.id.btalbuns){
Intent intent = new Intent(MainActivity.this, ListaAlbumActivity.class);
}else if (v.getId()==R.id.btvideo){
Intent intent = new Intent(MainActivity.this, ListaVideoActivity.class);
}
}
XML:
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/btmusica"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:text="Musicas" />
<Button
android:id="#+id/btartista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btmusica"
android:layout_alignBottom="#+id/btmusica"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:text="Artistas" />
<Button
android:id="#+id/btalbuns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="66dp"
android:text="Albuns" />
<Button
android:id="#+id/btvideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btalbuns"
android:layout_alignBottom="#+id/btalbuns"
android:layout_alignRight="#+id/btartista"
android:text="Videos" />
and AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tcc.etec.FlagPlayer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".tcc.etec.FlagPlayer.activity_main"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".tcc.etec.FlagPlayer.ListaMusicaActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".tcc.etec.FlagPlayer.MusicPlayerActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Try to replacing your:
public void acaobutton(){
btMusica = (Button)findViewById(R.id.btmusica);
btMusica.setOnClickListener((OnClickListener) this);
btArtista = (Button)findViewById(R.id.btartista);
btArtista.setOnClickListener((OnClickListener) this);
btAlbum = (Button)findViewById(R.id.btalbuns);
btAlbum.setOnClickListener((OnClickListener) this);
btVideo= (Button)findViewById(R.id.btvideo);
btVideo.setOnClickListener((OnClickListener) this);
}
To:
public void acaobutton(){
btMusica = (Button)findViewById(R.id.btmusica);
btMusica.setOnClickListener((OnClickListener) getBaseContext());
btArtista = (Button)findViewById(R.id.btartista);
btArtista.setOnClickListener((OnClickListener) getBaseContext());
btAlbum = (Button)findViewById(R.id.btalbuns);
btAlbum.setOnClickListener((OnClickListener) getBaseContext());
btVideo= (Button)findViewById(R.id.btvideo);
btVideo.setOnClickListener((OnClickListener) getBaseContext());
}

Categories

Resources