gpt4 book ai didi

android - MainActivity.java中的问题,尝试使用切换按钮

转载 作者:行者123 更新时间:2023-12-02 11:03:48 25 4
gpt4 key购买 nike

我的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);
}
}

程序崩溃!!!

感谢你所做的一切!!

最佳答案

您必须在onCreate()的开头加上这两行

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

同样,确定您的 ToggleButtonactivity_main.xml中。检查它是否在 fragment_main.xml中。如果它在 fragment_main.xml中,您将肯定会得到一个 NullPointerException

扩展 ActionBarActivity时,正确的做法是使用 onCreateView()扩展 View ,并将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()中删除这些行

关于android - MainActivity.java中的问题,尝试使用切换按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25573791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com