找不到ID 0x7f050016的视图

时间:2013-12-09 16:57:41

标签: android android-fragments android-logcat

我有片段列表的片段。当您选择元素并单击它时,新的窗口将显示图片和详细信息。像Android training中那样的简单  这很好。
现在,在其他类活动中,我有窗口的ID,我想打开一个然后我收到了这个错误:

  

找不到ID为0x7f050016

的视图

外部活动:

value = 3;
ShowElements newElement = new ShowElements();
Bundle args = new Bundle();
args.putInt("ARG_POSITION", value);
newElement.setArguments(args);

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newElement);
transaction.addToBackStack(null);
transaction.commit();

logcat的:

12-09 18:02:30.104: E/AndroidRuntime(29670): FATAL EXCEPTION: main
12-09 18:02:30.104: E/AndroidRuntime(29670): Process: com.urbanforms.main, PID: 29670
12-09 18:02:30.104: E/AndroidRuntime(29670): java.lang.IllegalArgumentException: No view found for id 0x7f050016 (com.urbanforms.main:id/frag) for fragment ShowElements{42379550 #0 id=0x7f050016}
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.os.Handler.handleCallback(Handler.java:733)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.os.Looper.loop(Looper.java:137)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at android.app.ActivityThread.main(ActivityThread.java:4998)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at java.lang.reflect.Method.invokeNative(Native Method)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at java.lang.reflect.Method.invoke(Method.java:515)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-09 18:02:30.104: E/AndroidRuntime(29670):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

覆盖onCreateView

中的HeadlinesFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.list_of_murals), container, false);
// Initialize your views here 
TextView tv = (TextView) v.findViewById(R.id.textView1);
ListView lv = (ListView) v.findViewById(R.id.list_of_murals);
//setadapter to listview
// listview on item click listener
return v;
}

详细检查(示例)

http://developer.android.com/guide/components/fragments.html

编辑:

public class MenuActivity extends FragmentActivity implements
OnHeadlineSelectedListener  {

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

if (findViewById(R.id.fragment_container) != null) {
    if (savedInstanceState != null) {
        return;
    }
    SelectElement selectedElement = new SelectElement();
    selectedElement.setArguments(getIntent().getExtras());
    getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_container, selectedElement).commit();
}
}

@Override
public void onArticleSelected(int position) {
    ShowElements newElement = new ShowElements();
    Bundle args = new Bundle();
    args.putInt(ShowElements.ARG_POSITION, position);
    newElement.setArguments(args);

    FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction();

    transaction.replace(R.id.fragment_container, newElement);
    transaction.addToBackStack(null);
    transaction.commit();
}
}

activity_menu.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"
tools:context=".MainActivity" >
 <FrameLayout 
     android:layout_below="@+id/textView1"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.iklikla.codetechblog.FrontpageActivity"
tools:ignore="MergeRootFrame" />

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="murals_list"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

SelectFragment

public class SelectElement extends ListFragment {
    String[] streetName; 
    OnHeadlineSelectedListener mCallback;
    interface OnHeadlineSelectedListener
    {
        public void onArticleSelected(int position);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Resources res = getActivity().getResources();
        streetName = res.getStringArray(R.array.item);
        int layout = android.R.layout.simple_list_item_1;

        setListAdapter(new ArrayAdapter<String>(getActivity(), layout,
                streetName));
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " dupa!");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        mCallback.onArticleSelected(position);

        getListView().setItemChecked(position, true);
    }

}

ShowElements

public class ShowElements extends Fragment {

    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;
    String[] authorsNicks;
    TextView autorTextView; 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.my_fragment2, container, false);
    if (savedInstanceState != null) {
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
    }

    Resources res = getActivity().getResources();

    authorsNicks = res.getStringArray(R.array.planets_array);
    autorTextView = (TextView) v.findViewById(
            R.id.textView1);

    return v;
}

    @Override
    public void onStart() {
        super.onStart();
        Bundle args = getArguments();
        if (args != null) {
            updateArticleView(args.getInt(ARG_POSITION));
        } else if (mCurrentPosition != -1) {
            updateArticleView(mCurrentPosition);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }

    public void updateArticleView(int position) {

        autorTextView.setText(authorsNicks[position]);
        mCurrentPosition = position;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return false;

        }
}

my_framgent2.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="65dp"
        android:text="TextView" />

</RelativeLayout>

使用的字符串数组

 <string-array name="item">
        <item>Austria</item>
        <item>Balraus</item>
        <item>Belgium</item>
    </string-array>
     <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>

对齐

enter image description here enter image description here