带有两个片段的活动会导致错误

时间:2013-02-22 16:24:18

标签: android menu fragment adapter

我在一个活动中遇到了两个片段的问题。 左侧片段是一个列表片段,右侧片段是正常片段。 我有一个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" 
/>

现在是listfragment的layout.xml(&#34; menu_item_fragment.xml&#34;)

<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" />

现在&#34;内容&#34;的layout.xml (&#34; menu_content_main.xml&#34)

<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" />

最后是每个listitem的layout.xml(&#34; menu_list_item&#34;)

<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包含&#34; menu_content_main.xml&#34;看起来像这样:

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

&#34; MenuItemFragment.class&#34;包含&#34; menu_list_item.xml&#34;看起来像这样:

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

最后&#34; MenuAdapter.class&#34; :

 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):致命异常:主要   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):在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.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.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):at 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):at 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)

错误在&#34; MenuAdapter&#34;在第47行(我在代码行中写了一条评论)。 我希望你能帮助我。 我没理得。

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

1 个答案:

答案 0 :(得分:0)

我认为你的意思是夸大menu_content_main而不是menu_list_item,因为menu_list_item没有带有tv_headline id的textview。

尝试更改

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

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