Adding layout file to class (Java Android Studio) - java

When adding an (empty activity), the class generates with the appropriate layout file but in the setContentview(R.layout.activity_home) it gives an error.
This is my (empty) class:
package com.test.learnlogin;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class HomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
}
And this is the layout file to refer to:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>

I am assuming that the name activity_home inside setContentView is of red color.
if yes and you are sure that activity_home file exists in Res>Layout then please Restart Your Android Studio, that All

You'll have to import R to remove that error.
import com.test.R // assuming your_package_name=com.test
at the top.

Related

Android Custom View's constructor is executed twice

I have faced an issue after creating custom view class in android. The issue is simply, when I create an object of the custom view, its constructor is executed multiple times.
Code of the main Activity Class
public class MainActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
CustomView customView = new CustomView(this, null);
}
}
Code of the customView
package com.example.myapplication;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class CustomView extends View {
public CustomView(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
System.out.println("Custom View is executed");
}
}
Code of main Activity's xml layout file
<?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"
android:weightSum="4"
tools:context=".MainActivity2">
<com.example.myapplication.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.example.myapplication.CustomView>
</androidx.constraintlayout.widget.ConstraintLayout>
The expected result is: the constructor of the view is executed one time
Actual Result: the constructor of the view is executed two times!
You can put a breakpoint in the constructor to examine the call stack and to learn where it is called exactly.
The first invocation comes from setContentView when the layout containing your custom view is inflated.
The second invocation is your explicit constructor call new CustomView(...) in your activity code.

Can't Cannot resolve constructor 'SlideModel(java.lang.String, java.lang.String)'?

I want to add auto slider image with indicator using image link inside my android application, but the error i got is here
Cannot resolve constructor 'SlideModel(java.lang.String, java.lang.String)
Main Activity
<?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">
<com.denzcoskun.imageslider.ImageSlider
android:id="#+id/image_slider"
android:layout_width="wrap_content"
android:layout_height="250dp"
app:iss_auto_cycle="true"
app:iss_period="1000"
app:iss_delay="0"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
Main Activity java
package com.example.imageslider;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import com.denzcoskun.imageslider.ImageSlider;
import com.denzcoskun.imageslider.models.SlideModel;
import java.util.ArrayList;
import java.util.List;
public class MainActivity<val> extends AppCompatActivity {
ImageSlider imageSlider;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
imageSlider = (ImageSlider) findViewById(R.id.image_slider);
final List<com.denzcoskun.imageslider.models.SlideModel> remoteImg = new ArrayList<>();
remoteImg.add(new SlideModel(R.drawable.banner_img, "The animal population decreased by 58
percent in 42 years."));
}
}
I am a beginner in Android Studio Please help me to fix this problem. I will be very
Thanks for them
Use version '0.0.6' of denzconkun:ImageSlideshow
dependencies { implementation 'com.github.denzcoskun:ImageSlideshow:0.0.6' }
https://youtu.be/1AS5HcXPpqk
Try using
remoteImg.add(
new SlideModel(R.drawable.banner_img,
"The animal population decreased by 58 percent in 42 years.",
ScaleTypes.FIT)
);
instead of
remoteImg.add(
new SlideModel(R.drawable.banner_img,
"The animal population decreased by 58 percent in 42 years.")
);
as the constructor have three variables

Android Relative layout button crashes app

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.

Wanting to throw an intent to open a new activity but continually crashes

There's nothing fancy or complicated here. I am just looking to open a new, blank, activity from when I press on the textview. From the projects completed files, I seem to have done it as it wants me to. However, every time I click on the textview to start the intent to open the other activity, it crashes. I cannot seem to find the cause either. In a separate app I've made that send a text string from an edit text field via a button, seems to work fine and that has some other more detailed code to perform that, so I am not entirely sure where the error is arising here.
MainActivity.java
package com.example.android.miwok;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
}
public void openNumbersList(View view) {
Intent i = new Intent(this, NumbersActivity.class);
startActivity(i);
}
}
NumbersActivity.java
package com.example.android.miwok;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class NumbersActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:background="#color/tan_background"
android:orientation="vertical"
tools:context="com.example.android.miwok.MainActivity">
<TextView
android:id="#+id/numbers"
style="#style/CategoryStyle"
android:background="#color/category_numbers"
android:onClick="openNumbersList"
android:text="#string/category_numbers" />
<TextView
android:id="#+id/family"
style="#style/CategoryStyle"
android:background="#color/category_family"
android:text="#string/category_family" />
<TextView
android:id="#+id/colors"
style="#style/CategoryStyle"
android:background="#color/category_colors"
android:text="#string/category_colors" />
<TextView
android:id="#+id/phrases"
style="#style/CategoryStyle"
android:background="#color/category_phrases"
android:text="#string/category_phrases" />
</LinearLayout>
In logcat there's an error showing when the app crashes:
02-20 20:10:26.762 7582-7582/com.example.android.miwok E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.miwok, PID: 7582
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.miwok/com.example.android.miwok.NumbersActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class TextView
In your xml layout for the Text view with id numbers add
android:clickable="true"
Only if this attribute is set to true the text view's onclick handler will be called
And also add height and width to all your textviews.. like this
android:height="wrap_content"
android:width="wrap_content"
This must have caused the app crash
There is a post on how to set click listeners to textviews here .
"Error inflating class TextView" means that your xml in the next activity is faulty. Try removing all the views from the 2nd activity's layout, double check the layout and then try it. If it is just the intent launch you wanna confirm, this should work.

ViewFlipper w/ WebView includes

I am creating a ViewFlipper that flips thru WebViews: I have no problems running the app if I place the WebViews within the main.xml. Since I will be using a multiple count of Web views, I decided to break them up into separate XML files. When I do this using the include android:id="#+id/myWebView001" layout="#layout/pg001" within the ViewFlipper of the main.xml, I get an force close when the app starts.
Please look thru the following code and if you have any suggestions for this to work correctly, it will greatly appreciate it. Thnx again!!
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ViewFlipper"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<include android:id="#+id/myWebView001" layout="#layout/pg001" />
</ViewFlipper>
main.java:
package com.aero.ac4313;
import android.app.Activity;
import android.os.Bundle;
public class main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set your content view, this will be your layout
setContentView(R.layout.main);
}
}
pg001.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myWebView001" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Pg001.java:
package com.aero.ac4313;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Pg001 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set your content view, this will be your layout
setContentView(R.layout.pg001);
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.myWebView001);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/pg001.html");
}
}
The error is obviously simple. Your main activity class is null. I believe that he added Pg001.class without linking it with the main activity class. If you did add it on the manifest file then try again.

Categories

Resources