i'm getting an error while using findViewById() - android

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

Related

.init(activity); is showing error [android]

i am trying to implement ShineButton in my project . I have successfully synced the library to the gradle and added shine button in the xml.
now when i am trying to write the java code
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Kill bill", Toast.LENGTH_SHORT).show();
}
});
ShineButton shineButton = (ShineButton) findViewById(R.id.po_image2);
shineButton.init(context);
}
}
.init(activity); is showing cannot resolve symbol activity.
You don't literally copy the code verbatim, you read the documentation and object types supported by the method.
public void init(Activity activity) {
For example, I assume you are running that from an activity based on the usage of findViewById? Then you need "this instance of the Activity"
shineButton.init(this);
or an instance of an Activity if you were in a Fragment
shineButton.init(getActivity());
If the code is in Activity use shineButton.init(Activityname.this).
If it is in
fragment use shineButton.init(getActivity()).
Read this: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/ and this https://developer.android.com/training/index.html
Change:
shineButton.init(context);
To:
shineButton.init(MainActivity.this);
MainActivity.this holds the instance of MainActivity class and can be used to initialise the view.

Annihilate Periodicity of a former activity [ANDROID]

I have been searching for a while a solution to my problem : I have two activities. The first one disappears to let the second one appears after 4 seconds.The problem is every 4 seconds the second activity is relaunched and so my application on my Smart phone is not stable at all (every 4 seconds the second activities appears, then disappears again and again !)
My aim is : after 4 seconds my first activity let place to the second one and the second must stay stable until an action of the user. How can I stop this "periodicity" ?
Here is the code of the two Java file : The first class =
public class MainActivityWelcome extends AppCompatActivity {
private static int TIME_OUT = 4000;
#Override
protected void onCreate(Bundle savedInstanceState) {
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_welcome);
TextView texteView = (TextView) findViewById(R.id.notick);
Typeface font = Typeface.createFromAsset(getAssets(),"fonts/USAAF_Stencil.ttf");
texteView.setTypeface(font);
texteView.setText(Html.fromHtml(getString(R.string.notick)));
final View myLayout = findViewById(R.id.notick);
new Handler().postDelayed(new Runnable() {
#Override
public void run(){
Intent i = new Intent(MainActivityWelcome.this,FirstConnexion.class);
startActivity(i);
finish();
}
}, TIME_OUT);
}
}
And the second class :
public class FirstConnexion extends MainActivityWelcome{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.first_connexion);
TextView texteView1 = (TextView) findViewById(R.id.titlenotick);
Typeface font = Typeface.createFromAsset(getAssets(),"fonts/USAAF_Stencil.ttf");
texteView1.setTypeface(font);
texteView1.setText(Html.fromHtml(getString(R.string.notick)));
}
}
Do you know where I must play to stop this phenomenon ?
Thank you very much !
you are extending MainActivityWelcome to your FirstConnexion that is why super.onCreate(savedInstanceState); again calling MainActivityWelcome's onCreate method and it is again calling your FirstConnexion after 4 seconds . Result Infinite Call
so you have to extend your FirstConnexion with AppCompatActivity
public class FirstConnexion extends AppCompatActivity
public class FirstConnexion extends AppCompatActivity{
}

Android application closes on click

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);
}

failed to start my android activity, javanullpiontException occured

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);

why am i getting this error?? Syntax error on token "void", # expected android programming

please help me with the following code...
i am trying to make an android application and i get an error in the code when i try to create a function inside onCreate..
the need to create a function is to access buttons, labels and textboxes....
here goes my code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText player = (EditText)findViewById(R.id.player);
final TextView plrlbl1 = (TextView)findViewById(R.id.textView1);
final ImageButton imageButton1 = (ImageButton)findViewById(R.id.imageButton1);
final ImageButton imageButton2 = (ImageButton)findViewById(R.id.imageButton2);
public void thisfunction()
{
}
}
please help me create a function inside of oncreate...
help appreciated
public void thisfunction()
{
}
you declare the thisfunction() inside the onCreate(). You should declare outside the onCreate. If you are having this kind of issue I recommend you to read a basic programming book.
please help me create a function inside of oncreate...
You can't, Java doesn't allow it. You have to declare it outside of onCreate().
The correct setup is something like this
public class MyActivity extended Activity {
private TextView myTextView;
#Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate () ;
setContentView(R.layout.main) ;
myTextView = (TextView) findViewById (R.id.textView1) ;
myNewMethod() ;
}
private void myNewMethod () {
myTextView.setText("Hello world") ;
}
}

Categories

Resources