Android Relative layout button crashes app - java

I'm making an app, now i want to make a button with an image and some text on it.
But every time I click the button my app crashes, android studio does compile however.
This is how i my xml code to make the layout of the screen.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MainActivity">
<RelativeLayout
android:id="#+id/buttonStartTest"
style="#android:style/Widget.Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<ImageView... />
<TextView.../>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
java code:
package com.example.user.ballbounceapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout StartTest = findViewById(R.id.buttonStartTest);
StartTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TestActivity();
}
});
}
public void TestActivity(){
Intent intent = new Intent(MainActivity.this,TestActivity.class);
startActivity(intent);
}
};

You may have forgot to declare your TestActivity inside your manifest.
More about activities in manifest here : https://developer.android.com/guide/topics/manifest/activity-element
So you will need something like this :
<activity android:name="TestActivity">
If the crash persists, the problem might be in your TestActivity 's OnCreate method.

Related

Getting white screen when trying to open the app. App is getting launched on my phone without errors

When I am trying to run my app on my phone it is getting successfully installed in my phone but I am getting white screen instead of my splash page . Attaching my xml code and java code for your reference . Your help is highly appreciated !!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:fontFamily="sans-serif-black"
android:text="MY APP"
android:textColor="#color/Black"
android:textSize="50sp" />
</Relativelayout>
activity_main.java
package com.example.kriova;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);
finish();
}
}
The finish() method is called and the activity destroys and returns to the home screen.
Your code should be like this
package com.example.kriova;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}}
remove the last line which is :-
finish();

Unable to change background color of a fragment

I am new to android and currently learning right now.
I faced a problem related to android-fragments, I write code and it did nothing! Just show the fragment color.
when I tried to remove the color of the fragment, it worked! I am unable to rectify this problem. Please help!
XML -activity_main.xml
<?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=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="113dp"
android:layout_height="50dp"
android:text="Switch1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.945" />
<fragment
android:id="#+id/fragment"
android:name="com.example.fragments.Color_Purple"
android:layout_width="390dp"
android:layout_height="385dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.68"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.137" />
</androidx.constraintlayout.widget.ConstraintLayout>
XML - Fragment code
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".Color_Purple">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C403FA" />
</FrameLayout>
JAVA-mainActivity.java
package com.example.fragments;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button b1;
Fragment f1;
Boolean flag=true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
f1=new Color_Purple();
// Toast.makeText(getApplicationContext(),"Button pressed",Toast.LENGTH_SHORT).show();
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
if(flag)
{
findViewById(R.id.fragment).setBackgroundColor(Color.GREEN);
// Toast.makeText(getApplicationContext(),"Button pressed Color GREEN",Toast.LENGTH_SHORT).show();
flag=false;
}
else
{
findViewById(R.id.fragment).setBackgroundColor(Color.RED);
//Toast.makeText(getApplicationContext(),"Button pressed Color RED",Toast.LENGTH_SHORT).show();
flag=true;
}
}
});
}
}
Java - fragment java code
package com.example.fragments;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Color_Purple extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_color__purple, container, false);
}
}
Note:
If i remove android:background from xml - fragment, this code works but not with color.
just change :
Boolean flag = true;
to
boolean flag = true;
and remove android:background="#C403FA" from your TextView :
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C403FA" //remove this line
/>

Facing problem in findView (Android Studio)

I'm new on Android Studio and I'm working on a project.
I'm facing this error
I've declared listview but don't know why it's not working.
CODE:
package com.example.animation;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class view_rooms extends AppCompatActivity {
ListView listView;
String mTitle[]={"Room1","Room2","Room3","Room4","Room5"};
String mDescription[]={"Access Room1","Access Room2","Access Room3","Access Room4","Access Room5"};
int images[]={R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_rooms);
listView=findViewById(R.id.ListView);
}
findViewById() searches the view by id into the layout file. In this case into the layout: R.layout.activity_view_rooms. You need to define into this layout one ListView widget and assign one id to search with findViewById().
For example into the layout activity_view_rooms you need to include the view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list_view_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
And later you can get the instance of view with finViewById() in this way:
package com.example.animation;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class view_rooms extends AppCompatActivity {
ListView listView;
String mTitle[]={"Room1","Room2","Room3","Room4","Room5"};
String mDescription[]={"Access Room1","Access Room2","Access Room3","Access Room4","Access Room5"};
int images[]={R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_rooms);
listView=findViewById(R.id.list_view_id);
}
You need to add this line android:id="#+id/ListView" in your activity_view_rooms.xml
So the code will looks like this:
<ListView
android:id="#+id/ListView"
android:layout_width="match_parent"
android:layout_height="match_parent">

Eclipse error : button1 cannot be resolved or is not a field

Here is my MainActivity.java code :
package com.dg.buttontest3;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("dg","button was clicked");
}
}
And here is the activity_main.xml code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="18dp"
android:text="Button" />
</LinearLayout>
I am getting the following error while running this code on Eclipse ADT : button1 cannot be resolved or is not a field.
I don't understand what is wrong with my code. Please help.
You haven't posted your XML code, but as this code seems to be working properly:
button = (Button)findViewById(R.id.button1);
I'd guess that in your activity_main.xml you never create something called button1, or that you don't use the #+id/ prefix.
It should look something like:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button1"/>
Just realised something else, you need to change your LinearLayout opening tag so it has a >.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
That might cause the button to never properly be recognised.

displaying blogs in webview

I want to display my blog as a part of an android application..i have the code ready..it works well when displaying websites like google etc but not with my blog..can someone help me solve this problem
mainActivity.java:
package com.mkyong.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);
}
});
}
}
webViewActivity.java:
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("pavan7vasan.blogspot.com");
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/buttonUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to http://www.google.com" />
</LinearLayout>
webview.xml:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
it shows web page not available in the android emulator
Change this line, and it should work.
webView.loadUrl("http://pavan7vasan.blogspot.com");

Categories

Resources