0

我试图升级我的Android项目以使用Android 5.0进行编译。在此之前,我正在成功使用appcompat-v7库。现在使用ActionBarActivity和Theme.AppCompat会导致我的分段布局中的视图不呈现或不可见。如果我将其更改回“活动”并删除appcompat主题,则视图将再次显示。任何人都可以指出我做错了的配置吗?我的Eclipse项目正在使用Android 5.0,专用库android-support-v4.jar和android-support-v7-appcompat.jar进行编译。在Android依赖项下是android-support-v7-appcompat.jar。Appcompat v7 Android 5.0 ActionBarActivity TextView不可见

而且我有下面的代码:

在AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE something> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.mycompany.ctest" 
    android:versionCode="33" 
    android:versionName="1.4.0" > 
    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18"/> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="My App" 
     android:allowBackup="true" > 
     <activity 
      android:name=".MyActivity" 
      android:theme="@style/Theme.AppCompat"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

在fragment_layout.xml:

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE something> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="Nothing here" 
     android:textColor="@color/blue" > 
    </TextView> 

</LinearLayout> 

在MyActivity.java:

package com.mycompany.ctest; 

import android.app.FragmentManager; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 

public class MyActivity extends ActionBarActivity { 

    private FragmentManager mFM; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mFM = getFragmentManager(); 
     mFM.beginTransaction() 
       .add(android.R.id.content, new MyFragment(),"frag") 
       .commit(); 
    } 
} 

而且, MyFragment.java:

package com.mycompany.ctest; 

import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class MyFragment extends Fragment { 

    @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) { 
     return inflater.inflate(R.layout.fragment_layout,group,false); 
    } 
} 

编辑:logcat的显示 10月12日至13日:10:09.709:E /的ResourceType(1013):风格包含错误项重点:0x01010479

虽然我认为一定是不相关的,因为如果我添加一个活动布局与加载的静态片段,而不是使用片段管理器,我的视图变得可见。而且我仍然收到带有错误输入消息的Style contains键。

回答

0

尝试从支持库导入而不是仅导入android.app.Fragment; ...希望这可能有所帮助

import android.support.v4.app.Fragment; 
    import android.support.v4.app.FragmentManager; 
+0

我更改为MyFragment中的v4 Fragment和MyActivity中的v4 FragmentManager。另外,我更改为getSupportFragmentManager。仍然没有运气 – mary 2014-12-13 16:07:59

0

我终于添加了一个带有id的空白帧布局的活动布局。

activity_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 


</LinearLayout> 

用。新增或.replace添加片段时的活动,而不是使用android.R.id.content然后使用该ID。

setContentView(R.layout.activity_layout); 
getSupportFragmentManager().beginTransaction() 
       .add(R.id.fragment_container,new MyFragment(), "frag") 
       .commit(); 

我仍然不知道为什么这是可行的,为什么android.R.id.content不工作。

+0

玛丽,如果您回答了您自己的问题,请将其标记为正确。 – 2015-02-13 04:43:35

相关问题