我在一個活動中遇到了兩個片段的問題。 左邊的片段是一個listfragment,右邊的是正常的片段。 我得到了一個laoyut.xml來合併片段,每個片段一個layout.xml和elayout.xml中listfragment中的每一行。 此外,我得到一個類來啓動包含這兩個片段的佈局的活動,一個用於listfragment的類和一個用於正常fragmetn和一個適配器的活動。 而我不知道如何解決這個問題。 我認爲發佈我的代碼和錯誤日誌更容易,我將標記創建錯誤的位置。首先,我將向您展示我的layout.xml用於組合片段。 據calld 「manu_pane.xml」有兩個片段的活動會導致錯誤
<LinearLayout 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:orientation="horizontal" >
<fragment
android:id="@+id/menu_item_fragment"
android:name="wak.iage.layout.MenuItemFragment"
android:layout_width="279dp"
android:layout_height="match_parent"
/>
<fragment
android:id="@+id/menu_content_fragment"
android:name="wak.iage.layout.MenuContentFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
現在layout.xml的listfragment( 「menu_item_fragment.xml」)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/right_hand_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/tutorial_headline_bg"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:text="@string/status_headline"
android:textColor="@color/tutorial_headline_font"
android:gravity="center"
android:textSize="@dimen/fontsize_slogan_titlescreen" />
<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
現在的佈局。 xml爲「內容」(「menu_content_main.xml」)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/titlescreen_bg"
android:orientation="vertical"
android:paddingTop="60dp"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_headline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="60dp"
android:paddingTop="60dp"
android:text="@string/slogan"
android:textSize="@dimen/fontsize_slogan_titlescreen" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="60dp"
android:paddingTop="60dp"
android:text="@string/slogan"
android:textSize="@dimen/fontsize_slogan_titlescreen" />
最後的layout.xml每個列表項( 「menu_list_item」)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<TextView android:id="@+id/menuListItemTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
/>
現在我告訴你我的方法與兩個片段開始活動
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
Intent intent = new Intent(this, MenuActivity.class);
startActivity(intent);
return super.onOptionsItemSelected(item);
}
MenuActivity.class看起來像這樣:
package wak.iage.layout;
import wak.iage.storage.DataManager;
import wak.iage.storage.DataManager.DataManagerException;
import wak.iage.R;
import android.app.Activity;
import android.os.Bundle;
public class MenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_pane);
bootstrap(); // load all less-data
}
private void bootstrap() {
DataManager dataManager = new DataManager(this);
try {
dataManager.loadStaticData();
} catch (DataManagerException e) {
e.printStackTrace();
}
}
}
的MenuContentFragment.class包含 「menu_content_main.xml」,看起來像這樣:
package wak.iage.layout;
import wak.iage.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MenuContentFragment extends Fragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.menu_content_main, container);
return v;
}
public void changeText(String title, String content) {
TextView head = (TextView) getActivity().findViewById(R.id.tv_headline);
TextView body = (TextView) getActivity().findViewById(R.id.tv_content);
body.setText(content);
head.setTag(title);
}
}
的 「MenuItemFragment.class」 包含 「menu_list_item.xml」,看起來像這樣:
package wak.iage.layout;
import java.util.ArrayList;
import wak.iage.R;
import wak.iage.tutorial.MenuItem;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class MenuItemFragment extends ListFragment
{
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
MenuAdapter adapter = (MenuAdapter) this.getListAdapter();
MenuContentFragment fragment = (MenuContentFragment) getFragmentManager().findFragmentById(R.id.menu_content_fragment);
fragment.changeText(adapter.getItem(position).getTitle(), adapter.getItem(position).getContent());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<MenuItem> values = new ArrayList<MenuItem>();
values.add(new MenuItem("Impressum", "hier steht das impressum"));
values.add(new MenuItem("Hilfe", "hier steht die Hilfe"));
values.add(new MenuItem("Kontakt", "hier steht der Kontakt"));
values.add(new MenuItem("Andere Kram", "hier steht krims krams"));
MenuAdapter adapter = new MenuAdapter(getActivity(), values);
setListAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.menu_item_fragment, container, false);
return view;
}
}
最後的 「MenuAdapter.class」:
package wak.iage.layout;
import java.util.ArrayList;
import wak.iage.R;
import wak.iage.tutorial.MenuItem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MenuAdapter extends ArrayAdapter<MenuItem> {
public ArrayList<MenuItem> values = null;
private static StatusRowContainer menuItem = null;
public MenuAdapter(Context context, ArrayList<MenuItem> values) {
super(context, R.layout.menu_list_item);
this.values = values;
this.addAll(values);
}
public ArrayList<MenuItem> getMenuItems(){
return values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = ((Activity) getContext()) .getLayoutInflater();
convertView = inflater.inflate(R.layout.menu_list_item, parent, false);
menuItem = new StatusRowContainer();
//Here is the error!!!
menuItem.text = (TextView) convertView.findViewById(R.id.tv_headline);
menuItem.content = (TextView) convertView.findViewById(R.id.tv_content);
convertView.setTag(menuItem);
} else {
menuItem = (StatusRowContainer) convertView.getTag();
}
MenuItem menu = values.get(position);
menuItem.text.setText(menu.getTitle());
menuItem.content.setText(menu.getContent());
return convertView;
}
private class StatusRowContainer {
public TextView content;
public TextView text;
}
}
和L AST是錯誤日誌:
02-22 16:53:41.838:E/AndroidRuntime(20746):致命異常:主 02-22 16:53:41.838:E/AndroidRuntime(20746):JAVA .lang.NullPointerException 02-22 16:53:41.838:E/AndroidRuntime(20746):at wak.iage.layout.MenuAdapter.getView(MenuAdapter.java:47) 02-22 16:53:41.838:E/AndroidRuntime(20746):在android.widget.AbsListView.obtainView(AbsListView.java:2218) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.widget.ListView.measureHeightOfChildren(ListView.java: 1244) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.widget.ListView.onMeasure(ListView.java:1155) 02-22 16:53:41.838:E/AndroidRun時間(20746):在android.view。View.measure(View.java:12929) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4703) 02-22 16:53: 41.838:E/AndroidRuntime(20746):在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.widget.LinearLayout.measureVertical LinearLayout.java:670) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.widget.LinearLayout.onMeasure(LinearLayout.java:563) 02-22 16:53:41.838:E/AndroidRuntime(20746):在android.view.View.measure(View.java:12929) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 4703) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.widget.LinearLayou t.measureChildBeforeLayout(LinearLayout.java:1385) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1027) 02-22 16:53: 41.838:E/AndroidRuntime(20746):at android.widget.LinearLayout.onMeasure(LinearLayout.java:565) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.view.View.measure( E/AndroidRuntime(20746):at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4703) 02-22 16:53:41.838:E/AndroidRuntime(20746):android.view.java:12929) 02-22 16:53:41.838: AndroidRuntime(20746):在android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.view.View.measure(View.java: 12929) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.widget.LinearLayout.measureVertical(L E/AndroidRuntime(20746):at android.widget.LinearLayout.onMeasure(LinearLayout.java:563) 02-22 16:53:41.838:E/AndroidRuntime(20746) AndroidRuntime(20746):在android.view.View.measure(View.java:12929) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java: 4703) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 02-22 16:53:41.838:E/AndroidRuntime(20746) :at com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2257) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.view.View.measure( View.java:12929) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.view.ViewRootImpl.performTraversals(ViewRootImpl .java:1240) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628) 02-22 16:53:41.838:E/AndroidRuntime (20746):at android.os.Handler.dispatchMessage(Handler.java:99) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.os.Looper.loop(Looper.java:137 ) 02-22 16:53:41.838:E/AndroidRuntime(20746):at android.app.ActivityThread.main(ActivityThread.java:4511) 02-22 16:53:41.838:E/AndroidRuntime(20746) (java.lang.reflect.Method.invokeNative(Native Method)) 02-22 16:53:41.838:E/AndroidRuntime(20746):at java.lang.reflect.Method.invoke(Method.java:511) 02 -22 16:53:41.838:E/AndroidRuntime(20746):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:980) 02-22 16:53:41.838:E/AndroidRuntime(20746):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 02-22 16:53:41.838:E/AndroidRuntime(20746 ):at dalvik.system.NativeStart.main(Native Method)
錯誤出現在第47行的「MenuAdapter」中(我在代碼行中寫了註釋)。 我希望你能幫助我。 我沒有明白。
感謝您的幫助和建議! 親切的問候 j0chn
OMG,這樣一個簡單的答案......是它的工作...現在我有測試菜單,但現在openes ...謝謝你很多! – j0chn 2013-02-22 16:50:31
酷不要忘記接受或提出更多的問題(聊天室很好!!!)如果你需要更多的幫助 – petey 2013-02-22 17:27:24
如果我有一個問題,我會先結帳聊天;)但我必須糾正我......你的答案給了工作,但不是我想要做的;)我的第一個版本誇大了正確的佈局。我只是設置了錯誤的TextViews。首先我設置了「內容」而不是「ListItem」,但這是我想要的,所以我的「ConvertView」是錯誤的。但現在我明白了。再次感謝 ;) – j0chn 2013-02-22 18:10:10