0

我在一项活动中遇到了两个片段的问题。左边的片段是一个列表片段,右边的是一个普通的片段。我得到了一个 laoyut.xml 来组合片段,每个片段一个 layout.xml 和一个 listfragment 中每一行的 elayout.xml。此外,我得到一个类来启动包含两个片段布局的活动,一个类用于 listfragment,一个类用于普通 fragmetn 和一个适配器。而且我不知道如何解决这个问题。我认为发布我的代码和错误日志更容易,我会标记错误的位置。首先,我将向您展示用于组合片段的 layout.xml。它被称为“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" 
/>

现在 listfragment 的 layout.xml ("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" />

现在“内容”的layout.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" 
 />

现在我向你展示我用两个片段启动 Activity 的方法

    @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;
}
}

最后是错误日志:

02-22 16:53:41.838: E/AndroidRuntime(20746): 致命异常: main 02-22 16:53:41.838: E/AndroidRuntime(20746): java.lang.NullPointerException 02-22 16:53:41.838: E/AndroidRuntime(20746): 在 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): 在 android.widget.ListView.measureHeightOfChildren(ListView.java:1244) 02-22 16:53:41.838: E/AndroidRuntime(20746 ): 在 android.widget.ListView.onMeasure(ListView.java:1155) 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): 在 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): 在 android.widget.LinearLayout.measureVertical(LinearLayout.java:670) 02-22 16 :53:41.838: E/AndroidRuntime(20746): 在 android.widget.LinearLayout.onMeasure(LinearLayout.java:563) 02-22 16:53:41.838: E/AndroidRuntime(20746): 在 android.view.View。测量(View.java:12929)02-22 16:53:41.838:E/AndroidRuntime(20746):在 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): 在 android.widget.LinearLayout.measureHorizo​​ntal(LinearLayout.java:1027 ) 02-22 16:53:41.838: E/AndroidRuntime(20746): 在 android.widget。LinearLayout.onMeasure(LinearLayout.java:565) 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): 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4703) 02-22 16:53:41.838: E/AndroidRuntime(20746): 在 android.widget.FrameLayout.onMeasure(FrameLayout.java :293) 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):在 android.widget.LinearLayout.measureVertical(LinearLayout.java:822) 02-22 16:53:41.838: E/AndroidRuntime(20746): 在 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):在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4703) 02-22 16:53:41.838: E/AndroidRuntime(20746): 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 02-22 16 :53:41.838: E/AndroidRuntime(20746): 在 com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2257) 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): 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1240) 02-22 16:53:41.838: E/AndroidRuntime(20746): 在 android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628) 02-22 16:53:41.838: E/AndroidRuntime(20746): 在 android.os.Handler .dispatchMessage(Handler.java:99) 02-22 16:53:41.838: E/AndroidRuntime(20746): 在 android.os.Looper.loop(Looper.java:137) 02-22 16:53:41.838: E/AndroidRuntime(20746): 在 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): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 02-22 16:53:41.838: E/AndroidRuntime(20746): 在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)invokeNative(本机方法)02-22 16:53:41.838:E/AndroidRuntime(20746):在 java.lang.reflect.Method.invoke(Method.java:511)02-22 16:53:41.838:E/AndroidRuntime (20746): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 02-22 16:53:41.838: E/AndroidRuntime(20746): 在 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)invokeNative(本机方法)02-22 16:53:41.838:E/AndroidRuntime(20746):在 java.lang.reflect.Method.invoke(Method.java:511)02-22 16:53:41.838:E/AndroidRuntime (20746): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 02-22 16:53:41.838: E/AndroidRuntime(20746): 在 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)747) 02-22 16:53:41.838: E/AndroidRuntime(20746): at dalvik.system.NativeStart.main(Native Method)747) 02-22 16:53:41.838: E/AndroidRuntime(20746): at dalvik.system.NativeStart.main(Native Method)

错误在第 47 行的“MenuAdapter”中(我在代码行中写了注释)。我希望你能帮助我。我不明白。

感谢您的帮助和建议!!亲切的问候 j0chn

4

1 回答 1

0

我认为您的意思是膨胀 menu_content_main 而不是 menu_list_item,因为 menu_list_item 没有 id 为 tv_headline 的 textview。

尝试改变

convertView = inflater.inflate(R.layout.menu_list_item, parent, false);

convertView = inflater.inflate(R.layout.menu_content_main, parent, false);
于 2013-02-22T16:37:51.563 回答