简体   繁体   中英

Android - Toast button not appearing

After reading about Android's Toast class, I had to try it out.
I added a button to my layout, followed the instructions on this page, and added an OnClickListener to my button that would call the toast.
The problem now is that when I debug the app, the button doesn't appear on the view.

Do I have something where it's not supposed to be? (Additional information available upon request)

Code:
activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relLayout"
    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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

MainActivity.java

package com.joseph.toasttest;
import com.joseph.toasttest.R;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {
    Context context = getApplicationContext();
    CharSequence text = "Test";
    int duration = Toast.LENGTH_SHORT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnToast = (Button) findViewById(R.id.button1);
        btnToast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastMe(v);
            }
        });
    }

    private void toastMe(View v) {
        Toast.makeText(context, text, duration).show();
    }
}

try this :

 btnToast.setOnClickListener(new OnClickListener() {

  @Override

  public void onClick(View v) {

  Toast msg = Toast.makeText(MainActivity.this, text, duration );



 msg.show();

You can't use the application context for UI stuff, like creating dialogs, toasts or launching activities. Just use the activity as a context instead.

Different contexts in Android, and what they can and can't be used for.

Just guessing here, but I'd start removing those paddings in your RelativeLayout . You may be running the app in a device in which values of

@dimen/activity_vertical_margin 

and/or

@dimen/activity_horizontal_margin

are ' moving ' your button out of the screen. You can also try aligning the button in the center of the view.-

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Button" />

You must create your context inside onCreate() method:

package com.joseph.toasttest;
import com.joseph.toasttest.R;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {
    Context context = getApplicationContext();
    CharSequence text = "Test";
    int duration = Toast.LENGTH_SHORT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnToast = (Button) findViewById(R.id.button1);
    btnToast.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            toastMe(v);
        }
    });
    }

    private void toastMe(View v) {
    Toast.makeText(context, text, duration).show();
    }
}

I got it to work by removing the context and duration variables altogether and setting them manually inside my Toast method.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnToast = (Button) findViewById(R.id.button1);
        btnToast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastMe(v);
            }
        });
    }

    private void toastMe(View v) {
        Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_SHORT).show();
    }
}

This should work:

package com.joseph.toasttest;
import com.joseph.toasttest.R;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {
    CharSequence text = "Test";
    int duration = Toast.LENGTH_SHORT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnToast = (Button) findViewById(R.id.button1);
    btnToast.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            toastMe(this);
        }
    });
    }

    private void toastMe(Context context) {
    Toast.makeText(context, text, duration).show();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM