Music don't start on button click - java

When I go to install the app on the device, I click the button but nothing happen.
I want to make device play tosse.acc from res/raw when I click the button...
Ok resolved the problem :) thanks for answers guys :D ( i have changed the name of the song from tosse to toxxe)
public void tosse1(View v) {
Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.toxxe);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
Here my code:
public void tosse1(View v) {
Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp = new MediaPlayer();
mp.create(this, R.raw.tosse);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Resume the music player
mp.start();
}
});
}
activity_main:
<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="com.example.tosseapp.app.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="tosse1" />
</RelativeLayout>
AndroidManifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<activity
android:name="com.example.tosseapp.app.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>
</manifest>

Declare your mp as private variable in your class:
public class PlayaudioActivity extends Activity {
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopPlaying();
mp = MediaPlayer.create(PlayaudioActivity.this, R.raw.tosse);
mp.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}

Try getting a Mediaplayer Instance like this:
final MediaPlayer mp = MediaPlayer.create(this, R.raw.tosse);
should solve your problem, because it also prepares the MediaPlayer, see documentation:
public static MediaPlayer create (Context context, int resid) Added in API level 1
Convenience method to create a MediaPlayer for a given
resource id. On success, prepare() will already have been called and
must not be called again.
When done with the MediaPlayer, you should call release(), to free the
resources. If not released, too many MediaPlayer instances will result
in an exception.

Related

Not sure if EditText or KeyEvent are not listening or is an XML problem

I'm trying to make a game but I need the user to fill in the username before jumping from Main Activity to Activity 2.
If the username is empty, itdoesn't do anything.
If the username has at least one letter, it launches second activity.
In my case it never works and I don't know what I'm doing wrong.
This is my code.
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText et1;
TextView tv1;
String usuario;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = findViewById(R.id.et1);
tv1 = findViewById(R.id.tv1);
/*Button next = findViewById(R.id.boton);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MainActivity2.class);
startActivity(myIntent);
}
});*/
et1.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
usuario = et1.getText().toString();
if(et1.getText().toString().trim().length() > 0) {
if((event.getAction() == KeyEvent.ACTION_DOWN) && (event.getAction() == KeyEvent.KEYCODE_ENTER)) {
startActivity2();
}
}
return false;
}
});
}
public boolean startActivity2(){
Intent myIntent = new Intent(this, MainActivity2.class);
startActivity(myIntent);
return false;
}
And the activity_main.xml
<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"
tools:context=".MainActivity">
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/texto"
android:layout_centerVertical="true"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:background="#ff8200"
android:layout_marginEnd="50dp"
android:layout_marginStart="50dp" />
<Button
android:id="#+id/boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="jump"
android:layout_below="#+id/tv1"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/et1"
android:ems="7"
android:inputType="text"
android:maxLines="1"
android:maxLength="11"
android:layout_toRightOf="#+id/tv1"
android:layout_centerInParent="true"/>
</RelativeLayout>
I tried for the ifs !isEmpty(), trim(), !equals and absolutely nothing.
Here is the manifest, if necesarry.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.juegocartas3">
<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/Theme.JuegoCartas3">
<activity
android:name=".MainActivity3"
android:exported="false" />
<activity
android:name=".MainActivity2"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Try this-
public class MainActivity extends AppCompatActivity {
EditText et1;
TextView tv1;
String usuario;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = findViewById(R.id.et1);
tv1 = findViewById(R.id.tv1);
et1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE) {
usuario = et1.getText().toString();
if (usuario.trim().length() > 0) {
startActivity2();
} else {
Toast.makeText(getApplicationContext(), " Please add User name", Toast.LENGTH_LONG).show();
}
return true;
}
return false;
}
});
public void startActivity2() {
Intent myIntent = new Intent(this, MainActivity2.class);
startActivity(myIntent);
}}

Run Time Main Activity not come after Splash Activity and app close

Everything in my code looks good in both the .java and .xml files (according to the software) because I have no errors yet every time I click the app icon in the emulator, it brings up the splash screen then the app exits without showing my main activity. Here's my code so far:
SplashActivity.java
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//hide title bar of activity
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//Making activity full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
int t =3000; //time for spash screen 3000 means 3sec
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
}
},t);
}
}
SplashActivity.xml
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="#drawable/icon"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
MainActivity.java:
public class MainActivity extends AppCompatActivity
{
String webAdress ="https://www.google.com/";
WebView webView;
FrameLayout frameLayout;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = (WebView) findViewById(R.id.webView);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
webView.setWebViewClient(new HelpClient());
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
frameLayout.setVisibility(View.VISIBLE);
progressBar.setProgress(newProgress);
setTitle("Loading..."); //when url is loading set this on actionbar
if (newProgress == 100) {
frameLayout.setVisibility(View.GONE); // Hide progress bar when page is loaded
setTitle(view.getTitle()); //get amd set title of opend page
}
super.onProgressChanged(view, newProgress);
}
});
webView.getSettings().setJavaScriptEnabled(true); //enable javaScript
//check internet connection
if (haveNetworkConnection()) {
webView.loadUrl(webAdress);
} else {
Toast.makeText(this, "No internet Connection", Toast.LENGTH_SHORT).show();
}
progressBar.setProgress(0);
}
private class HelpClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
frameLayout.setVisibility(View.VISIBLE);
return true;
}
}
private boolean haveNetworkConnection(){
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
for(NetworkInfo ni : networkInfos){
if(ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if(ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//check if the event was the back button and if there's history
if ((keyCode==KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
webView.goBack();
return true;
}
//if it was the back or there is no web page history, bubble up to the default system behaviour
return super.onKeyDown(keyCode, event);
}
}
MainActivity.xml:
<!--Progressbar-->
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="3dip"
android:background="#android:color/transparent">
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="match_parent"
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="7dp"
android:layout_gravity="top"
android:layout_marginTop="-3dp"
android:progressDrawable="#drawable/custom_progress"
android:background="#android:color/transparent"
android:progress="20"/>
</FrameLayout>
<!--WebView-->
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</WebView>
Here's my manifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--internet permision-->
<uses-permission android:name="android.permission.INTERNET"/>
<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=".SplashActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity">
</activity>
</application>
Any hints would be greatly appreciated, thank you!
The problem is in onCreate method of MainActivity, you do not provide any UI (xml layout file), as a result, the app cannot find view elements to init, set listeners, etc. I guess you can find an exception in logcat when your app close.
Solution: Provide a layout xml file for MainActivity
public class MainActivity extends AppCompatActivity {
String webAdress ="https://www.google.com/";
WebView webView;
FrameLayout frameLayout;
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Add this line
webView = (WebView) findViewById(R.id.webView);
...
}
}
try use
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Your code is not so good. If you want to show splashscreen only when app start I recommend using the following code:
AndroidManifest.xml
<activity android:name=".view_layer.activities.SplashScreenActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
styles.xml
<style name="SplashTheme" parent="#android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:windowBackground">#drawable/splash</item>
</style>
SplashscreenActivity.java
public class SplashscreenActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
More: https://android.jlelse.eu/the-complete-android-splash-screen-guide-c7db82bce565

resume button not working to go last activity

have 4 activity : Main Activity,p1,p2,p3
my resume button not working . i want when i click in resume button app open my last activity .
for example : when in p2 click go to main , then in main when click resume , p2 open . here is my code :
Main Activity :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
Button resume = (Button) findViewById(R.id.resume);
Button next = (Button) findViewById(R.id.next);
Button exit = (Button) findViewById(R.id.exit);
resume.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences myPref = getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
String lastActivity= myPref.getString("lastactivity","");
try {
Intent fooActivity = new Intent(MainActivity.this,Class.forName(lastActivity));
startActivity(fooActivity);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, p1.class);
startActivity(intent);
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
});
}
}
XML layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="resume"
android:layout_width="wrap_content"
android:id="#+id/resume"
android:layout_height="wrap_content" />
<Button
android:text="next"
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="exit"/>
</LinearLayout>
p1 :
public class p1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.p1);
Button next = (Button) findViewById(R.id.next);
Button home=(Button)findViewById(R.id.home);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(p1.this, p2.class);
startActivity(intent);
}
});
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(p1.this, MainActivity.class);
startActivity(intent);
}
});
}
private void storeCurrentActivity(){
SharedPreferences myPref =getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myPref.edit();
editor.putString("lastactivity", this.getClass().getSimpleName());
editor.commit();
}
#Override
public void onResume(){
super.onResume();
storeCurrentActivity();
}
}
XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/next"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="page 1"/>
<Button
android:text="go in main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/home"/>
</LinearLayout>
and p2,p3 like p1
and this my manifast :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.er.myapplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".p1"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".p2"
android:label="#string/title_activity_p2"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".p3"
android:label="#string/title_activity_p3"
android:theme="#style/AppTheme.NoActionBar"/>
</application>
</manifest>
you have to do below things to achieve what you want
whenever you open activity you have to save previous activity name in prefrance.
when you press resume button get activity name from prefrance and open activity using below code
Intent intent = new Intent();
intent.setClassName("com.android.browser","com.android.BrowserActivity");
context.startActivity(intent);
// clear previous activity before start
when you app relaunch app then do step 2 again in main Activity
in this type of approch you have to maintain flow of application using singletone or clear stack before launch activity. because you have to handle backbutton

Button displays toast and starts service

I have two buttons (start/stop) that when clicked need to have a Toast pop up saying what has happened to the Service. Start = popup of "service has started" and the service actually starts. The service isn't finished and will be grabbing some GPS info later on.
Anyway, none of my Toasts show up and I'm hoping I'm not missing something obvious.
Main (Activity)
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBtn = (Button) findViewById(R.id.startButton);
startBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startService(new Intent(getBaseContext(), ParseService.class));
}
});
Button stopBtn = (Button) findViewById(R.id.startButton);
stopBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopService(new Intent(getBaseContext(), ParseService.class));
}
});
}
}
ParseService
public class ParseService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent e, int flags, int startId){
Toast.makeText(this, "Service has Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
#Override
public void onDestroy(){
super.onDestroy();
Toast.makeText(this, "Service has Stopped", Toast.LENGTH_LONG).show();
}
}
activity_main.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"
android:textAlignment="center"
tools:context="${packageName}.${activityClass}" >
<Button
android:id="#+id/startButton"
android:layout_width="150dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="92dp"
android:text="Start" />
<Button
android:id="#+id/stopButton"
android:layout_width="150dp"
android:layout_height="60dp"
android:layout_alignLeft="#+id/startButton"
android:layout_below="#+id/startButton"
android:layout_marginTop="74dp"
android:text="Stop" />
</RelativeLayout>
You are supposed to use the Application's Context from a Service and not the Service's Context.
From your Service:
Toast.makeText(getApplicationContext(), // application Context not 'this'
"Service has Started",
Toast.LENGTH_LONG).show();
Alternatively, you could display them from your Activity before you invoke the Service if you wish but I think where you have the calls currently positioned makes more sense and would reduce repetition later on as long as you always want the Toast displayed.
EDIT:
Ok, after making a quick test app I think I found what might be going wrong for you.
Do you have the Service declared in your AndroidManifest.xml??
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.indivisible.testapp">
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".toasts.ToastActivity"
android:label="#string/title_activity_toast">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Define your Service as below.
In my case the path is:
.../TestApp/app/src/main/java/com/indivisible/testapp/toasts/ToastService.java
Android Studio has a nice auto-complete when you press '.'
-->
<service
android:name=".toasts.ToastService"
android:label="ToastService">
</service>
</application>
</manifest>
It seems like your service is not getting started, make sure the service is declared in the manifest and enabled as below:
<service
android:name=".ParseService"
android:enabled="true" />
Hope this helps

Switch to different Activities onClick

I am new to Android and exploring it at the moment.
I have two Image Buttons which have to load different activities onClick.
ImageButton btn1= (ImageButton)findViewById(R.id.timetable);
btn1.setOnClickListener(btnListener1);
ImageButton btn2= (ImageButton)findViewById(R.id.location);
btn2.setOnClickListener(btnListener2);
private OnClickListener btnListener1 = new OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(getBaseContext(), HelloWorld1.class);
startActivity(myIntent);
}
};
private OnClickListener btnListener2 = new OnClickListener()
{
public void onClick(View view)
{
Intent myIntent2 = new Intent(getBaseContext(), HelloWorld2.class);
startActivity(myIntent2);
}
};
//my manifest ......
<activity android:name="myApp" 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=".HelloWorld1"></activity>
<activity android:name=".HelloWorld2"></activity>
//and my main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="#+id/widget34"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
>
<GridView
android:id="#+id/widget36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:layout_x="110px"
android:layout_y="32px"
android:layout_centerInParent="true">
</GridView>
<ImageButton
android:id="#+id/timetable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="210px"
android:layout_y="142px"
android:background="#drawable/icon2">
</ImageButton>
<ImageButton
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="342px"
android:background="#drawable/icon">
</ImageButton>
This code causes errors, could anyone point where I am going wrong please.
Many thanks in advance.
Let's put aside the finish() method since i dont know what the heck it's doing there :)
Case1: Look careful at your activity xml view file, you might accidentally define your button as Button instead of ImageButton -> Error
Case2: dont use view.getContext(), instead use getBaseContext() or getApplicationContext()
You are calling startActivityForResult() and then immediately calling finish(). Where is the result going to go if the activity is finished?
What behavior do you want and what are you getting instead. The more specific you can be, the better the quality of help you will get.
Try to write something simillar like code below. You can also define first function which will create new activity when button will be pressed.
public class HelloAndroid extends Activity {
private Button button_1;
private Button button_2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialiyeFields();
}
private void initialiyeFields(){
button_1 = (Button)findViewById(R.id.button1);
button_1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(HelloAndroid.this, HelloWord1.class);
startActivity(intent);
}
});
button_2 = (Button)findViewById(R.id.button2);
button_2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(HelloAndroid.this, HelloWord2.class);
startActivity(intent);
}
});
}
}

Categories