1

私のアプリは長い数値を収集し、それらを配列に追加します。Viewgroup次に、それらを(番号がキャプチャされた時点で)に追加したいのですが、 NullPointerException.

フラグメントの拡張:

public class detailed extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mContainerView = (ViewGroup) findViewById(R.id.container); //Viewgroup container (item list)
        return inflater.inflate(R.layout.detailed_nodata, container, false);
    }}

そしてaddViewコード:

private void addItem() {
    // Instantiate a new "row" view.
    findViewById(android.R.id.empty).setVisibility(View.GONE);
    final ViewGroup newView = (ViewGroup) LayoutInflater.from(getApplicationContext()).inflate(
            R.layout.detailed, mContainerView, false);

    ((TextView) newView.findViewById(android.R.id.text1)).setText(String.valueOf(dtotal));

    // Set a click listener for the "X" button in the row that will remove the row.
    newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Remove the row from its parent (the container view).
            // Because mContainerView has android:animateLayoutChanges set to true,
            // this removal is automatically animated.
            mContainerView.removeView(newView);

            // If there are no rows remaining, show the empty view.
            if (mContainerView.getChildCount() == 0) {
                findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
            }
        }
    });

    // Because mContainerView has android:animateLayoutChanges set to true,
    // adding this view is automatically animated.
    mContainerView.addView(newView, 0);
    return;
}

ログキャット:

09-28 21:43:58.480    9965-9965/com.jawright.cruisespeed E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity
    at android.view.View$1.onClick(View.java:3633)
    at android.view.View.performClick(View.java:4240)
    at android.view.View$PerformClick.run(View.java:17721)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at android.view.View$1.onClick(View.java:3628)
    ... 11 more
    Caused by: java.lang.NullPointerException
    at com.jawright.cruisespeed.MainActivity.addItem(MainActivity.java:703)
    at com.jawright.cruisespeed.MainActivity.testClick(MainActivity.java:372)
4

1 に答える 1

3

mContainerViewonActivityCreatedメソッドでへの参照を取得してみてくださいFragment

于 2013-09-28T21:31:21.987 に答える