2017-04-05 62 views
-1

我試圖在選擇子活動中的彈出菜單中的項目時更改主要活動的背景顏色。但是,當我運行該應用程序並選擇所述項目時,該應用程序崩潰。這是次活動代碼:選擇彈出菜單項目時發送意圖

public class Modify extends AppCompatActivity { 
Button button, submit; 
EditText edit; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.modify); 
    button = (Button) findViewById(R.id.button); 
    submit = (Button) findViewById(R.id.submit); 
    edit = (EditText) findViewById(R.id.editText); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      PopupMenu pop = new PopupMenu(Modify.this, button); 
      pop.getMenuInflater().inflate(R.menu.menu,pop.getMenu()); 
      pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){ 
       @Override 
       public boolean onMenuItemClick(MenuItem item){ 
        Intent i = new Intent(Modify.this, MainActivity.class); 
        switch (item.getItemId()){ 
         case R.id.red: 
          i.putExtra("color","red"); 
          break; 

         //TODO add more colours 

         default: 
          return false; 

        } 
        return true; 
       } 
      }); 

      pop.show(); 
     } 
    }); 


    submit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      String message = edit.getText().toString(); 
      Intent intent = new Intent(); 
      intent.putExtra("MESSAGE", message); 
      setResult(2, intent); 
      finish(); 
     } 

    }); 

    } 
} 

我真的不知道,如果在每一種情況下,我宣佈的意圖實際上是讓回changeBackground方法的主要活動:

public class MainActivity extends AppCompatActivity { 
//GLOBALS 
TextView txt; 
Button btn; 
RelativeLayout rel; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    txt = (TextView) findViewById(R.id.Text); 
    btn = (Button) findViewById(R.id.Btn); 
    rel = (RelativeLayout) findViewById(R.id.activity_main); 

    Intent intent = getIntent(); 
    String color = intent.getStringExtra("color"); 
    changeBackground(color); 


    btn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //ON CLICK SETS INTENT 
      Intent intent = new Intent(MainActivity.this, Modify.class); 

      //INTENT STARTS ACTIVITY 2 
      startActivityForResult(intent, 2); 
     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 2) { 
     //SET TEXT WHEN RESULT IS ACCEPTED 
     String message = data.getStringExtra("MESSAGE"); 
     txt.setText(message); 

    } 

} 

public void changeBackground(String color) { 

    if (color.equals("red")) { 
     rel.setBackgroundColor(Color.RED); 
    } else if (color.equals("green")) { 
     rel.setBackgroundColor(Color.GREEN); 
    } else if (color.equals("blue")) { 
     rel.setBackgroundColor(Color.BLUE); 
    } 
    } 
} 
+1

你的'startActivityForResult()'在哪裏?顯示logcat – tahsinRupam

+0

我有一個startActivityForResult()文本額外。然而,我可以用一個子活動的意圖來設置主活動的背景顏色,這個子活動會發送額外的東西作爲changeBackground方法的參數傳遞嗎? –

+0

請在下面檢查我的答案。 – tahsinRupam

回答

0

剛使用startActivityforResult(intent);

+0

我可能誤解了我的問題。我試圖用彈出菜單中的每個項目的意圖設置主要活動的顏色,同時還從單獨的按鈕發送額外的文本作爲onActivityResult。這兩個視圖都是在您修改主要活動外觀的子活動中。 –

0

試試下面的代碼:

botton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      PopupMenu pop = new PopupMenu(Modify.this, botton); 
      pop.getMenuInflater().inflate(R.menu.menu,pop.getMenu()); 
      pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){ 
       @Override 
       public boolean onMenuItemClick(MenuItem item){ 
        Intent i = new Intent(Modify.this, MainActivity.class); 

        switch (item.getItemId()){ 
         case R.id.red: 
          i.putExtra("color","red");                
          break; 

         case R.id.magenta: 
          i.putExtra("color","magenta");       
          break; 

         default: 
          return false; 
        } 

        startActivity(i); 
        finish(); 

        return true; 
       } 
      });      
      pop.show(); 
     } 
    }); 

MainActivityonCreate()

String color = getIntent().getStringExtra("color", "red"); 
    if (color.equals("red")) { 
     rel.setBackgroundColor(Color.RED); 
    } else if (color.equals("green")) { 
     rel.setBackgroundColor(Color.GREEN); 
    } else if (color.equals("blue")) { 
     rel.setBackgroundColor(Color.BLUE); 
    } 

希望這有助於。

+0

我很感激這個幫助,但它在啓動時崩潰。出於某種原因,只要我對第二個代碼段進行切換評論,它就會正常運行,但對於我選擇項目時沒有任何操作。 –

+0

1)您是否將'onMenuItemClick'更改爲'return true'? 2)你在哪裏添加了第二個代碼片段?它應該在setContentView之後的'onCreate()'中。請檢查你正在收到什麼錯誤 – tahsinRupam

+0

是的。我也有一個單獨的onActivityResult主要活動收到一個額外的文本。也許我不能混用這兩種意圖。 –

0

解決了,有點粗暴:

子活動:

public class Modify extends AppCompatActivity { 
Button button, submit; 
EditText edit; 
String s; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.modify); 
    button = (Button) findViewById(R.id.button); 
    submit = (Button) findViewById(R.id.submit); 
    edit = (EditText) findViewById(R.id.editText); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      PopupMenu pop = new PopupMenu(Modify.this, boton); 
      pop.getMenuInflater().inflate(R.menu.menu, pop.getMenu()); 
      pop.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 

       @Override 
       public boolean onMenuItemClick(MenuItem item) { 

        switch(item.getItemId()){ 

         case R.id.red: 
          s="red"; 
          return true; 

         case R.id.green: 
          s="green"; 
          return true; 

         case R.id.blue: 
          s="blue"; 
          return true; 

         case R.id.yellow: 
          s="yellow"; 
          return true; 

         case R.id.magenta: 
          s="magenta"; 
          return true; 

         default: 
          return false; 
        } 
       } 
      }); 
      pop.show(); 
     } 

    }); 


    submit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent i = new Intent(Modify.this,MainActivity.class); 
      String message = edit.getText().toString(); 
      String colorMessage = s; 
      i.putExtra("MESSAGE", message); 
      i.putExtra("COLOR", colorMessage); 
      setResult(2,i); 
      finish(); 
     } 

    }); 

    } 

} 

主要活動:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    txt = (TextView) findViewById(R.id.Text); 
    btn = (Button) findViewById(R.id.Btn); 
    rel = (RelativeLayout) findViewById(R.id.activity_main); 

    btn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(MainActivity.this, Modify.class); 
      startActivityForResult(intent,2); 

     } 
    }); 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode==2) 
    { 

     String message=data.getStringExtra("MESSAGE"); 
     txt.setText(message); 
     String color = data.getStringExtra("COLOR"); 
     changeBackground(color); 
    } 
} 
public void changeBackground(String color) { 
    if (color.equals("red")) { 
     rel.setBackgroundColor(Color.RED); 
    } else if (color.equals("green")) { 
     rel.setBackgroundColor(Color.GREEN); 
    } else if (color.equals("blue")) { 
     rel.setBackgroundColor(Color.BLUE); 
    } else if (color.equals("yellow")) { 
     rel.setBackgroundColor(Color.YELLOW); 
    } else if (color.equals("magenta")) { 
     rel.setBackgroundColor(Color.MAGENTA); 
    } 
} 

} 

請讓我知道如果有一個更優雅的方式來獲得的顏色開關盒不必將它們分配給它們中的每一個變量。

Cheers