I recently started developing apps, and I've gotten my first gig. It's a simple app with a logo and a button linking to a website. Everytime I run it, it crashes, and I'm stumped as to why, as it's such a simple program. I've spent a lot of time looking for similar problems on SO, but to no avail. I've also gone through eclipse and eliminated any compiler warnings. Does anyone have an idea as to what could have gone wrong?
Error messages:
I've kept cutting things out over and over to the app's most simple function. It still won't work. Here is my .java and main activity. (It's a one activity and one class app)
//necessary imports are omitted. I get no errors for imports
public class FullscreenActivity extends Activity {
Button button = (Button) findViewById(R.id.button1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
goToUrl("http://switchemup.com");
}
});
}
private void goToUrl(String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.fullscreen, menu);
return true;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".FullscreenActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:src="#drawable/switchuplogo" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="94dp"
android:text="View Website" />
</RelativeLayout>
This line Button button = (Button) findViewById(R.id.button1); should create an error and this should cause the crash you app. You set a variable which contains a method that should be in onCreate method.
Try this instead:
// init your variable only
Button button;
// Then in onCreate, as you already did:
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
// set findViewById method only here
button = (Button) findViewById(R.id.button1);
// ...
}
Let me know if this helps.
Related
I think it a very easy Question. I am new to Android Studio and I could not find an answer.
I have an activity that switches per Button to another activity. However, on my second activity, my simple Button just to change the Textview which doesn't work.
Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartActivity">
<TextView
android:id="#+id/twText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="127dp"
android:layout_marginLeft="127dp"
android:layout_marginTop="88dp"
android:layout_marginEnd="127dp"
android:layout_marginRight="127dp"
android:layout_marginBottom="624dp"
android:text="Ihr Passwort war Richtig!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btnChecke"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="134dp"
android:layout_marginLeft="134dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="189dp"
android:layout_marginRight="189dp"
android:layout_marginBottom="371dp"
android:text="Check"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
onCreate method in my second activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
tw = findViewById(R.id.twText);
btnCheck = findViewById(R.id.btnChecke);
btnCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tw.setText("work");
}
});
}
And this is how I am starting the second activity (onCreate method from my first activity).
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
tw = findViewById(R.id.twText);
Button btnCheck = findViewById(R.id.btnChecke);
btnCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView("Work"R.layout.activity_main);
}
});
}
I have checked the variable names in the layout and looks like everything is fine.
Thank you in advance.
This answer is based on the problem which I found before the latest edit from the OP. Here is the version of the edit where I found the problem.
First of all, something is very wrong about the onClickListener of your button. You are setting the content view again, instead of starting your second activity. It looks like, you are just setting the content view of your StartActivity which actually does not launch the MainActivity that you are trying to start.
In order to start a new activity, you need to add the following in your button onClickListener.
btnCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// setContentView(R.layout.activity_main); // This is not the way for starting another activity
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
});
Once your MainActivity has started, you should be able to change the text with no problem.
Bundle bundle = getIntent().getExtras()
if(bundle.getString("changeTextView")
TextView .... = (TextView) ....setText();
Hi. If I good understand, you should use Intent with "putExtra()".
Exmple:intent.putExtras("changeTextView");
After that in your onCreate() method you can put:
I have a problem with creating 2 activities for 1 button. I created a button and I managed to connect that button with one activity but I dont know how to add another activity to the same button.. What am I trying to do is that when we press on that button it will start playing animation (I managed to do that) but I also want it to send SMS to a specific number. I have code for that, but I dont know how to include / connect everything together.
Code from a button:
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" />
Code from a imageview (animation):
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:scaleType="centerInside"
android:src="#drawable/keers" />
Code from main activity, for animation:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
button = (Button) findViewById(R.id.button);
running = false;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(!running){
((AnimationDrawable) imageView.getDrawable()).start();
running = true;}
}
}
);
}
}
Now what I want to add for the same button is this :
public void sendText(View paramView)
{
Toast.makeText(this, "SENT",Toast.LENGTH_SHORT).show();
SmsManager.getDefault().sendTextMessage("+3564245237",null,"1", null, null);
I'm not that experienced in programming, so it might be pretty easy for you guys.
Both actions seen to be fired by the same Button, so you may add both to the onClickListener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(!running){
((AnimationDrawable) imageView.getDrawable()).start();
running = true;}
Toast.makeText(view.getContext(), "SENT",Toast.LENGTH_SHORT).show(); //Or YourActivity.this
SmsManager.getDefault().sendTextMessage("+3564245237",null,"1", null, null);
}
}
i have trouble to call layout programmatically, i try in xml using include and its work
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="#+id/btnTes"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/LL1"
android:orientation="vertical">
<include layout="#layout/extend">
</include>
<include layout="#layout/extend">
</include>
</LinearLayout>
but i want create it programmatically
this is my extend XML :
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView" />
<TextView
android:text="TextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2" />
and this is my java :
public class MainActivity extends AppCompatActivity {
Button btnTes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup tes = (ViewGroup) findViewById(R.id.LL1);
btnTes = (Button) findViewById(R.id.btnTes);
final View extend = LayoutInflater.from(this).inflate(R.layout.extend,null);
btnTes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "KLIk", Toast.LENGTH_SHORT).show();
tes.addView(extend);
}
});
}
}
when i click btnTes
for the first clicked its ok, but when i click it again my program just force close. This is my error
FATAL EXCEPTION: main
Process: com.example.siwonhansamu.test, PID: 3796
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Thanks
You can't add the same view to multiple parents.
You have to do a copy of the view if you want it multiple times. You could do like this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup tes = (ViewGroup) findViewById(R.id.LL1);
btnTes = (Button) findViewById(R.id.btnTes);
btnTes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "KLIk", Toast.LENGTH_SHORT).show();
final View extend = LayoutInflater.from(view.getContext()).inflate(R.layout.extend, tes, false);
tes.addView(extend);
}
});
}
when i click btnTes for the first clicked its ok, but when i click it
again my program just force close.
that's the expected behavior. A View's instace can have only one parent. When you press the button for the second time extend has already a parent (tes), and you cannot call addView again on that instance. The quick fix is to move
final View extend = LayoutInflater.from(this).inflate(R.layout.extend,null);
into onClick. This way, every time you press on the Button, a new instance is created.
Could anyone please help me with the below java code as I want to create a image slideshow without a click of a button. I want the view flipper to automatically switch through the different iamges without a click of a button. I want it to keep on showing all the images again and again, without a click of a button. I have deleted the button in my XML file as I dont require it.
Java File Code
public class MainActivity extends Activity {
int mFlipping = 0 ; // Initially flipping is off
Button mButton ; // Reference to button available in the layout to start and stop the flipper
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Click event handler for button */
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.flipper1);
if(mFlipping==0){
/** Start Flipping */
flipper.startFlipping();
mFlipping=1;
mButton.setText(R.string.str_btn_stop);
}
else{
/** Stop Flipping */
flipper.stopFlipping();
mFlipping=0;
mButton.setText(R.string.str_btn_start);
}
}
};
/** Getting a reference to the button available in the resource */
mButton = (Button) findViewById(R.id.btn);
/** Setting click event listner for the button */
mButton.setOnClickListener(listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
XML File
<ViewFlipper
android:id="#+id/flipper1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:flipInterval="3000"
android:inAnimation="#android:anim/slide_in_left"
android:outAnimation="#android:anim/slide_out_right"
android:layout_centerInParent="true"
>
<ImageView
android:src="#drawable/img1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/str_img1"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:src="#drawable/img2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/str_img2"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:src="#drawable/img3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/str_img3"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:src="#drawable/img4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/str_img4"
android:layout_gravity="center_horizontal"
/>
<ImageView
android:src="#drawable/img5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/str_img5"
android:layout_gravity="center_horizontal"
/>
</ViewFlipper>
</RelativeLayout>
For(int mflipping:0;mflipping less than total images-1 length; mflipping++)
Curly brases starts
Flipper.startflipping();
//Delay for sometime
Curly Brases End
Flipper.stopflipping();
Sodu code,write your code now
Just remove button listener and try.
public class MainActivity extends Activity {
int mFlipping = 0 ; // Initially flipping is off
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.flipper1);
if(mFlipping==0){
/** Start Flipping */
flipper.startFlipping();
mFlipping=1;
}
else{
/** Stop Flipping */
flipper.stopFlipping();
mFlipping=0;
}
}
};
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Its a simple login screen with two edit text one checkbox and a imagebutton ... it was working fine recently i have changed Button to imageButton and now it giving me problem
logcat
04-30 02:11:24.699: E/AndroidRuntime(9571): FATAL EXCEPTION: main
04-30 02:11:24.699: E/AndroidRuntime(9571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.markitberry/com.example.markitberry.Login}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.EditText
04-30 02:11:24.699: E/AndroidRuntime(9571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
04-30 02:11:24.699: E/AndroidRuntime(9571): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-30 02:11:24.699: E/AndroidRuntime(9571): at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-30 02:11:24.699: E/AndroidRuntime(9571): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
here is java
public class Login extends Activity implements AnimationListener {
ImageButton btnLogin;
EditText inputEmail,inputPassword;
CheckBox loginRemember;
// Animation
Animation animBounce1,animBounce2,animBounce3,animBounce4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// load the animation
animBounce1 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_out);
animBounce2 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_out_right);
animBounce3 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move_out);
animBounce4 = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);
// set animation listener
animBounce1.setAnimationListener(this);
animBounce2.setAnimationListener(this);
animBounce3.setAnimationListener(this);
animBounce4.setAnimationListener(this);
inputEmail=(EditText)findViewById(R.id.etloginEmail);
inputPassword=(EditText)findViewById(R.id.etloginPassword);
loginRemember=(CheckBox)findViewById(R.id.cbRemember);
ImageButton btnLogin =(ImageButton) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
inputEmail.startAnimation(animBounce1);
inputPassword.startAnimation(animBounce2);
loginRemember.startAnimation(animBounce3);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_call:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9871952704"));
startActivity(callIntent);
// help action
return true;
case R.id.action_email:
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","markitberry#gmail.com", null));
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "message");
startActivity(Intent.createChooser(intent, "Choose an Email client :"));
case R.id.action_locate:
Intent i = new Intent(Login.this, Locate.class);
startActivity(i);
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if (animation == animBounce3) {
Intent it=new Intent(Login.this,Home.class);
startActivity(it);
}
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
}
login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/login_bck"
android:gravity="center">
<include
layout="#layout/login_cover"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
login_cover.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/login_gradient_bck"
android:orientation="vertical" >
<EditText
android:id="#+id/etloginPassword"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/cbRemember"
android:layout_alignLeft="#+id/etloginEmail"
android:layout_alignRight="#+id/etloginEmail"
android:layout_marginBottom="26dp"
android:background="#BFFFFFFF"
android:ems="10"
android:gravity="center"
android:hint="Enter Password"
android:inputType="textPassword"
android:textColor="#FFFFFF" />
<EditText
android:id="#+id/etloginEmail"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/etloginPassword"
android:layout_centerHorizontal="true"
android:layout_margin="30dp"
android:layout_marginBottom="36dp"
android:background="#BFFFFFFF"
android:ems="10"
android:gravity="center"
android:hint="Enter Email"
android:inputType="textEmailAddress"
android:textColor="#FFFFFF" />
<CheckBox
android:id="#+id/cbRemember"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/btnLogin"
android:layout_alignLeft="#+id/etloginPassword"
android:layout_alignRight="#+id/etloginPassword"
android:layout_marginBottom="62dp"
android:text="Remember Me"
android:textColor="#E6E6E6" />
<ImageButton
android:id="#+id/btnLogin"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:background="#drawable/button"
android:src="#drawable/login_icon_2" />
</RelativeLayout>
Maybe you're having this problem because you didn't change your Button control to ImageButton in your view (login.xml). If that's not the case, you should clean your project and build it again, the R.java file sometimes does not refresh it self with the new changes (this issue is kind of weird, but sometimes it happens).
Just for the record... when this kind of problem happens and you have your code with no errors, it's because you changed the position of your control (For example... your Button was at the beginning but after a while you moved it to the end of the layout) for some reason the project doesn't realize this change and still thinks your Button is the first control in the layout.
Sorry to write this suggestion in an answer... I don't have enough points to comment in your question.
Button extends TextView while ImageButton extends ImageView. Kind of wierd, but that's Android for you.
I would suggest possibly using Button then using android:background="#drawable/MyDrawable" and see if that will achieve what you are trying to do.
You are using the ImageButton two times in you class:
ImageButton btnLogin;
And;
ImageButton btnLogin =(ImageButton) findViewById(R.id.btnLogin);
Please remove the ImageButton btnLogin; if you are using the ImageButton somewhere else..
Secondly, note that you are not registering the ImageButton anywhere in the onCreate()..
My suggestion, just remove the ImageButton btnLogin; before the onCreate() and try it.
Or change this :
ImageButton btnLogin =(ImageButton) findViewById(R.id.btnLogin);
to:
btnLogin =(ImageButton) findViewById(R.id.btnLogin);
Should be working now.