简体   繁体   English

带有文本字段和图像字段的相对布局不会在Android应用程序上显示

[英]Relative Layout with a text field and Image field doesn't display on Android Application

I start an activity from main activity at startup and finish that activity using finish(). 我从启动时的主要活动开始一个活动,然后使用finish()完成该活动。 Now the issue is that contents of the activity being called are not visible. 现在的问题是,被调用活动的内容不可见。 Heres the code snippet. 这是代码段。

MainActivity onCreate() MainActivity onCreate()

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

    Intent intent = new Intent(this, SplashActivity.class);
    startActivity(intent);
    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}

SplashActivity onCreate() SplashActivity onCreate()

public class SplashActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    //setContentView(R.layout.fragment_splash);
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
    finish();
}

SplashActivity Fragment LAYOUT FILE SplashActivity片段布局文件

<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:gravity="center"
    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.daq.SplashActivity$PlaceholderFragment" >

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

     <ImageView
        android:id="@+id/imageView1"
        android:layout_width="192dp"
        android:layout_height="146dp"
        android:layout_gravity="center"
        android:contentDescription="@string/desc"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

SplashActivity activity_splash.xml SplashActivity activity_splash.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.daq.SplashActivity"
    tools:ignore="MergeRootFrame" >


</FrameLayout>

Android Manifest FIle Android清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.daq"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.daq.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.daq.SplashActivity"
            android:label="@string/title_activity_splash"
            android:parentActivityName="com.example.daq.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.daq.MainActivity" />
        </activity>
    </application>

</manifest>

I donot know why 2 layout files are created when I make one activity (fragment_splash.xml and activity_splash.xml) 我不知道为什么进行一个活动(fragment_splash.xml和activity_splash.xml)时会创建2个布局文件

I would like to mention that the code works without errors and activities are shown but without content. 我想提及的是,该代码可以正常运行,没有错误,并且显示了活动但没有内容。

您可以在MainActivity之后首先启动SplashActivity。

You are calling finish() as soon as the fragment is placed in the container so the activity will finish and go back to the activity that called it. 一旦将片段放入容器中,您就在调用finish(),以便该活动将完成并返回到调用它的活动。

public class SplashActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    //setContentView(R.layout.fragment_splash);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }    
    finish();
}

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

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