简体   繁体   English

更改背景颜色时出现错误

[英]Getting the error in changing background color

making an app that asks the user for their favorite color, then take them to a new Activity that uses that color as a background color and says something nice about them. 制作一个向用户询问他们喜欢的颜色的应用程序,然后将他们带到一个新的“活动”中,该活动将该颜色用作背景颜色,并对他们说出一些好话。 but i dont why its crashing again and agian 但是我不明白为什么它再次崩溃和变态

heres the code 继承人的代码

MainActivity.java MainActivity.java

private TextView mtextview;
private EditText medittext;
private Button mbutton;

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

     mtextview = (TextView) findViewById(R.id.textView);
     medittext=(EditText)findViewById(R.id.editText);
    mbutton=(Button)findViewById(R.id.button);

    mbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String name =medittext.getText().toString();
            startStory(name);
        }

        private void startStory(String name) {
            Intent intent = new Intent(MainActivity.this,AnotherActivity.class);
            intent.putExtra("name",name);
            startActivity(intent);
        }
    });


}

} }

AnotherActivity.java AnotherActivity.java

public class AnotherActivity extends AppCompatActivity {

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

    final RelativeLayout mrelativelayout = (RelativeLayout) findViewById(R.id.activity_main);
    Intent intent   = getIntent();
    String name = intent.getStringExtra("name");
    mrelativelayout.setBackgroundColor(Color.parseColor(name));

}

} }

I assume you are passing color name as "black", "blue", "skyblue" and likewise other color names. 我假设您将颜色名称传递为“黑色”,“蓝色”,“天蓝色”以及其他颜色名称。

But hope you know about the string parameters value supported by parseColor() method. 但是希望您了解parseColor()方法支持的字符串参数值。

Supported formats are: #RRGGBB #AARRGGBB or one of the following names: 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow', 'lightgray', 'darkgray', 'grey', 'lightgrey', 'darkgrey', 'aqua', 'fuchsia', 'lime', 'maroon', 'navy', 'olive', 'purple', 'silver', 'teal'. 支持的格式为:#RRGGBB #AARRGGBB或以下名称之一:'红色','蓝色','绿色','黑色','白色','灰色','青色','品红色','黄色' ,“浅灰色”,“ darkgray”,“灰色”,“ lightgrey”,“ darkgrey”,“ aqua”,“ fuchsia”,“ lime”,“ maroon”,“ navy”,“ olive”,“ purple”,“银”,“蓝绿色”。

Change 1 : 变更1:

first of all AnotherActivity add below code inside the OnCreate method. 首先, AnotherActivityOnCreate方法内添加以下代码。

final RelativeLayout mrelativelayout = (RelativeLayout) findViewById(R.id.activity_main) ; final RelativeLayout mrelativelayout = (RelativeLayout) findViewById(R.id.activity_main) ;

because it give you NullPointerException . 因为它给您NullPointerException

Change 2 : 变更2:

Instead of trying this 而不是尝试这个

Editable name = medittext.getText();

use this way 用这种方式

 String name = medittext.getText().toString();

and pass that string to AnotherActivity via Intent . 并通过Intent将该字符串传递给AnotherActivity

Make sure that when you putExtra in the intent, you convert it to String. 确保将intent放入putIntra时,将其转换为String。 Currently you are just passing an Editable object and not String object to the intent 当前,您只是将一个Editable对象而不是String对象传递给该意图

Example: 例:

intent.putExtra("name",name.toString());

or better, change the type of name 或更好,更改名称的类型

String name = medittext.getText().toString();

Please, post your activity_main.xml and activity_another.xml. 请发布您的activity_main.xml和activity_another.xml。 The problem is that there is not button with android:id="@+id/button" in your activity_main.xml, probably that's in activity_another.xml. 问题是您的activity_main.xml中没有带有android:id =“ @ + id / button”的按钮,可能是在activity_another.xml中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM