lundi 31 août 2015

App was Force close when button Clicked

actually i had button that clicked it will be random his position every second so i declare the code like this

this mainactivity class

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;



public class MainActivity extends Activity {

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

         buttonblack = (Button)findViewById(R.id.button1);

         buttonblack.setOnClickListener(new View.OnClickListener() {

                     @Override
                     public void onClick(View view) {
                         startRandomButton(buttonblack);    
                         }               
                 });

         startRandomButton(buttonblack);    

    }

    public static Point getDisplaySize(@NonNull Context context) {
        Point point = new Point();
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getSize(point);
        return point;
    }

    private void setButtonRandomPosition(final Button button){
        int randomX = new Random().nextInt(getDisplaySize(this).x);
        int randomY = new Random().nextInt(getDisplaySize(this).y);

        button.setX(randomX);
        button.setY(randomY);
    }

    private void startRandomButton(Button button) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                setButtonRandomPosition(buttonblack);
            }
        }, 1000, 1000);//Update button every second
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

and this is the xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:ads="http://ift.tt/GEGVYd"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

     <RelativeLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         android:layout_margin="30dp"
         android:background="@drawable/backgroundblankred" >

         <Button
             android:id="@+id/button1"
             android:layout_width="100dp"
             android:layout_height="100dp"
             android:background="@drawable/eeclevelfirstblackbuton" />

     </RelativeLayout>

</RelativeLayout>

when the app is open in a second will force close

can anyone help me to fix this?

and second, can anyone help me with explain code to that make the button stop random? because i dont know to make the button stop lol.

thanks in advance.




Aucun commentaire:

Enregistrer un commentaire