簡體   English   中英

Android片段XML崩潰

[英]Android fragments xml crash

當我將設置的內容視圖更改為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);
        }
    }
}

這是布局文件夾中的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>

這是原木貓 在此處輸入圖片說明

命名xml文件時,不能包含數字或大寫字母。

activity_main1view.xml應該是activity_mainview.xml,從布局名稱中排除“ 1”。

更改

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

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

運行您的應用程序,它將使您的片段膨脹。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM