【问题标题】:Problems In MainActivity.java Trying to use ToogleButtonMainActivity.java 中的问题尝试使用切换按钮
【发布时间】:2014-08-29 18:02:25
【问题描述】:

我的 Android 应用程序有问题,我想做的是按下 ToggleButton,应用程序每两秒写入一个文件 Hello(我真正想要的是将 gps 位置保存在文件中每 2 秒),ToggleButton 有 id:boto1。

这是 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);
    }
}

程序崩溃!!!

谢谢大家!!

【问题讨论】:

  • 欢迎使用 StackOverflow,发布 LogCat 中显示的消息是一个非常好的做法,请发布您的错误消息!:)

标签: android compiler-errors gps togglebutton main-activity


【解决方案1】:

你必须在onCreate()的开头给出这两行

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

再次,您确定您的ToggleButtonactivity_main.xml 中。检查它是否在fragment_main.xml..如果它在fragment_main.xml,你肯定会得到NullPointerException

在扩展ActionBarActivity 时,正确的做法是使用onCreateView() 扩展视图,并且UI 元素应放置在片段内(例如,fragment_main.xml)。在您的代码中,您在onCreate() 中执行此操作自己。

所以你的onCreateView() 应该是这样的

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

并从onCreate() 中删除这些行

【讨论】:

  • Eclipse 告诉我:fragment_main 无法解析或不是字段。在:视图视图=inflater.inflate(R.layout.fragment_main.xml, null);
  • 默认情况下eclipse会创建两个xml文件即activity_main.xml和fragment_main.xml (android 4.4)
【解决方案2】:

您在调用setContentView(R.layout.activity_main) 之前调用了findViewById(R.id.boto1),因此它找不到视图。我认为崩溃可能是NullPointerException,因为ToggleButton bt 绝对为空,因为尚未设置任何布局或视图。 :)

试试这个:

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

【讨论】:

    【解决方案3】:

    我很确定你有一个NullPointerException,所以在onCreate() 方法之后添加两行

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

    然后您的代码将在您的 activity_main 布局中找到元素 boto1

      ToggleButton bt = (ToggleButton) findViewById(R.id.boto1);
    

    【讨论】:

      猜你喜欢
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-03
      • 2021-09-16
      • 1970-01-01
      相关资源
      最近更新 更多