0

私のコードが間違って書かれている可能性があり、これが私の問題を引き起こしていると思います。基本的に、複数の「プログラム」ボタンを作成しようとしています (これらのボタンは、相対ビュー、線形ビュー、2 つのテキスト ビュー、2 つのボタンで構成されています)。以下のコードを見ることができます。この構造を XML で作成し、アプリに新しいプログラムを追加するときにその XML を拡張しています。次に、データベースの一意の ID に基づいて ID を追加し、このボタンの作成されたすべてのビューに割り当てます。

ボタンをクリックするなど、問題なく動作しているようです。問題は、ボタンを破棄しようとすると、アプリがクラッシュし続けることです。今、私が選んだ方法が根本的に間違っているのではないかと思い始めています。

   public void buildprogramholders(String rowID, String title_text, String description_text) {

        int dbRowID = Integer.parseInt(rowID);

        totalTid++;

        LinearLayout ll = (LinearLayout)findViewById(R.id.traininglist);

        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.trainingprogram, ll);

        tp_container = new RelativeLayout(this);
        tp_container = (RelativeLayout) findViewById(R.id.tp_container);
        tp_container.setId(dbRowID);

        tp_icon = new ImageView(this);
        tp_icon = (ImageView) findViewById(R.id.tp_icon);
        tp_icon.setId(dbRowID);

        tp_textholder = new LinearLayout(this);
        tp_textholder = (LinearLayout)findViewById(R.id.tp_textholder);
        tp_textholder.setId(dbRowID);

        //tp_title = new TextView(this);
        tp_title = (TextView)findViewById(R.id.tp_title);
        tp_title.setId(dbRowID);
        tp_title.setText(title_text);

        tp_description = new TextView(this);
        tp_description = (TextView)findViewById(R.id.tp_description);
        tp_description.setId(dbRowID);
        tp_description.setText(description_text);

        tp_button = new Button(this);
        tp_button = (Button)findViewById(R.id.tp_button);
        tp_button.setId(dbRowID);
        tp_button.setTag(3);
        tp_button.setOnClickListener(this);

        tp_delete_button = new Button(this);
        tp_delete_button = (Button)findViewById(R.id.tp_delete_button);
        tp_delete_button.setId(dbRowID);
        tp_delete_button.setTag(6);
        tp_delete_button.setOnClickListener(this);


    }

次に、OnClick でプログラムを削除しようとします。drop_tp_xx.setVisibitly(View.gone) 領域全体をコメントアウトしてみましたが、プログラムは機能します。そこから1行でも元に戻すと、プログラムが信頼できなくなります。時々 1 つまたは 2 つを削除できますが、その後クラッシュしますか?

    public void onClick(View v) {
int rowId = v.getId();
                    RelativeLayout  drop_tp_container   = (RelativeLayout) tp_container.findViewById(rowId);            
                    ImageView       drop_tp_icon        = (ImageView) tp_icon.findViewById(rowId);
                    LinearLayout    drop_tp_textholder  = (LinearLayout) tp_textholder.findViewById(rowId);
                    TextView        drop_tp_title       = (TextView) tp_title.findViewById(rowId);
                    TextView        drop_tp_description = (TextView) tp_description.findViewById(rowId);
                    Button          drop_tp_button      = (Button) tp_button.findViewById(rowId);
                    Button          drop_tp_delete_button = (Button) tp_delete_button.findViewById(rowId);



                    drop_tp_container.removeAllViews();
                    drop_tp_delete_button.setClickable(false);
                    drop_tp_button.setClickable(false);     
                    drop_tp_title.setVisibility(View.GONE);
                    drop_tp_description.setVisibility(View.GONE);
                    drop_tp_delete_button.setVisibility(View.GONE);
                    drop_tp_textholder.setVisibility(View.GONE);
                    drop_tp_icon.setVisibility(View.GONE);
                    drop_tp_button.setVisibility(View.GONE);
                    drop_tp_container.setVisibility(View.GONE);

    }

エラー ダンプ テキスト

04-04 14:20:06.145: E/AndroidRuntime(17242): java.lang.NullPointerException
04-04 14:20:06.145: E/AndroidRuntime(17242):    at com.mediabarltd.digittrainer.HomePage.onClick(HomePage.java:153)
04-04 14:20:06.145: E/AndroidRuntime(17242):    at android.view.View.performClick(View.java:2538)
04-04 14:20:06.145: E/AndroidRuntime(17242):    at android.view.View$PerformClick.run(View.java:9152)
04-04 14:20:06.145: E/AndroidRuntime(17242):    at android.os.Handler.handleCallback(Handler.java:587)
04-04 14:20:06.145: E/AndroidRuntime(17242):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-04 14:20:06.145: E/AndroidRuntime(17242):    at android.os.Looper.loop(Looper.java:130)
04-04 14:20:06.145: E/AndroidRuntime(17242):    at android.app.ActivityThread.main(ActivityThread.java:3691)
04-04 14:20:06.145: E/AndroidRuntime(17242):    at java.lang.reflect.Method.invokeNative(Native Method)
04-04 14:20:06.145: E/AndroidRuntime(17242):    at java.lang.reflect.Method.invoke(Method.java:507)
04-04 14:20:06.145: E/AndroidRuntime(17242):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
04-04 14:20:06.145: E/AndroidRuntime(17242):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
04-04 14:20:06.145: E/AndroidRuntime(17242):    at dalvik.system.NativeStart.main(Native Method)

見てくれてありがとう。

4

1 に答える 1

0

onClick メソッドの 153 行目に存在しない参照にアクセスしようとしています。

行番号を指定しないので、コード内のどの行かわかりませんが、その行にブレークポイントを設定して、参照が null である理由を確認してください。

アップデート:

findViewById を正しく使用していません。

rootlayout.findViewById(ID)rootlayout が親レイアウトである場所を使用する必要があります。現在、ヌル参照の元であるボタン自体からボタンにアクセスしようとしています。

于 2012-04-04T13:51:10.543 に答える