无法解析符号"显示"

时间:2017-06-21 13:19:18

标签: android

Android编码的新手,并且遇到了我多次尝试修复但无效的错误。

这是我的代码:

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_task:
                Log.d(TAG, "Add a new task");
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    final EditText taskEditText = new EditText(this);
    AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("Add a new task")
            .setMessage("What do you want to do next?")
            .setView(taskEditText)
            .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String task = String.valueOf(taskEditText.getText());
                    Log.d(TAG, "Task to add: " + task);
                }
            })
            .setNegativeButton("Cancel", null)
            .create();
    dialog.show();
}

错误是无法解析符号"显示"

任何建议都表示赞赏,并提前致谢。 :)

2 个答案:

答案 0 :(得分:0)

在这样的函数中创建对话框。并在任何地方调用该函数

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    showDialogBox();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_add_task:
            Log.d(TAG, "Add a new task");
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}
public void showDialogBox()
{
    final EditText taskEditText = new EditText(this);
    AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("Add a new task")
            .setMessage("What do you want to do next?")
            .setView(taskEditText)
            .setPositiveButton("Add", new     DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String task = String.valueOf(taskEditText.getText());
                    Log.d(TAG, "Task to add: " + task);
                }
            })
            .setNegativeButton("Cancel", null)
            .create();
    dialog.show();
}
}

答案 1 :(得分:0)

  

请参考对话框here !!

        Properties properties = new Properties();
        InputStream in = ClassLoader.getSystemResourceAsStream("myfile.properties");
        properties.load(in);
        in.close();
  • 关闭 AlertDialog.Builder类后,您必须调用alert alertDialog.show。