Frage

Hello im trying to use onclicklistener along with onmenuselectlistener in my mainactivity class and the program runs but selecting the popup menu opions dont work they wont set the text to want it want anyone have any idea's? I know i did not implement onMenuSelectListener and maybe thats my problem if it is is there any other way to makes this work?

Here is my code:

public class MainActivity extends Activity implements OnClickListener {
// init variables
Handler uiHandler;
EditText cl;
TextView info;
Button enter;
Button line;
Button arc;
DrawingUtils callDU = new DrawingUtils();
DrawingTools callDT = new DrawingTools();
EditTools callET = new EditTools();
Conversion callConversion = new Conversion();
GLSurfaceView mGLSurface;
String Tag = "Debug";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGLSurface = new GLSurfaceView(this);
    mGLSurface.setRenderer(new BasicRenderer());
    setContentView(R.layout.canvas);
    FrameLayout v = (FrameLayout) findViewById(R.id.canvas);
    v.addView(mGLSurface);

    // init views and buttons
    info = (TextView) findViewById(R.id.info);
    enter = (Button) findViewById(R.id.enter);
    line = (Button) findViewById(R.id.line);
    arc = (Button) findViewById(R.id.arc);
    cl = (EditText) findViewById(R.id.cl);

    /*
     * Handler for Main Thread uiHandler = new Handler() { public void
     * handleMessage(Message msg) { switch (msg.what) {
     * 
     * } Bundle bundle = msg.getData(); String string1 =
     * bundle.getString("P1Key"); String string2 =
     * bundle.getString("P2Key"); info.setText(string1);
     * info.setText(string2); } };
     */

}

@Override
public void onClick(View v) {
    switch (v.getId()) {

    case R.id.enter:

        break;
    case R.id.line:

        break;
    case R.id.arc:

        break;

    }

};

public void CreatePopupMenu(View v) {
    PopupMenu mypopupmenu = new PopupMenu(this, v);
    MenuInflater inflater = mypopupmenu.getMenuInflater();
    inflater.inflate(R.menu.filemenu, mypopupmenu.getMenu());
    mypopupmenu.show();
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.newCanas:
        info.setText("New");
        Log.d(Tag, "New was clicked");
        break;
    case R.id.open:
        break;
    case R.id.save:
        break;
    }
    return super.onMenuItemSelected(featureId, item);

}

}

War es hilfreich?

Lösung

It doesn't look like you're attaching onClickListeners to anything. What that means is you're saying "whenever the onClick event is fired with me as the target, perform this action". But then you're never making yourself a target. Try adding the following code to your onCreate.

arc.setOnClickListener(this);
line.setOnClickListener(this);
enter.setOnClickListener(this);

The same thing happens with the PopupMenu it appears. Try adding mypopupmenu.addOnMenuItemSelectListener(this) right after you inflate the layout for your menu.

Andere Tipps

Jonathan is right. You should add .setOnClickListener(this); to each of the buttons added after you create them.

For the menu items though, you have to do the following:

1) Create a layout with the items on your menu and store it in your res/menu/ directory.

Example: main.xml

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

</menu> 

2) Override the method called: onCreateOptionsMenu() to populate the items in the menu.

Example:

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;
}

3) Override the method onOptionsItemSelected() to do whatever you want with a switch like you were doing with the actionListeners.

4) Additionally, you could also override the method onPrepareOptionsMenu() which is the same as the previous one, but it is called every time the menu opens.

Good luck

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top