【问题标题】:Android fragments xml crashAndroid 片段 xml 崩溃
【发布时间】:2014-10-18 14:53:37
【问题描述】:

当我将设置的内容视图更改为 xml 布局时,我一直在崩溃,但不确定出了什么问题。该程序应该有两个片段,左侧的列表视图和右侧的 web 视图,当它处于横向时,只有纵向列表。对不起所有的代码,我只是不知道发生了什么。

这是我的主要活动

public class MainActivity extends Activity {

static final String LOGTAG = MainActivity.class.getSimpleName() + "_TAG";

static Resources mRes = null;
static FragmentManager mFrgmntMngr = null;
static MainActivity mThisAct = null;


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


    FragmentManager.enableDebugLogging(true);
    setContentView(R.layout.activity_main_1view);
    mRes = getResources();
    mFrgmntMngr = getFragmentManager();
    mThisAct = this;
}


static boolean isInLandscapeOrientation() {
    return mRes.getConfiguration().orientation
            == Configuration.ORIENTATION_LANDSCAPE;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
public void displayTwainText(int mCurPosition) {
    // TODO Auto-generated method stub
     if ( isInLandscapeOrientation() ) {
            // Check what fragment is shown, replace if needed.
            TwainTitleWebViewFragment tTxtFrgmnt = (TwainTitleWebViewFragment)
                    mFrgmntMngr.findFragmentById(R.id.twain_title_list);
            if (tTxtFrgmnt == null || tTxtFrgmnt.getDisplayedTwainIndex()   !=mCurPosition)
                // Make new fragment to show this selection.
                tTxtFrgmnt = TwainTitleWebViewFragment.newInstance(mCurPosition);

                // Execute a transaction, replacing any existing
                // fragment inside the frame with the new one.
                Log.d(LOGTAG, "about to run FragmentTransaction...");
                FragmentTransaction frag_trans
                        = mFrgmntMngr.beginTransaction();

                frag_trans.setCustomAnimations(R.animator.bounce_in_down,
                        R.animator.fade_out);
                frag_trans.setCustomAnimations(R.animator.bounce_in_down,
                        R.animator.slide_out_right);

                frag_trans.replace(R.id.list, tTxtFrgmnt);

                frag_trans.commit();
            }

        } else {
            // Otherwise we need to launch a new activity to display
            // the dialog fragment with selected text.
            Intent intent = new Intent();
            intent.setClass(mThisAct, TwainTitleViewActivity.class);
            intent.putExtra(mRes.getString(R.string.twain_index_key), mCurPosition);
            this.startActivity(intent);
        }
    }
}

这是layout文件夹中的activity_main1view.xml

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

<fragment class="com.example.hw_07_final_.TitleListFragment"
    android:id="@+id/twain_title_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
 </LinearLayout>

这是布局中的那个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.example.hw_07_final_.TitleListFragment"
    android:id="@+id/list"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent" 
    android:background="#00550033"/>
<FrameLayout
    android:id="@+id/twain_text"
    android:layout_weight="4"
    android:layout_width="0dp"
    android:layout_height="match_parent" />
 </LinearLayout>

这是原木猫

【问题讨论】:

  • 试过了,同样的错误

标签: android xml android-fragments


【解决方案1】:

命名 xml 文件时不能使用数字或大写字母。

activity_main1view.xml 应该是 activity_mainview.xml,布局名称中不包括“1”。

【讨论】:

  • 我把它改回了activity_mainview,但现在纵向模式下什么都没有显示,横向模式下它崩溃了
  • 请在进行此更改后更新您最近的代码和 logCat。也可以试试 intent = new intent(MainActivity.this, TwainTitleViewActivity.class);
【解决方案2】:

改变

<fragment class="com.example.hw_07_final_.TitleListFragment"

到

<fragment android:name="com.example.hw_07_final_.TitleListFragment"

运行你的应用,它会膨胀你的片段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多