Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-将字符串传递给Asynctask?_Android_String_Android Asynctask_Parameter Passing - Fatal编程技术网

Android-将字符串传递给Asynctask?

Android-将字符串传递给Asynctask?,android,string,android-asynctask,parameter-passing,Android,String,Android Asynctask,Parameter Passing,与我之前关于ANR问题()的问题相关 我试着按照受访者的建议使用AsyncTask,但现在我不知所措 我需要将一个字符串从我的菜单活动传递给一个异步任务,但这真的让我很困惑。我已经搜索和学习了5个小时,但仍然做不到 以下是我的代码片段: @Override public boolean onCreateOptionsMenu(Menu menu) { /** Create an option menu from res/menu/items.xml */ getMenuInfla

与我之前关于ANR问题()的问题相关

我试着按照受访者的建议使用AsyncTask,但现在我不知所措

我需要将一个字符串从我的菜单活动传递给一个异步任务,但这真的让我很困惑。我已经搜索和学习了5个小时,但仍然做不到

以下是我的代码片段:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    /** Create an option menu from res/menu/items.xml */
    getMenuInflater().inflate(R.menu.items, menu);

    /** Get the action view of the menu item whose id is search */
    View v = (View) menu.findItem(R.id.search).getActionView();

    /** Get the edit text from the action view */
    final EditText txtSearch = ( EditText ) v.findViewById(R.id.txt_search);

    /** Setting an action listener */
    txtSearch.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

            final EditText txtSearch = ( EditText ) v.findViewById(R.id.txt_search);
            String enhancedStem = txtSearch.getText().toString();
            TextView databaseOutput = (TextView)findViewById(R.id.textView8);

            new AsyncTaskRunner().execute();
            // should I put "enhancedStem" inside execute?
            }
          });
return super.onCreateOptionsMenu(menu);
}
以下是异步部分:已更新

public class AsyncTaskRunner extends AsyncTask<String, String, String> {
      String curEnhancedStem;
      private ProgressDialog pdia;

      public AsyncTaskRunner (String enhancedStem)
      {
           this.curEnhancedStem = enhancedStem;
      }

      @Override
      protected void onPreExecute() {
       // Things to be done before execution of long running operation. For
       // example showing ProgessDialog
          super.onPreExecute();
          pdia = ProgressDialog.show(secondactivity.this, "" , "Searching for words");
      }



    @Override
    protected String doInBackground(String... params) {

        if(curEnhancedStem.startsWith("a"))
        {
            String[] wordA = getResources().getStringArray(R.array.DictionaryA);
            String delimiter = " - ";
            String[] del;
            TextView databaseOutput1 = (TextView)findViewById(R.id.textView8);
            for (int wordActr = 0; wordActr <= wordA.length - 1; wordActr++)
            {
                String wordString = wordA[wordActr].toString();
                del = wordString.split(delimiter);

                if (curEnhancedStem.equals(del[0]))
                {
                    databaseOutput1.setText(wordA[wordActr]);
                    pdia.dismiss();
                    break;
                }
                else
                    databaseOutput1.setText("Word not found!");
            }
        }

       return null;
      } 


      @Override
      protected void onProgressUpdate(String... text) {
       // Things to be done while execution of long running operation is in
       // progress. For example updating ProgessDialog

      }

      @Override
      protected void onPostExecute(String result) {
       // execution of result of Long time consuming operation
      }
}
公共类AsyncTaskRunner扩展了AsyncTask{
线固化剂;
私家侦探;
公共AsyncTaskRunner(字符串增强项)
{
this.curEnhancedStem=增强型tem;
}
@凌驾
受保护的void onPreExecute(){
//在执行长时间运行的操作之前要做的事情
//显示ProgesDialog的示例
super.onPreExecute();
pdia=ProgressDialog.show(secondactivity.this,“,”搜索单词“);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
如果(currenhancedstem.startsWith(“a”))
{
String[]wordA=getResources().getStringArray(R.array.DictionaryA);
字符串分隔符=“-”;
字符串[]del;
TextView数据库输出1=(TextView)findViewById(R.id.textView8);

对于(int-wordActr=0;wordActr这就是问题所在,它们是您在中声明它们的方法的本地类,然后您正在声明一个没有访问它们的
AsyncTask
类。如果
AsyncTask
是菜单活动的内部类,那么您可以将它们声明为成员变量

public class MenuActivity extends Activity
{
     String enhancedStem;
     ....
如果它是一个单独的类,那么您可以在
Async
类中创建一个构造函数,并将变量传递给构造函数

public class AsyncTaskRunner extends AsyncTask<String, String, String> {
  String curEnhancedStem;
  private ProgressDialog pdia;

  public void AsyncTaskRunner (String variableName)
{
     this.curEnhancedStem = variableName;
}
此外,您不能在
doInBackground
中执行
UI
操作,因此这需要在
活动
类中更改,或者在
Async
类中更改其他方法,例如
onPostExecute()
如果它是一个内部类。否则,您可以将一个值传递回您的菜单活动,以更新您的
TextView

编辑

您仍在尝试使用此选项更改
doInBackground()
中的
UI

TextView databaseOutput1 = (TextView)findViewById(R.id.textView8);

然后再次调用
setText()
。这需要放在
onPostExecute()
中,但随后需要将
TextView
的引用传递给
AsyncTask
。您只需将
onPostExecute()中要设置文本的
String
并在您的
活动中设置它。希望这对您有所帮助这就是问题所在,它们是您在其中声明它们的方法的本地对象,那么您正在声明一个没有访问它们的
AsyncTask
类。如果
AsyncTask
是菜单活动的内部类,那么您可以将它们声明为成员variab莱斯

public class MenuActivity extends Activity
{
     String enhancedStem;
     ....
如果它是一个单独的类,那么您可以在
Async
类中创建一个构造函数,并将变量传递给构造函数

public class AsyncTaskRunner extends AsyncTask<String, String, String> {
  String curEnhancedStem;
  private ProgressDialog pdia;

  public void AsyncTaskRunner (String variableName)
{
     this.curEnhancedStem = variableName;
}
此外,您不能在
doInBackground
中执行
UI
操作,因此这需要在
活动
类中更改,或者在
Async
类中更改其他方法,例如
onPostExecute()
如果它是一个内部类。否则,您可以将一个值传递回您的菜单活动,以更新您的
TextView

编辑

您仍在尝试使用此选项更改
doInBackground()
中的
UI

TextView databaseOutput1 = (TextView)findViewById(R.id.textView8);
然后再次调用
setText()
。这需要放在
onPostExecute()
中,但随后需要将
TextView
的引用传递给
AsyncTask
。您只需将
onPostExecute()中要设置文本的
String
并在您的
活动中设置它。希望这有帮助

  • AsyncTaskRunner
    类创建构造函数
  • 上下文
    (活动上下文)和
    数据库输出
    文本视图
  • 作为参数传递给
    AsyncTaskRunner
    类构造函数
  • 将对这两个对象的引用保存在
    AsyncTaskRunner
  • enhancedStem
    传递给
    execute()
    方法
  • 使用传递给构造函数的
    上下文
    作为
    ProgessDialog.show()的第一个参数
  • 无法从
    doInBackground()
    方法访问
    databaseOutput
    。只能在运行在UI线程上的
    onPostExecute()
    中访问它。因此,请使用传递给构造函数的
    databaseOutput
    引用更新
    onPostExecute()中的文本视图
    方法
  • 请注意,从
    doInBackground()
    方法返回的任何内容都将作为
    onPostExecute()
    方法的参数提供给您

    请参阅

    我建议您传递所需的数据,而不是使用封闭类访问它-这使您的
    ASyncTaskRunner
    更加灵活,通常是更好的做法

  • AsyncTaskRunner
    类创建构造函数
  • 上下文
    (活动上下文)和
    数据库输出
    文本视图
  • 作为参数传递给
    AsyncTaskRunner
    类构造函数
  • 将对这两个对象的引用保存在
    AsyncTaskRunner
  • enhancedStem
    传递给
    execute()
    方法
  • 使用传递给构造函数的
    上下文
    作为
    ProgessDialog.show()的第一个参数
  • 无法从
    doInBackground()
    方法访问
    databaseOutput
    。只能访问