Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 点击按钮后Android背景图像消失_Java_Android_Xml_Sdk - Fatal编程技术网

Java 点击按钮后Android背景图像消失

Java 点击按钮后Android背景图像消失,java,android,xml,sdk,Java,Android,Xml,Sdk,Android开发非常新。我有一个培训应用程序,它有一个文本输入字段和一个按钮。如果输入的文本与某个单词匹配,则单击按钮将创建一个新页面(Intent?),其中显示一些文本 我遇到的问题是,通过单击按钮创建的新页面没有与主页相同的背景图像。我不知道为什么。这是我到目前为止的相关代码。如果需要,我可以发布更多 **fragment_welcome_screen.xml** <?xml version="1.0" encoding="utf-8"?> <LinearLayout

Android开发非常新。我有一个培训应用程序,它有一个文本输入字段和一个按钮。如果输入的文本与某个单词匹配,则单击
按钮将创建一个新页面(Intent?),其中显示一些文本

我遇到的问题是,通过单击按钮创建的新页面没有与主页相同的背景图像。我不知道为什么。这是我到目前为止的相关代码。如果需要,我可以发布更多

**fragment_welcome_screen.xml**

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/bg_pic">
    <EditText android:id="@+id/edit_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message"
        android:layout_weight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage"/>
</LinearLayout>
我希望只有在键入“Secret”时按钮才起作用

DisplayMessageActivity.java


public class DisplayMessageActivity extends Activity {

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

        // Get the message from the intent
        Intent intent = getIntent();
        //String message = intent.getStringExtra(WelcomeScreen.EXTRA_MESSAGE);
        String message = "Test String";
        TextView textView = new TextView(this);
        textView.setTextSize(24);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }    
我知道我可能做错了什么

activity_display_message.xml


<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@drawable/test_pic"
    tools:context="com.example.varun.myapplication.DisplayMessageActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:background="@drawable/test_pic"/>

</RelativeLayout>
activity\u display\u message.xml
我希望test_pic是按下按钮时显示的页面的背景。但事实并非如此。背景是白色的

我意识到这可能是一个微不足道的问题,但有人能帮我吗?我会非常感激的

谢谢。

删除此文件 setContentView(文本视图)

取下这个
setContentView(文本视图)

为什么要编写setContentView(textView)


只要删除上面给定的代码行,您就完成了。

为什么要编写setContentView(textView)

只需删除上面给定的代码行,您就完成了。

更改代码

public class DisplayMessageActivity extends Activity {

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

        // Get the message from the intent
        Intent intent = getIntent();
        //String message = intent.getStringExtra(WelcomeScreen.EXTRA_MESSAGE);
        String message = "Test String";
        TextView textView = (TextView)findViewByID(R.id.textMessage)
        textView.setTextSize(24);
        textView.setText(message);
        }
并为textview设置id

<TextView android:id="@+id/textMessage"
  android:text="@string/hello_world" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:background="@drawable/test_pic"/>

更改代码

public class DisplayMessageActivity extends Activity {

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

        // Get the message from the intent
        Intent intent = getIntent();
        //String message = intent.getStringExtra(WelcomeScreen.EXTRA_MESSAGE);
        String message = "Test String";
        TextView textView = (TextView)findViewByID(R.id.textMessage)
        textView.setTextSize(24);
        textView.setText(message);
        }
并为textview设置id

<TextView android:id="@+id/textMessage"
  android:text="@string/hello_world" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:background="@drawable/test_pic"/>

像这样试试

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:background="@drawable/test_pic"
        tools:context="com.example.varun.myapplication.DisplayMessageActivity">

        <TextView android:id="@+id/txtmsg"
            android:text="@string/hello_world" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:background="@drawable/test_pic"/>

    </RelativeLayout>


    public class DisplayMessageActivity extends Activity {

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

            // Get the message from the intent
            Intent intent = getIntent();
            //String message = intent.getStringExtra(WelcomeScreen.EXTRA_MESSAGE);
            String message = "Test String";
            TextView textView = (TextView) findViewById(R.id.txtmsg);
            textView.settext(message);
        }    

公共类DisplayMessageActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u display\u message);
//从意图中获得信息
Intent=getIntent();
//字符串消息=intent.getStringExtra(WelcomeScreen.EXTRA_消息);
字符串消息=“测试字符串”;
TextView TextView=(TextView)findViewById(R.id.txtmsg);
textView.settext(消息);
}    
像这样试试

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:background="@drawable/test_pic"
        tools:context="com.example.varun.myapplication.DisplayMessageActivity">

        <TextView android:id="@+id/txtmsg"
            android:text="@string/hello_world" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:background="@drawable/test_pic"/>

    </RelativeLayout>


    public class DisplayMessageActivity extends Activity {

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

            // Get the message from the intent
            Intent intent = getIntent();
            //String message = intent.getStringExtra(WelcomeScreen.EXTRA_MESSAGE);
            String message = "Test String";
            TextView textView = (TextView) findViewById(R.id.txtmsg);
            textView.settext(message);
        }    

公共类DisplayMessageActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u display\u message);
//从意图中获得信息
Intent=getIntent();
//字符串消息=intent.getStringExtra(WelcomeScreen.EXTRA_消息);
字符串消息=“测试字符串”;
TextView TextView=(TextView)findViewById(R.id.txtmsg);
textView.settext(消息);
}    

为什么在DisplayMessageActivity.java中使用setContentView()两次。删除secound setContentView()并在activity\u display\u message.xml中为textview分配一些id。以下是为textview分配id的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/test_pic"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            tools:context="com.example.varun.myapplication.DisplayMessageActivity">

<TextView
    android:id="@+id/mytv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/test_pic"
    android:text="@string/hello_world"/>
并使用此textview引用显示来自上一个活动的消息,如下所示

textView.setText(message);

我希望这会对您有所帮助。

为什么在DisplayMessageActivity.java中使用setContentView()两次。删除secound setContentView()并在activity\u display\u message.xml中为textview分配一些id。以下是为textview分配id的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/test_pic"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            tools:context="com.example.varun.myapplication.DisplayMessageActivity">

<TextView
    android:id="@+id/mytv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/test_pic"
    android:text="@string/hello_world"/>
并使用此textview引用显示来自上一个活动的消息,如下所示

textView.setText(message);

我希望这对您有所帮助。

您的textview上还有“android:background=“@drawable/test\u pic”,所以活动背景将与textview相同?在欢迎活动中,您使用bg\u pic作为背景,DisplayMessageActivity使用test\u pic作为背景,那么这两个活动的背景如何相同?您还有“android:background”=“@可牵引/测试图"在您的textview上,活动背景将与textview相同?在欢迎活动中,您使用bg_pic作为背景,DisplayMessageActivity使用test_pic作为背景,那么这两个活动的背景如何相同?@MK为什么您总是提供所有代码???只需提供一个带有一些描述的解决方案或提示。无需提供所有代码…这很简单这不是一个正确的答案。@MK为什么您总是提供所有代码???只需提供一个解决方案或带有一些描述的提示。不需要提供所有代码……这不是一个正确的答案。