从活动转换为片段?获得空指针异常

时间:2014-07-01 06:14:43

标签: java android xml android-activity android-fragments

我正在使用Madlib应用。您在下面看到的代码是处理从madlibinput1.xml中找到的edittext框收集输入的类,将该信息存储为字符串,然后将该文本发送到madliboutput.xml,它将替换所有" wx"在输入xml中收集输入。这个类曾经是一个活动,但我发现它是一个片段有助于更好地实现我的导航抽屉。但是,我是一个几乎完全初学者的片段(和一般的编程),感觉我没有转换我的代码来很好地处理片段。

当我点击转换按钮时,有人可以帮我弄清楚为什么我在gather()方法中有一个空指针异常(也许在其他地方可能是空指针)。也许我在获取有关我的xml的信息或与我的观点有关的事情上做错了。非常感谢谢谢。

package com.shamu11.madlibsportable;

import java.io.IOException;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;

public class Madlibs extends Fragment {
int inLayout; // will hold the id number for layout file
                // main_class_activity_in.xml.
int outLayout; // will hold the id number for layout file
                // main_class_activity_out.xml.
int outviewid; // will hold the id number for the textview found in
                // main_class_activity_out.xml
//Activity activity = this;
String test;
AutoCompleteTextView autoview;
StringBuffer stringbuffer = new StringBuffer();
String[] stringviews = new String[16];

Button convert;

View view;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //super.onCreate(savedInstanceState);

    view = inflater.inflate(R.layout.madlibinput1, container, false);

    //setContentView(R.layout.madlibinput1);
    outLayout = (R.layout.madliboutput1);
    inLayout = (R.layout.madlibinput1);
    outviewid = (R.id.outview14);

    convert = (Button) view.findViewById(R.id.convert);
    convert.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            gather();
            postIt();
        }
    });

    return view;

}

public void gather() {

    ViewGroup layout = (ViewGroup) getView().findViewById(R.id.anskey_clover);
    View[] views = new View[layout.getChildCount()];
    int[] ids = new int[layout.getChildCount()];

    for (int i = 0; i < layout.getChildCount(); i++) {
        ids[i] = layout.getChildAt(i).getId();
    }

    for (int i = 0; i < layout.getChildCount(); i++) {
        AutoCompleteTextView au = (AutoCompleteTextView) getActivity().findViewById(ids[i]);
        stringbuffer.append(au.getText().toString() + "\n");

    }

    //setContentView(outLayout);
    TextView outview = (TextView) getView().findViewById(outviewid);
    outview.setText(stringbuffer.toString());

}

public void postIt() {
    String str = "let's go to the park";
    String str2;
    String newstr = null;

    //setContentView(outLayout);
    TextView outview = (TextView) view.findViewById(outviewid);
    str = (String) outview.getText();
    stringviews = stringbuffer.toString().split("\n"); // turns the
                                                        // stringbuffer from
                                                        // getAllXml() into
                                                        // an array and
                                                        // assigns to
                                                        // stringviews.

    for (int i = 0; i < stringviews.length; i++) {
        str2 = stringviews[i];
        newstr = str.replaceFirst("wx", str2); // replaces all the "wx"s in
                                                // the
                                                // main_class_activity_out
                                                // textview with values from
                                                // strinvgviews.
        str = newstr;
    }
    outview.setText(newstr);
    stringbuffer.delete(0, stringbuffer.length());

    Fragment newFragment = new MadlibsOutput();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    transaction.replace(R.id.fragment1, newFragment);
    transaction.addToBackStack(null);

    transaction.commit();

}

}

madlibinput1.xml:

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.39"
    android:background="#ffffff" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="184dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ffffff" >

        <TextView
            android:id="@+id/outview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="1.) Adjective to Describe:" />

        <TextView
            android:id="@+id/lvheader_song"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="2.) ing-verb (i.e. running):" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="3.) Negative Adjective:" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="4.) Negative Verb:" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="5.) Adjective" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="6.) Plural Noun:" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="7.) Verb (ending in &apos;-ed&apos;):" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/anskey_clover"
        android:layout_width="match_parent"
        android:layout_height="234dp"
        android:orientation="vertical" >

        <AutoCompleteTextView
            android:id="@+id/auview1"
            android:layout_width="141dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.40"
            android:ems="10"
            android:text="la dee dah" />

        <AutoCompleteTextView
            android:id="@+id/auview2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" />

        <AutoCompleteTextView
            android:id="@+id/auview3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" />

        <AutoCompleteTextView
            android:id="@+id/auview4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" />

        <AutoCompleteTextView
            android:id="@+id/auview5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" />

        <AutoCompleteTextView
            android:id="@+id/auview6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" />

        <AutoCompleteTextView
            android:id="@+id/auview7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" />

    </LinearLayout>
</LinearLayout>

<Button
    android:id="@+id/convert"
    android:layout_width="222dp"
    android:layout_height="93dp"
    android:layout_weight="0.25"
    android:text="Get My Mad Lib" />

</LinearLayout>

madliboutput1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/outview14"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:gravity="left"
    android:text="madlib text.........etc" />

</LinearLayout>

LOG CAT:

06-30 23:00:11.530: E/AndroidRuntime(5567): FATAL EXCEPTION: main
06-30 23:00:11.530: E/AndroidRuntime(5567): Process: com.shamu11.madlibsportable, PID: 5567
06-30 23:00:11.530: E/AndroidRuntime(5567): java.lang.NullPointerException
06-30 23:00:11.530: E/AndroidRuntime(5567):     at com.shamu11.madlibsportable.Madlibs.gather(Madlibs.java:78)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at com.shamu11.madlibsportable.Madlibs$1.onClick(Madlibs.java:51)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at android.view.View.performClick(View.java:4438)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at android.view.View$PerformClick.run(View.java:18422)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at android.os.Handler.handleCallback(Handler.java:733)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at android.os.Looper.loop(Looper.java:136)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at android.app.ActivityThread.main(ActivityThread.java:5001)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at java.lang.reflect.Method.invokeNative(Native Method)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at java.lang.reflect.Method.invoke(Method.java:515)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
06-30 23:00:11.530: E/AndroidRuntime(5567):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您尝试通过以下行找到不在片段中的活动中的视图:

AutoCompleteTextView au = (AutoCompleteTextView) getActivity().findViewById(ids[i]);