Android服务与方法错误
我正在尝试调用一个名为Android服务与方法错误,android,methods,service,Android,Methods,Service,我正在尝试调用一个名为GetText()的方法从MainActivity更改版面上的TextView。我使用服务类调用此方法,每次调用此方法时,我的程序都会完全崩溃,并出现致命异常错误 //主要活动 package com.example.modulefour; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import andr
GetText()的方法
从MainActivity
更改版面上的TextView
。我使用服务类调用此方法,每次调用此方法时,我的程序都会完全崩溃,并出现致命异常
错误
//主要活动
package com.example.modulefour;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Button BtnStart, BtnStop;
EditText Edt;
TextView one;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BtnStart = (Button)findViewById(R.id.button1);
BtnStop = (Button) findViewById(R.id.button2);
Edt = (EditText) findViewById(R.id.editText1);
one = (TextView) findViewById(R.id.textView1);
}
public void GetText()
{
String Text = this.Edt.getText().toString();
this.one.setText(Text);
}
public void StartService(View v){
//start Service
startService(new Intent(getBaseContext(), ServiceAdapter.class));
}
public void StopService(View v){
//stop service
GetText();
}
@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);
}
}
//服务适配器
package com.example.modulefour;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class ServiceAdapter extends Service {
MainActivity main;
FileWriter fw;
BufferedWriter bw;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
this.main.GetText();
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
//write EditText to text file
return START_STICKY;
}
@Override
public void onDestroy()
{
super.onDestroy();
Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}
}
服务类中的
main
没有设置在任何位置,因此,如果this.main.GetText()
您将得到一个NullPointerException
要了解您想要做什么,请查看如何在活动和服务之间进行通信。主要是,您必须绑定服务或使用某种广播方式。我尝试使用此绑定教程,但它并不真正适用于我试图执行的操作,如上图所示。是否有其他教程,你可以告诉我,这将工作?谢谢。您应该先阅读有关该主题的官方文件: