1
I need to search the controls to change values in the layout. But passing the context by parameter to the class it does not find the controls declared in xml returning null for each cast made.
The class where I am trying to recover this data extends Asynctask:
public class WSCatalogo91 extends AsyncTask<Void, ProgressAux, Boolean>
private ProgressBar mProgressBar;
private TextView mProgressText;
public WSCatalogo91(WSCatalogo91Listener listener) throws SQLException {
this.listener = listener;
dh = new DatabaseHelper((Activity) listener);
confDao = new ConfiguracaoDao(dh.getConnectionSource());
pDao = new ProdutoDao(dh.getConnectionSource());
mProgressBar = (ProgressBar) ((Activity) listener).findViewById(R.id.progressBar);
mProgressText = (TextView) ((Activity) listener).findViewById(R.id.progressBarTextView);
auxProgresso = new ProgressAux();
}
mProgressBar and mProgressText are null, but my context there is not null, so much so that it is being used on the top lines perfectly. Follow the xml code where I declare the ids.
<TextView android:text="@string/progressBar" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/progressBarTextView"
android:maxWidth="400dp"
android:maxHeight="400dp"/>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:indeterminate="false"
android:layout_centerHorizontal="true"
android:layout_marginTop="165dp"
android:minWidth="300dp" />
tried to use
mProgressBar = (ProgressBar) MainActivity.findViewById(R.id.progressBar)
?– Paulo Roberto Rosa
@Pauloroberto this class is not declared within the Activity, I believe it would not be a good practice.
– Luiz Negrini
because this class is not declared within Activity? why do you think it would not be good practice? is that on android I usually use Imports or searching from Mainactivity as I told you to find xml elements
– Paulo Roberto Rosa
In order for you to find these elements, you must call the Activity that is with the
this.setContentView(R.layout.nomedoseuLayout)
set in theonCreate()
and uses it as a namespace instead ofMainActivity
in the example I showed you– Paulo Roberto Rosa
@Pauloroberto Put as an answer, if that is what I already put as a right answer and you earn your points. I await the answer.
– Luiz Negrini
The correct thing is to test if it worked first because I didn’t have time to test, it might be?
– Paulo Roberto Rosa
@Pauloroberto I don’t understand very well what you mean, in onCreate() that is within the Activity the setContentView is working normally. But when I pass the context to my other class, I’ll have to do it again in the same process?
– Luiz Negrini
It depends, whether "your other class" for an Activity, you can do it yes, it will work. But if it’s not, then you have to import your Activity and use its name as namespace
SuaActivityQueTemOSetContentViewFuncionando.findViewById(R.id.progressBar)
– Paulo Roberto Rosa
Let’s go continue this discussion in chat.
– Luiz Negrini