我的Android应用程序存在问题,我试图做的是按下ToggleButton,应用程序每两秒写一次文件Hello(我真正想要的是保存在每隔2秒发送一次gps位置),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);
}
}
程序崩溃!!!
谢谢大家!!
答案 0 :(得分:0)
我很确定你有一个NullPointerException
,所以在onCreate()
方法之后添加两行
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
然后您的代码会在boto1
布局
activity_main
ToggleButton bt = (ToggleButton) findViewById(R.id.boto1);
答案 1 :(得分:0)
您必须在onCreate()
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
同样,您确定ToggleButton
位于activity_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()
答案 2 :(得分:0)
您在致电findViewById(R.id.boto1)
之前致电setContentView(R.layout.activity_main)
,以便无法找到该视图。我认为崩溃可能是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 {
...
}
}
});
}