objectName.java
package randomexcessor.foutuneteller;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class objectName extends AppCompatActivity {
EditText objectInput;
FloatingActionButton actionButton;
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_object_name);
// An intent received and hence been written bellow the Edit_text input to
be shown as consideration.
Intent toAgeInput = getIntent();
final String name = toAgeInput.getStringExtra("users name");
final String display = toAgeInput.getStringExtra("alphabet");
TextView show = (TextView) findViewById(R.id.disclaimer);
show.setText("Please type the name starting with the alphabet, Capital '
" + display+ " '");
objectInput = (EditText) findViewById(R.id.inputName);
actionButton = (FloatingActionButton) findViewById(R.id.outputButton);
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.custom_toast_container));
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/*condition layout which allows user to dial only an alphabet
starting with random selected alphabet from baseInt activity
input.*/
if (!objectInput.getText().toString().matches(display+ "[a-zA-
Z]+"))
{
text = (TextView) layout.findViewById(R.id.errorReport);
text.setText("You have to Start with 'Capital " +
display+"'.");
Toast toast = new Toast(objectName.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.setView(layout);
toast.show();
}
else if (!objectInput.getText().toString().matches("[a-zA-Z]+"))
{
text = (TextView) layout.findViewById(R.id.errorReport);
text.setText("Your name must be in words");
Toast toast = new Toast(objectName.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.setView(layout);
toast.show();
}
else
{
Intent openAge = new Intent(objectName.this,
ageInput.class);
String input = objectInput.getText().toString();
openAge.putExtra("alphabet", display);//alphabet to use
openAge.putExtra("users name", name);//name of the user.
openAge.putExtra("belonging", input);//the object name
provided in this class.
startActivity(openAge);//running activity.
}
}
});
}}
This file is almost working great but whenever I click on the next button it crashes the app
activity_objectName.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/object1bg">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/appbar2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="45dp"
app:srcCompat="#drawable/type15"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/appbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5sp"
android:background="#drawable/edit_background"
android:padding="2dp"
android:weightSum="1">
<EditText
android:id="#+id/inputName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/edit_text"
android:ems="10"
android:inputType="text"
android:padding="10dp"
android:textSize="18sp"
android:layout_margin="3dp"
android:layout_weight="1.02"
android:hint="#string/Object"
android:textColor="#A52A2A"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/outputButton"
android:layout_marginEnd="10dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:backgroundTint="#32CD32"
app:elevation="2dp"
app:fabSize="normal"
app:srcCompat="#drawable/ic_keyboard_arrow_right_white_24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/appbar2"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/button"
tools:ignore="UnknownIdInLayout">
<TextView
android:id="#+id/disclaimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Main problem is here I'm not able to go to this file "ageInput.java",
activity_ageInput.xml
package randomexcessor.foutuneteller;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ageInput extends AppCompatActivity {
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_age_input);
final EditText ageFind = (EditText) findViewById(R.id.inputAge);
FloatingActionButton ageButton = (FloatingActionButton)
findViewById(R.id.ageButton);
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.custom_toast_container));
ageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ageFind.getText().toString().isEmpty())
{
text = (TextView) layout.findViewById(R.id.errorReport);
text.setText("Please type your age or System error.");
text = (TextView) layout.findViewById(R.id.errorReport);
Toast toast = new Toast(ageInput.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.setView(layout);
toast.show();
}
else if(ageFind.getText().length() > 2)
{
text = (TextView) layout.findViewById(R.id.errorReport);
text.setText("Your age cannot be " +
ageFind.getText().toString() +"!!");
text = (TextView) layout.findViewById(R.id.errorReport);
Toast toast = new Toast(ageInput.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.setView(layout);
toast.show();
}
else
{
Intent runResult = getIntent();
final String alpha = runResult.getStringExtra("alphabet");
final String name = runResult.getStringExtra("users name");
final String belong = runResult.getStringExtra("belonging");
Intent finalPage = new Intent(ageInput.this, result.class);
finalPage.putExtra("alphabet", alpha);
finalPage.putExtra("users name", name);
finalPage.putExtra("belonging", belong);
finalPage.putExtra("users age",
ageFind.getText().toString());
startActivity(finalPage);
}
}
});
}
}
activity_ageInput.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/agefact2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
app:srcCompat="#drawable/type13"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="6sp"
android:background="#drawable/edit_background"
android:padding="2dp"
android:weightSum="1">
<EditText
android:id="#+id/inputAge"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/edit_text"
android:ems="10"
android:hint="#string/age"
android:inputType="text"
android:padding="10dp"
android:textSize="18sp"
android:layout_margin="3dp"
android:layout_weight="1.02"
android:textColor="#color/colorAccent"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/ageButton"
android:layout_marginEnd="10dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:backgroundTint="#32CD32"
app:elevation="2dp"
app:fabSize="normal"
app:rippleColor="#7FFF00"
app:srcCompat="#drawable/ic_keyboard_arrow_right_white_24dp" />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
edit_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="1dp">
<stroke
android:color="#696969"
android:width="2dp"/>
<solid android:color="#FFFFFF"></solid>
<corners
android:radius="25dp" />
</shape>
edit_text.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
</shape>
crash report
07-02 13:29:40.966 14586-14586/randomexcessor.foutuneteller E/AndroidRuntime: FATAL EXCEPTION: main
Process: randomexcessor.foutuneteller, PID: 14586
java.lang.OutOfMemoryError: Failed to allocate a 19743564 byte allocation with 15898356 free bytes and 15MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2477)
at android.content.res.Resources.loadDrawable(Resources.java:2384)
at android.content.res.Resources.getDrawable(Resources.java:787)
at android.content.Context.getDrawable(Context.java:403)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:54)
at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:66)
at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at randomexcessor.foutuneteller.ageInput.onCreate(ageInput.java:22)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
The first level of your crash report shows an OOM, which means OutOfMemory. Any Bitmap that you put inside activity_ageInput.xml or that maybe you used in your Toast´s layout is too big (or all bitmaps). You should consider to reduce the size of your bitmaps. Read this thread about handling bitmaps:
https://developer.android.com/topic/performance/graphics/index.html
Related
I am a student who currently taking mobile application development. Although my app can run but it doesnt show any activity instead of just give me W/RecyclerView: No adapter attached; skipping layout. I dont know how to solve the issue, so I was hoping can get some guidlines from experts.
Here is my code
MainActivity.java
package my.edu.utar.practicalassignment;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private Toolbar tool;
private TextView textView;
private RecyclerView recyclerView;
private Button AddBtn;
RecyclerView.LayoutManager layoutManager;
private FirebaseAuth auth;
private DatabaseReference ref;
private String userID = "";
private ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tool = findViewById(R.id.toolbar);
setSupportActionBar(tool);
//getSupportActionBar().setTitle("Today's Spending");
textView=findViewById(R.id.totalCashBack);
recyclerView =findViewById(R.id.recycle_view);
// recyclerView =(RecyclerView)findViewById(R.id.recycleView);
// layoutManager = new LinearLayoutManager(this);
// recyclerView.setLayoutManager(layoutManager);
AddBtn = findViewById(R.id.add_new);
auth = FirebaseAuth.getInstance();
userID = auth.getCurrentUser().getUid();
ref = FirebaseDatabase.getInstance().getReference().child("expenses").child(userID);
// if(userID !=null){
// try{
// ref = FirebaseDatabase.getInstance().getReference().child("expenses").child(userID);
//
// }catch (NullPointerException ex){
// ex.printStackTrace();
// }
// }
progressDialog = new ProgressDialog(this);
AddBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addItem();
}
});
}
private void addItem() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater layout = LayoutInflater.from(this);
View view = layout.inflate(R.layout.input_layout, null);
alert.setView(view);
final AlertDialog dialog = alert.create();
dialog.setCancelable(false);
final Spinner spinnerItem = view.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.category));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerItem.setAdapter(adapter);
final EditText amount = view.findViewById(R.id.insert_amount);
final EditText notes = view.findViewById(R.id.insert_note);
final Button saveButton = view.findViewById(R.id.add_button);
final Button cancelButton = view.findViewById(R.id.cancel_button);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String amountGet = amount.getText().toString();
String notesGet = notes.getText().toString();
String categoryGet = spinnerItem.getSelectedItem().toString();
Drawable icon = getResources().getDrawable(R.drawable.error);
if(amountGet.isEmpty()){
amount.setError("Please enter an amount!!!", icon );
return;
}
if(notesGet.isEmpty()){
notes.setError("Please enter a notes!!!", icon );
return;
}
if(categoryGet.equals("Select a Category")){
Toast.makeText(MainActivity.this, "Please select a valid category", Toast.LENGTH_SHORT).show();
}
else{
progressDialog.setMessage("Had added the information into database");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
String id = ref.push().getKey();
DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
Calendar calendar = Calendar.getInstance();
String date = format.format(calendar.getTime());
Data inform = new Data(categoryGet, date, id, notesGet, Integer.parseInt(amountGet));
ref.child(id).setValue(inform).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(MainActivity.this, "Information added successfully", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Fail to add the information", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
}
dialog.dismiss();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
}
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#000"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffff00"
android:elevation="4dp" />
<TextView
android:id="#+id/totalCashBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:text=" Total Cash Back = RM 0"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="556dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>
<Button
android:id="#+id/add_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:backgroundTint="#ffff00"
android:text="Add"
app:fabSize="normal" />
</LinearLayout>
InputLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:elevation="10dp"
app:cardElevation="10dp"
android:layout_margin="10dp"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="What did you had spend on ?"
android:gravity="center"
android:textColor="#fff"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#ffff00"
android:entries="#array/category"/>
<EditText
android:id="#+id/insert_amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please enter a amount:"
android:textColor="#fff"
android:inputType="number"
android:textColorHint="#fff"/>
<EditText
android:id="#+id/insert_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please enter a note:"
android:textColor="#fff"
android:textColorHint="#fff"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="#+id/cancel_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#000"
android:textStyle="bold"
android:background="#fff"
android:layout_margin="2dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="#+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Transaction"
android:textAllCaps="false"
android:textColor="#000"
android:textStyle="bold"
android:background="#ffff00"
android:layout_margin="2dp"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
strings.xml
<resources>
<string name="app_name">PracticalAssignment</string>
<string-array name="category">
<item>Select a category</item>
<item>Petrol Spend</item>
<item>Groceries Spend</item>
<item>eWallet Transaction</item>
<item>Other Eligible Spend</item>
</string-array>
</resources>
You have to create an adapter class and add with your recycler view.
The basic snippet to set adapter to recycler view is like bellow
YourAdater adapter = YourAdater();
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
for more detail that how to create a RecyclerView AdapterClass you can see this or this
I am getting an error in forgot password activity, while clicking on forgot password the app is stopping and getting an error as
Process: com.example.beasport.sport, PID: 5878
java.lang.IllegalStateException: Could not find method onClickForgot(View) in a parent or ancestor Context for
android:onClick attribute defined on view class
android.support.v7.widget.AppCompatTextView with id 'forgotpassword'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:380)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
My forgot password activity and corresponding xml file are:
ResetForgotActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
public class ResetPasswordActivity extends AppCompatActivity {
private EditText inputEmail;
private Button btnReset, btnBack;
private FirebaseAuth auth;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.forgotpassword);
inputEmail = (EditText) findViewById(R.id.email);
btnReset = (Button) findViewById(R.id.btn_reset_password);
btnBack = (Button) findViewById(R.id.btn_back);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
auth = FirebaseAuth.getInstance();
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = inputEmail.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplication(), "Enter your registered email id", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
auth.sendPasswordResetEmail(email)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(ResetPasswordActivity.this, "We have sent you instructions to reset your password!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ResetPasswordActivity.this, "Failed to send reset email!", Toast.LENGTH_SHORT).show();
}
progressBar.setVisibility(View.GONE);
}
});
}
});
}
}
forgotpassword.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_gravity="center"
android:fitsSystemWindows="true"
tools:context=".Login">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="#string/lbl_forgot_password"
android:textColor="#android:color/white"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center_horizontal"
android:padding="#dimen/activity_horizontal_margin"
android:text="#string/forgot_password_msg"
android:textColor="#android:color/white"
android:textSize="14dp" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:hint="#string/hint_email"
android:inputType="textEmailAddress"
android:textColor="#android:color/white"
android:textColorHint="#android:color/white" />
</android.support.design.widget.TextInputLayout>
<!-- Login Button -->
<Button
android:id="#+id/btn_reset_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="#color/colorAccent"
android:text="#string/btn_reset_password"
android:textColor="#android:color/black" />
<Button
android:id="#+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#null"
android:text="#string/btn_back"
android:textColor="#color/colorAccent" />
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:visibility="gone" />
Why am I getting this error? It would be helpful if anyone could help me to resolve this? Thanks in advance!
My 1st Activity java file source code is not working the way I want. I can not seem to get the animation to run at the start of my 1st activity. I can only get it to run after the 2nd Java Activity by physically going back to the 1st activity by hard pressing back on the Android keyboard.
package com.demotxt.droidsrce.welcomescreen;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
public class WelcomeActivity extends AppCompatActivity {
LinearLayout l1, l2;
Button btnsub;
Animation uptodown, downtoup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
btnsub = (Button) findViewById(R.id.buttonsub);
btnsub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
NextActivity();
}
});
}
public void NextActivity() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
l1 = (LinearLayout) findViewById(R.id.l1);
l2 = (LinearLayout) findViewById(R.id.l2);
uptodown = AnimationUtils.loadAnimation(this, R.anim.uptodown);
downtoup = AnimationUtils.loadAnimation(this, R.anim.downtoup);
l1.setAnimation(uptodown);
l2.setAnimation(downtoup);
}
}
This is my XML File for the 1st Activity and everything here is also running smoothly, but I have the same problem. There seems to be an issue running the java and XML script backward and forward to run the animation both ways.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.demotxt.droidsrce.welcomescreen.WelcomeActivity"
android:orientation="vertical"
android:background="#drawable/background">
<LinearLayout
android:id="#+id/l1"
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:text="Welcome to"
android:textColor="#color/lightorange"
android:textSize="30sp"
android:textStyle="bold" />
<TextView
android:layout_width="398dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text=" BLUEY AUSTRALIA"
android:textAlignment="textStart"
android:textColor="#android:color/holo_blue_light"
android:textColorHighlight="#android:color/holo_blue_bright"
android:textColorHint="#android:color/holo_blue_bright"
android:textColorLink="#android:color/holo_blue_bright"
android:textSize="30sp" />
<ImageView
android:id="#+id/Bluey_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/bluey_logo" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:id="#+id/l2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="#drawable/spaceullustration"
android:orientation="vertical">
<Button
android:id="#+id/buttonsub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/buttonstyle"
android:text="JOIN BLUEY"
android:textColor="#color/bluey"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
try change the order
l1 = (LinearLayout) findViewById(R.id.l1);
l2 = (LinearLayout) findViewById(R.id.l2);
uptodown = AnimationUtils.loadAnimation(this, R.anim.uptodown);
downtoup = AnimationUtils.loadAnimation(this, R.anim.downtoup);
l1.setAnimation(uptodown);
l2.setAnimation(downtoup);
startActivity(intent); //<<<<<<<<<<<<<<,
My problem is that once my editText fields are created, I don't know how to remove them one by one with the other button in my app. I don't really understand the android:id element as it relates to setID() in my java code. I guess my main question is, how do I figure out how to target dynamically created editText views with their id's if I don't know their id's. Here is my code, any help is really appreciated:
Java:
package com.zdowning.decisionmaker;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ScrollView;
import android.widget.Toast;
import java.lang.*;
import static com.zdowning.decisionmaker.R.layout.activity_main;
public class MainActivity extends AppCompatActivity {
public int numberOfLines = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getAnswer();
}
});
final Button add_button = (Button) findViewById(R.id.add_button);
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Add_Line();
}
});
final Button remove_button = (Button) findViewById(R.id.remove_button);
remove_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Remove_Line();
}
});
}
public void Add_Line() {
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setId(numberOfLines++);
et.setText("Enter Answer #" + numberOfLines++);
ll.addView(et);
numberOfLines++;
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void Remove_Line() {
}
public void getAnswer() {
String[] options = new String[numberOfLines];
EditText text = (EditText)findViewById(R.id.editText2);
options[0] = text.getText().toString();
text = (EditText)findViewById(R.id.editText3);
options[1] = text.getText().toString();
text = (EditText)findViewById(R.id.editText4);
options[2] = text.getText().toString();
int number = (int)(Math.random() * 3);
String answer = options[number];
TextView answerBox = (TextView)findViewById(R.id.textView7);
answerBox.setText(answer);
}
}
XML:
<?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"
android:background="#d4cfcd">
tools:context="com.zdowning.decisionmaker.MainActivity">
<LinearLayout
android:id="#+id/linearLayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Your Question Here"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/>
<LinearLayout
android:id="#+id/linearLayoutDecisions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #2" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #1" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:hint="Enter Answer #3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/add_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="+"
android:textColor="#android:color/background_light"
android:textSize="18sp"/>
<View
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="#+id/remove_button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="-"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_margin="10dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/button"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="DECIDE!"
android:textColor="#android:color/background_light"
android:textSize="18sp"
android:layout_centerInParent="true" />
</RelativeLayout>
<TextView
android:id="#+id/textView7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="20dp"
android:textColor="#android:color/black"
android:textSize="36sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
There is no relationship between android:id and your setID.
Check this answer to unsderstand more about that.
Anyway, one approach to achieve to remove a generated line could be based on the children index of your LinearLayout like this:
private LinearLayout mEditTextContainer;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
mEditTextContainer = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
...
}
public void addLine() {
// add edittext
EditText et = new EditText(this);
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Enter Answer #" + (mEditTextContainer.getChildCount()+1));
mEditTextContainer.addView(et);
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
}
public void removeLine() {
mEditTextContainer.removeViewAt(mEditTextContainer.getChildCount()-1);
}
I am beginner in Android Studio. I am trying to run my app on my mobile, but when i try to run my application on my mobile it shows
Unfortunately app has been stopped
Here is the error showed in Logcat android monitor.
10-23 23:19:25.882 18014-18014/com.example.dhara.codesprint E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dhara.codesprint, PID: 18014
java.lang.RuntimeException: Unable to start activity ComponentInfo
{com.example.dhara.codesprint/com.example.dhara.codesprint.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5372)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
at com.example.dhara.codesprint.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5258)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1099)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2239)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5372)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
at dalvik.system.NativeStart.main(Native Method)
Below is the code for MainActivity.java
package com.example.dhara.codesprint;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
TextView text1;
TextView text2;
TextView text3;
TextView text4;
TextView text5;
TextView text6;
EditText text7;
Button checkAnswer;
Button reset;
//private WordDictionary dictionary;
private String currentWord;
private ArrayList<String> words;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
AssetManager assetManager = getAssets();
try {
InputStream inputStream = assetManager.open("words.txt");
} catch (IOException e) {
Toast toast = Toast.makeText(this, "Could not load dictionary", Toast.LENGTH_LONG);
toast.show();
}
text1 = (TextView) findViewById(R.id.textView1);
text2 = (TextView) findViewById(R.id.textView2);
text3 = (TextView) findViewById(R.id.textView3);
text4 = (TextView) findViewById(R.id.textView4);
text5 = (TextView) findViewById(R.id.textView5);
text6 = (TextView) findViewById(R.id.textView6);
text7 = (EditText) findViewById(R.id.edittext);
checkAnswer = (Button) findViewById(R.id.button);
reset = (Button) findViewById(R.id.button2);
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
checkAnswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dhara.codesprint">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Codesprint"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
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:id="#+id/activity_main"
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"
android:background="#color/background"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:layout_marginLeft="13dp"
android:layout_marginStart="13dp"
android:layout_marginTop="14dp"
android:id="#+id/imageView1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:id="#+id/imageView2"
android:layout_alignTop="#+id/imageView3"
android:layout_alignLeft="#+id/imageView5"
android:layout_alignStart="#+id/imageView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:id="#+id/imageView3"
android:layout_alignTop="#+id/imageView1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="24dp"
android:layout_marginEnd="24dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:layout_marginTop="27dp"
android:id="#+id/imageView4"
android:layout_below="#+id/imageView1"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignStart="#+id/imageView1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:id="#+id/imageView5"
android:layout_alignTop="#+id/imageView4"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:id="#+id/imageView6"
android:layout_alignTop="#+id/imageView5"
android:layout_alignLeft="#+id/imageView3"
android:layout_alignStart="#+id/imageView3" />
<EditText
android:text="Your Answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"
android:id="#+id/edittext"
android:textSize="20sp"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColorLink="?attr/actionMenuTextColor" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:id="#+id/textView2"
android:textAlignment="center"
android:textStyle="normal|bold"
android:layout_alignTop="#+id/imageView2"
android:layout_alignLeft="#+id/imageView2"
android:layout_alignStart="#+id/imageView2"
android:layout_alignRight="#+id/imageView2"
android:layout_alignEnd="#+id/imageView2"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignRight="#+id/imageView3"
android:layout_alignEnd="#+id/imageView3"
android:id="#+id/textView3"
android:layout_alignLeft="#+id/imageView3"
android:layout_alignStart="#+id/imageView3"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView4"
android:layout_alignRight="#+id/imageView4"
android:layout_alignEnd="#+id/imageView4"
android:layout_marginTop="30dp"
android:id="#+id/textView4"
android:layout_alignLeft="#+id/imageView4"
android:layout_alignStart="#+id/imageView4"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView4"
android:layout_alignRight="#+id/imageView5"
android:layout_alignEnd="#+id/imageView5"
android:id="#+id/textView5"
android:layout_alignLeft="#+id/imageView5"
android:layout_alignStart="#+id/imageView5"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView5"
android:layout_alignRight="#+id/imageView6"
android:layout_alignEnd="#+id/imageView6"
android:id="#+id/textView6"
android:layout_alignLeft="#+id/imageView6"
android:layout_alignStart="#+id/imageView6"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignRight="#+id/imageView1"
android:layout_alignEnd="#+id/imageView1"
android:id="#+id/textView1"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignStart="#+id/imageView1"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<Button
android:text="Check Your Answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:id="#+id/button"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light"
android:background="#color/colorPrimary"
android:layout_below="#+id/edittext"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Reset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="27dp"
android:id="#+id/button2"
android:background="#color/colorPrimary"
android:textSize="14sp"
tools:textColor="#android:color/background_light" />
</RelativeLayout>
I have tried some solution from web but it is not working.
You layout does not have a Toolbar, and yet you are calling the following:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Either remove these calls or add a Toolbar to your layout.
For the latter to work you also need to use (or derive from) one of the AppCompat themes that do not have an ActionBar, Theme.AppCompat.Light.NoActionBar for example.
So in your styles.xml you would have something like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
You can't initiate your view elements (TextView, Buttons etc) before setting out the layout xml which for your case is R.layout.activity_main file.
package com.example.dhara.codesprint;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//private WordDictionary dictionary;
private String currentWord;
private ArrayList<String> words;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text1 = (TextView) findViewById(R.id.textView1);
TextView text2 = (TextView) findViewById(R.id.textView2);
TextView text3 = (TextView) findViewById(R.id.textView3);
TextView text4 = (TextView) findViewById(R.id.textView4);
TextView text5 = (TextView) findViewById(R.id.textView5);
TextView text6 = (TextView) findViewById(R.id.textView6);
TextView text7 = (TextView) findViewById(R.id.edittext);
Button checkAnswer = (Button) findViewById(R.id.button);
Button reset = (Button) findViewById(R.id.button2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
AssetManager assetManager = getAssets();
try {
InputStream inputStream = assetManager.open("words.txt");
} catch (IOException e) {
Toast toast = Toast.makeText(this, "Could not load dictionary", Toast.LENGTH_LONG);
toast.show();
}
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
checkAnswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
Please initialize your layouts element in onCreate() because when your activity get start it calls onCreate() method ,but here you are calling your layout from your onCreate() method but after that you are not initializing your elements ,also check that whether you have added the name of activity in your manifest or not ,please read and implement carefully ,hope it ll help you.