尝试调用虚方法' boolean java.lang.String.equals(java.lang.Object)'(我尽我所能。但它没有帮助)

时间:2015-10-14 08:30:28

标签: android

我正在尝试构建我的第一个应用程序,其中有厨师的名字,然后将开启选择VEG / NONVEG的常见活动。然后根据他们选择的选择和厨师,它将引导他们进入菜单。我好像不知道我哪里出错了。

MainActivity.java

      import android.app.Activity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.view.Menu;
      import android.view.MenuInflater;
      import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    public class MainActivity extends Activity implements View.OnClickListener {

    Button sanjeev, vikas, tarla;
    Bundle bundle;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sanjeev= (Button) findViewById(R.id.button1);
        vikas= (Button) findViewById(R.id.button2);
        tarla= (Button) findViewById(R.id.button3);

        sanjeev.setOnClickListener(this);
        vikas.setOnClickListener(this);
        tarla.setOnClickListener(this);
    }

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

    @Override
    public void onClick(View v) {
        if (v==sanjeev){
            Toast.makeText(getApplicationContext(),"Sanjeev was clicked", Toast.LENGTH_SHORT).show();
            intent=new Intent(this,Choice.class);
            intent.putExtra("Veg1", "SanjeevStarter");
            intent.putExtra("NVeg1","SanjeevNStarter");
            startActivity(intent);

        }
        else if (v==vikas){
            Toast.makeText(getApplicationContext(),"Vikas was clicked", Toast.LENGTH_SHORT).show();
            intent=new Intent(this, Choice.class);
            intent.putExtra("Veg2","VikasVeg");
            intent.putExtra("NVeg2","VikasNVeg");
            startActivity(intent);

        }
        else if (v==tarla){
            Toast.makeText(getApplicationContext(),"Tarla was clicked", Toast.LENGTH_SHORT).show();
            intent=new Intent(this,Choice.class);
            intent.putExtra("Veg3", "TarlaVeg");
            intent.putExtra("NVeg3","TarlaNVeg");
            startActivity(intent);
        }

    }
}

Choice.java

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    public class Choice extends Activity implements View.OnClickListener{

    Button button1,  button2;
    Intent i;
    Bundle extras;
    String veg, nveg, vveg, vnveg, tveg, tnveg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choice);
        button1= (Button) findViewById(R.id.button1);
        button2= (Button) findViewById(R.id.button2);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);

        extras=getIntent().getExtras();

            if (extras!=null){
                if (extras.containsKey("Veg1")&& extras.containsKey("NVeg1")){
                    veg=extras.getString("Veg1");
                    nveg=extras.getString("NVeg1");
                }
                else if (extras.containsKey("Veg2") && extras.containsKey("NVeg2")){
                    vveg=extras.getString("Veg2");
                    vnveg=extras.getString("NVeg2");
                }
                else if (extras.containsKey("Veg3") && extras.containsKey("NVeg3")){
                    tveg=extras.getString("Veg3");
                    tnveg=extras.getString("NVeg3");
                }
            }
    }

    @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_sanjeev_1, 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);
    }

    @Override
    public void onClick(View v) {
        try {

            if (v == button1) {
                if (veg.equals("SanjeevStarter")) {
                    Toast.makeText(this, "Veg Was Clicked", Toast.LENGTH_SHORT).show();
                    i = new Intent(Choice.this, SanjeevVegCourse.class);
                    startActivity(i);
                } else if (vveg.equals("VikasVeg")) {
                    Toast.makeText(this, "Veg Was Clicked", Toast.LENGTH_SHORT).show();
                    i = new Intent(Choice.this, VikasVegCourse.class);
                    startActivity(i);
                } else if (tveg.equals("TarlaVeg")) {
                    Toast.makeText(this, "Veg Was Clicked", Toast.LENGTH_SHORT).show();
                    i = new Intent(Choice.this, TarlaVegcourse.class);
                    startActivity(i);
                }
            } else if (v == button2) {
                if (nveg.equals("SanjeevNStarter")) {
                    Toast.makeText(this, "Non-Veg Was Clicked", Toast.LENGTH_SHORT).show();
                    i = new Intent(Choice.this, SanjeevNVCourse.class);
                    startActivity(i);
                } else if (vnveg.equals("VikasNVeg")) {
                    Toast.makeText(this, "Non-Veg Was Clicked", Toast.LENGTH_SHORT).show();
                    i = new Intent(Choice.this, VikasNVcourse.class);
                    startActivity(i);
                } else if (tnveg.equals("TarlaNVeg")) {
                    Toast.makeText(this, "Still working on Non-Veg Menu :-)", Toast.LENGTH_SHORT).show();

                }
            }
        }
        catch (Exception e){
            Toast.makeText(getApplicationContext(),"Fix the error MADARBHAGAT", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案