私のアクティビティには 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);
ます。