0

I've a problem with my Android Aplication, what i'm trying to do is that pressing the ToggleButton, the aplication writes in a File Hello every two seconds (What I really wanted is that save in a file the gps position every 2 seconds), the ToggleButton Have id: boto1.

This is the MainActivity.java:

package com.example.gasquefabaixada;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;


public class MainActivity extends ActionBarActivity {

    private ToggleButton Boto1;
    @Override



    protected void onCreate(Bundle savedInstanceState) {

        ToggleButton bt = (ToggleButton) findViewById(R.id.boto1);
        bt.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked)
                {
                    BufferedWriter writer = null;
                    try {
                        writer = new BufferedWriter(new FileWriter("mnt/sdcard/andromina.txt"));
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    while(isChecked==true)
                    {


                        Thread.currentThread();
                        try {

                            writer.write("HELLO");
                            writer.newLine();
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    try {
                        writer.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                else
                {

                }

            }
        });

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @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);
    }
}

The program crashes!!!

Thank you for all!!

david.t_92
  • 1,971
  • 1
  • 11
  • 15
  • Welcome to StackOverflow, is a very good practice posting the messages displayed in LogCat, please post your error message!:) – Jorgesys Aug 29 '14 at 18:10

3 Answers3

0

Im pretty sure that you have a NullPointerException, so add the two lines after the onCreate() method

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

and then your code will find the element boto1 inside your activity_main layout

  ToggleButton bt = (ToggleButton) findViewById(R.id.boto1);
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

You have to give these two lines at the start of onCreate()

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Again, Are you sure that your ToggleButton is in activity_main.xml. Check if it is in fragment_main.xml..If it is in fragment_main.xml you are going to get a NullPointerException for sure.

While extending ActionBarActivity, the right practice is to inflate the view using the onCreateView() and the UI elements should be placed inside fragments (eg.fragment_main.xml) .In your code you are doing it in onCreate() itself.

So your onCreateView() should look like

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_main.xml, null);
    ToggleButton bt = (ToggleButton) findViewById(R.id.boto1);
    bt.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(isChecked)
            {
                BufferedWriter writer = null;
                try {
                    writer = new BufferedWriter(new FileWriter("mnt/sdcard/andromina.txt"));
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                while(isChecked==true)
                {


                    Thread.currentThread();
                    try {

                        writer.write("HELLO");
                        writer.newLine();
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                try {
                    writer.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else
            {

            }

        }
    });
    return view;
}

and remove those lines from onCreate()

Lal
  • 14,726
  • 4
  • 45
  • 70
  • Eclipse says me: fragment_main cannot be resolved or is not a field. in: View view=inflater.inflate(R.layout.fragment_main.xml, null); – david.t_92 Aug 29 '14 at 18:46
  • By default eclipse creates two xml files ie activity_main.xml and fragment_main.xml (android 4.4) – Lal Aug 29 '14 at 18:47
0

You called findViewById(R.id.boto1) before calling setContentView(R.layout.activity_main) so that it couldn’t find the view. I think the crash may be NullPointerException as ToggleButton bt is definitely null since any layout or view are set yet. :)

Try this:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ToggleButton bt = (ToggleButton) findViewById(R.id.boto1);
    bt.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if(isChecked) {
                ...
            } else {
                ...
            }
        }
    });
}