我正试图在Android上实现这个代码,我是一个菜鸟

时间:2015-01-17 16:08:05

标签: java android popupwindow

我正在尝试为学校制作应用程序,每次按下按钮时我都想制作一个弹出框。

但后来我收到了错误:

  

预期的课程或包

来自这行代码:

showPopUp.OnClickListener

第25行

public class MainActivity extends ActionBarActivity {
@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button showPopUp = (Button) findViewById(R.id.button);
    showPopUp.OnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            showSimplePopUp();
        }
    });
}

private void showSimplePopUp() {

    AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
    helpBuilder.setTitle("Opening Line");
    helpBuilder.setMessage("You popped my jingles up");
    helpBuilder.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // Do nothing but close the dialog
                }
            });

    // Remember, create doesn't show the dialog
    AlertDialog helpDialog = helpBuilder.create();
    helpDialog.show();
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

2 个答案:

答案 0 :(得分:2)

看起来像拼写错误,但应该是:

Button showPopUp = (Button) findViewById(R.id.button);
showPopUp.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v) {
        showSimplePopUp();
    }
});

答案 1 :(得分:0)

你可以试试这个:

private AlertDialog showSimplePopUp() {

    AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
    helpBuilder.setTitle("Opening Line");
    helpBuilder.setMessage("You popped my jingles up");
    helpBuilder.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // Do nothing but close the dialog
                    dialog.dismiss();
                }
            });

    // Remember, create doesn't show the dialog
    helpBuilder.show();
    return helpBuilder.create();
}

希望它有所帮助!!!