public class BouncingBallActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
View bouncingBallView = new BouncingBallView(this);
super.onCreate(savedInstanceState);
setContentView(bouncingBallView);
}
}
Please note:
Make sure all your activities are listed in the manifest file
Make sure BouncingBallView class has a constructor of the type public
BouncingBallView(Context context)
{
super(context);
}
BouncingBallView must extend View
check all your code. check whether you are returning null anywhere.
Post your logcat trace and and try and pin point where exactly are you going wrong.
View bouncingBallView = new BouncingBallView(this);
put this line after super.onCreate(savedInstanceState);
Related
i want to use the method (findViewById) but i get an error which i cant solve.
the error is "Type Parameter T has incompatible upperbounds: View and RatingBar".
For fixing this i tried closing the activity and opening another one but it didn't work.
public class RatingBar extends AppCompatActivity {
RatingBar got , bd;
TextView gottxt , bdtxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating_bar);
init()
}
private void init(){
got = findViewById(R.id.gotrate);
}
}
you are using RatingBar as your activity name. It's the built-in class in Android. Change name of your Activity and try again
Here is Problem : ` got = findViewById(R.id.gotrate); && Also Change Name of Activity becauase it is reserved Word
`
it will like this :
public class MyActivity extends AppCompatActivity {
RatingBar got , bd;
TextView gottxt , bdtxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating_bar);
init()
}
private void init(){
got = (RatingBar)findViewById(R.id.gotrate);
}
declare RatingBar as final (eg. Final RatingBar or Final TextView)and see if it can solve the issue
I am working on an app with a RecyclerView, and I thought I almost had it running, but ran into a strange error.
I had made a few small changes to correct a small bug, and after that the app would no longer finish setting up the main activity layout. Instead I got the following screens:
App closed --- App keeps closing
My first thought was to look back at my small changes. But they did not cause this problem. (Undoing those changes did not fix the problem.)
I now believe that the problem is related to a change in the way the app operates between the first "successful" (though buggy) run and the following runs that fail.
In the first run, the app had to request permission from the user in order to access the documents folder. But after that, the app no longer needs to ask, because the user has already granted permission.
This means the order of execution has changed prior to the RecyclerView layout being created. But I can't (yet) figure out what's going wrong or how to fix it.
Here's the OnCreate() method in my main activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prog_summary);
}
Here's the OnCreate() method & other related methods in the superclass (the class that implements the permissions request):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.state = savedInstanceState;
if (state != null) {
isInPermission = state.getBoolean(STATE_IN_PERMISSION, false);
}
if (hasAllPermissions(getDesiredPermissions())) {
onReady(state);
}
else if (!isInPermission) {
isInPermission = true;
ActivityCompat.requestPermissions(this,
netPermissions(getDesiredPermissions()),
REQUEST_PERMISSION);
}
}
#Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults) {
isInPermission = false;
if (requestCode == REQUEST_PERMISSION) {
if (hasAllPermissions(getDesiredPermissions())) {
onReady(state);
}
else {
onPermissionDenied();
}
}
}
Here's the OnReady() method in my activity class (called when permission is granted):
#Override
protected void onReady(Bundle savedInstanceState) {
_alEntries = new ArrayList();
TaskData tdSource = new TaskData();
// load task item array & trim the excess (unused) ArrayList space
tdSource.LoadData(this, _alEntries);
_alEntries.trimToSize();
// create summary item array & populate it based on task item array
_alSummaries = new ArrayList();
PopulateSummaryList();
_rv = (RecyclerView) findViewById(R.id.rvBookList);
_li = getLayoutInflater();
_rv.setLayoutManager(new LinearLayoutManager(this));
_adapter = new TaskItemAdapter();
_rv.setAdapter(_adapter);
}
The app actually closes when the call to setLayoutManager is executed. Any thoughts on where I've gone wrong or how to proceed in tracking this down?
Seems like it could be a NullPointerException in that your _rv variable is null. Judging by the code it makes sense, your superclass's onCreate method calls onReady() BEFORE setContentView() is called in your subclass's onCreate therefore by the time onReady() is called in your subclass findViewById(R.id.rvBookList) will return null as you haven't set the content of the class yet. To fix this I recommend making your superclass an abstract class. For example, in your superclass do this:
public abstract class BaseActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
this.state = savedInstanceState;
// ... remaining code from your superclass's onCreate()
}
public abstract int getLayoutId();
}
Now in your MainActivity, extend BaseActivity. Since BaseActivity is abstract and getLayoutId() is an abstract method, it will force you to implement getLayoutId() like so:
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public int getLayoutId() {
return R.layout.activity_prog_summary;
}
}
Doing this will ensure that all classes that inherit your superclass will perform the correct order of operations, in that setContentView is called before anything else.
I do my android app , but I have a bug I don't know how to fix it.
My code is below :
This is my Main Activity :
public class MainActivity extends Activity {
private ImageView imgHot;
public final static String EXTRA_MESSAGE="com.cuonglm.KhoHinh.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgHot=(ImageView)findViewById(R.id.imageViewHot);
imgHot.setOnClickListener(toContentHot);
}
View.OnClickListener toContentHot=new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent content=new Intent(MainActivity.this,ContentActivity.class);
String signal="1";
content.putExtra(EXTRA_MESSAGE,signal);
startActivity(content);
}
};
And this is my second Activity :
public class ContentActivity extends Activity {
private TextView viewMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent content=getIntent();
String messageReceive=content.getStringExtra(MainActivity.EXTRA_MESSAGE);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
setContentView(R.layout.activity_content);
}
I want to click on the image on the Main Activity , string "1" or number "1" will send to the Second Activity via Intent and view on the TextView.
But my app will be close "Unfortunately..."
Thanks
Change to
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
In your ContentActivity
findViewById looks for a view with the id for the current infalted layout. SO you need to set the content of your layout to the activity first and then initialize your views.
You are probably gettting NullPointerException coz your initialization fails.
You need to call setContentView() in your second Activity before trying to access any of the Views in that layout. Change it to
public class ContentActivity extends Activity {
private TextView viewMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
Intent content=getIntent();
String messageReceive=content.getStringExtra(MainActivity.EXTRA_MESSAGE);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
}
If this doesn't fix your problem then please post your logcat so we can see the error. Also always post logcat in the future when your app crashes. They aren't always this easy to see.
Also, I'm not sure you understand how putExtra() works. It is a key, value pair so when you put EXTRA_MESSAGE as the key then that is what you would use to retrieve the value added in the second param. So the way you are doing it may work if the Activity gets destroyed but it looks really strange to me and probably not realy safe or efficient. I would change it to something like
content.putExtra("message",signal);
in your first Activity then get it with
String messageReceive = content.getStringExtra("message");
in your second Activity
You need to set the layout before trying to reference the Views associated with it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent content=getIntent();
String messageReceive=content.getStringExtra(MainActivity.EXTRA_MESSAGE);
setContentView(R.layout.activity_content);
viewMessage=(TextView)findViewById(R.id.content_receive);
viewMessage.setText(messageReceive);
}
I stuck at this issue many times and I passed the problem in different ways and I'm not sure that I made it in the right way.
I simplified the problem in a the following example. I know that I can pass only the data to the class but I do want to pass the editText cause I have this problem with more difficult UI controls.
mainactivity.java
public class mainactivity extends Activity {
public EditText clickEditText;
int count =0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
newTxt();
}
public void newTxt() {
txt = new MyText(context);
txt.updateTextEdit("Main Activity");
}
}
myText.java
public class MyText
{
private Context _context;
// constructor
public MyText(Context context)
{
_context = context;
}
public void updateTextEdit(String str)
{
private EditText strEditText;
strEditText= (EditText)findViewById(_context.R.id.editTextClick); // ????
strEditText.setText(str + " and myTxt");
}
}
if you could explain me how to fix the updateTextEdit function. i passed the context of the main activity. How can I change the editText? Thank you very much!!!
If you really want to do this this way, you need to save a reference to Activity, not Context. Like this:
public class MyText
{
private Activity _activity;
// constructor
public MyText(Activity activity)
{
_activity= activity;
}
public void updateTextEdit(String str)
{
private EditText strEditText;
strEditText= (EditText)activity.findViewById(R.id.editTextClick);
strEditText.setText(str + " and myTxt");
}
}
and in newTxt() you will need to change:
txt = new MyText(context);
to:
txt = new MyText(this);
But wouldn't it be easier to just put this method inside your activity? Why do you want it in another class? If it really needs to be in another class, you could make that class an inner class of your activity and you would still have access to the activity's methods and member variables.
There's a similar question here
How to access Activity UI from my class?
You didn't say how you obtained the context, you should use this and get the mainactivity in the other class. not context.
then you can call runOnUIThread to perform UI updates.
If I have a reference to Context, is it possible to finish the current activity?
I don't have the reference to current activity.
yes, with a cast:
((Activity) ctx).finish();
In my Case following worked,
I need to finish my activity in a AsyncTask onPostExcute().
Where my AsyncTask class is separate public class , which has a constructor with param of Context.
((Activity)(mContext)).finish();
Only the above worked for me... Anyway I got this idea from #2red13 and #lucy answers... Thanks to all...
I know it's an old post but, perhaps it could be a good idea to call it this way:
if(context instanceof Activity){
((Activity)context).finish(); }
This way we make sure we don't get any unnecesary ClassCastExceptions
If you have access to the running view of the Activity you want to finish (for example, you are in a click listener), you could do:
((Activity)getContext()).finish();
(With thanks to 2red13 to get me here).
If you start the activity using:
startActivityForResult(i, 1);
you can call finishActivity(1) to finish any activities started with that request code, like this:
((Activity)getContext()).finishActivity(1);
In my case I need to use one in a handler postDelayed. Using this you can be sure of which activity you are finishing. Hope it helps!
I had the same problem when closing a preference activity. Here is what I did:
public class T2DPreferenceActivity extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new T2DPreferenceFragment()).commit();
}
public static class T2DPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.server_screen_preference);
Preference testServicePreference = getPreferenceScreen().findPreference("PREFERRED SERVER");
testServicePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
T2DPreferenceActivity.closeActivity(getActivity());
return true; //returning true still makes the activity handle the change to preferences
}
});
}
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ListView lv = (ListView)view.findViewById(android.R.id.list);
ViewGroup parent = (ViewGroup)lv.getParent();
parent.setPadding(0, 100, 0, 0);
}
}
protected static void closeActivity(Activity activity) {
activity.finish();
}
}
Try:
((Activity) context.getApplicationContext()).finish();