0

私のアクティビティには 3 つのボタンがあります。最初のボタンをクリックすると、グラフ付きのダイアログが表示されます (レイアウト自体は正常に機能します)。

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

            @Override
            public void onClick(View v) {
                 dialog = new Dialog(ChartsDuration.this);
                 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                 dialog.setContentView(R.layout.dialog_charts1);     

// code to show a graph. Here I have a function that calls  drawChartAll(), 
// but since the layout is declared outside the dialog it cannot render it to the
// linearlayout, and my graph1 linearlayout will be empty.

                 dialog.show();


            }
        });

グラフは、次のような外部関数でクエリされるデータを使用します

public void drawChartAll()
   {
//blablabla and this is how I define the layout and render the graph to it:
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);   
mChartView = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values),renderer,Type.DEFAULT);
mChartView.setBackgroundColor(renderer.getBackgroundColor());
layout.addView(mChartView);
}

したがって、ダイアログがなくても、「同じレベル」にあるため、ボタンの下などのgraph1 LinearLayoutにグラフを簡単に表示できますが、ボタンをクリックして開いたダイアログにグラフを表示したいです。もし私がダイアログの中にいたら、私はこれをするでしょう:LinearLayout layout = (LinearLayout)dialog.findViewById(R.id.graph1); しかし、私はダイアログの外にいるので、今はこれを行うことができません.

このレイアウトに到達するにはどうすればよいですか?

編集

user113215 私はこれをしました:

アクティビティで:

 LayoutInflater inflater = LayoutInflater.from(ChartsDuration.this);
 customDialog = (ViewGroup) inflater.inflate(R.layout.dialog_charts1, null);


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

            @Override
            public void onClick(View v) {
                 dialog.setContentView(customDialog);
                 //queries

                 dialog.show();


            }
        });

そしてdrawChartAllで:

   public void drawChartAll()
       {
    //code
     LinearLayout layout = (LinearLayout) customDialog.findViewById(R.id.graph1);
    }

これはあなたが意味するものですか?これにより、行にnullpointer例外がスローされdialog.setContentView(customDialog);ます。

4

2 に答える 2

1

私が問題を正しく理解していれば、ダイアログに入るレイアウト上のものを操作するのに問題があります。を呼び出す代わりにsetContentView(int)、自分でレイアウトを膨張させてから を使用しますsetContentView(View)

LayoutInflater inflater = LayoutInflater.from(this);
ViewGroup customDialog = (ViewGroup) inflater.inflate(R.layout.dialog_charts1, null);

// Do chart things here
// Prefix all calls to findViewById with "customDialog."
LinearLayout layout = (LinearLayout) customDialog.findViewById(R.id.graph1);
mChartView = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values),renderer,Type.DEFAULT); 
mChartView.setBackgroundColor(renderer.getBackgroundColor()); 
layout.addView(mChartView);

// Put the manipulated layout into the dialog
dialog.setContentView(customDialog);

これと同じトリックを使用して、AlertDialog.Builderクラスを利用しながら、ダイアログをカスタム レイアウトで埋めることができます。

于 2012-08-20T19:25:56.357 に答える
0

dialog変数を Activity クラスのフィールドにします。どこからでもアクセスできます。dialog.findviewbyidアクティビティ全体で引き続き使用できます。setcontentView電話をかける前に、電話がかかってきたことを確認してくださいfindviewbyid

since I am outside the dialog.編集:あなたが書いたときに、変数にアクセスできないことを意味したことを願っています

于 2012-08-20T19:44:23.807 に答える