When i try to run my app i get this error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.mysolver/com.example.mysolver.MainActivity}:
java.lang.ClassCastException:
androidx.constraintlayout.widget.ConstraintLayout cannot be cast to
android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to
android.widget.Button
at com.example.mysolver.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Other people have had this problem but none of the solutions worked for me.
Here is my "content_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:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:onClick="onClick"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:hint="Enter letters"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<ScrollView
android:id="#+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/output"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Output will appear here..."
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Here's my "activity_main.xml":
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include
android:id="#+id/button"
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Here's my "MainActivity.java":
package com.example.mysolver;
import android.content.res.AssetManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class MainActivity extends AppCompatActivity {
Button butt;
EditText text;
String letters;
TextView mOutput;
processData procDat;
public static final String TAG = MainActivity.class.getName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
Log.d(TAG, "Set up tool bar");
setSupportActionBar(toolbar);
text = findViewById(R.id.editText);
Log.d(TAG, "Set up text");
mOutput = findViewById(R.id.output);
butt = findViewById(R.id.button);
Log.d(TAG, "Set up button");
AssetManager assetManager = getAssets();
InputStream inputStream;
try {
inputStream = assetManager.open("dictionary.txt");
} catch (IOException e) {
inputStream = null;
}
Reader reader = new InputStreamReader(inputStream);
BufferedReader is = new BufferedReader(reader);
procDat = new processData("", is);
Log.d(TAG, "Initialized procDat");
}
public void onClick(View v)
{
Log.d(TAG, "Button clicked");
String output;
letters = text.getText().toString();
procDat.setLetters(letters);
try {
output = procDat.doProcessing();
} catch (IOException e) {
output = "Error while opening file";
}
mOutput.setText(output);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Simply rename the id with other id instead of button. You can't use the same id in two layout since content_main.xml is included in activity_main.xml.
<include
android:id="#+id/button"
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
the main problem is in your "AndroidManidfest.xml" file and it is corrected by changing the theme of your application or theme of your activity. here are 3 solutions for that...
Changing the theme of all activity by
"android:theme="#style/Theme.AppCompat"" or by
"android:theme="#style/Theme.MaterialComponents.Light.NoActionBar""
Changing the theme of your application by
"android:theme="#style/Theme.AppCompat"" or by
"android:theme="#style/Theme.MaterialComponents.Light.NoActionBar""
Or by changing your
"androidx.constraintlayout.widget.ConstraintLayout" by
"RelativeLayout" in your "content_main.xml" file
I'm sure that it works...
Related
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
I am very new when it comes to android app development and android studio.
I have been searching around for some couple days but still havent found the solution.
How can i have different toolbar Items on different activity's is their an way to do that ?
Do you have an example so i could take a look at that?
Do you know an tutorial that has that in it?
already much thanks for the help.
MainActivity.java
package com.example.stage.absa;
import android.Manifest;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class MainActivity extends AbsRuntimePermission {
private ZXingScannerView scannerView;
private static final int REQUEST_PERMISSION = 10;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
requestAppPermissions(new String[]{
Manifest.permission.CAMERA},
R.string.msg, REQUEST_PERMISSION);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (id == R.id.action_settings) {
setContentView(R.layout.activity_settings);
return true;
}
return super.onOptionsItemSelected(item);
}
public void browser1(View view) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.suppliance.nl/"));
startActivity(browserIntent);
}
public void scanCode(View view) {
scannerView = new ZXingScannerView(this);
scannerView.setResultHandler(new ZxingScannerResultHandler());
setContentView(scannerView);
scannerView.startCamera();
}
class ZxingScannerResultHandler implements ZXingScannerView.ResultHandler {
#Override
public void handleResult(Result result) {
String resultCode = result.getText();
Toast.makeText(MainActivity.this, resultCode, Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
scannerView.stopCamera();
}
}
#Override
public void onPermissionsGranted(int requestCode) {
//Do anything when permisson granted
Toast.makeText(getApplicationContext(), "Toegang geaccepteerd", Toast.LENGTH_LONG).show();
}
}
activity_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
<!-- Layout for content is here. This can be a RelativeLayout -->
<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="com.example.stage.absa.MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="browser1"
android:layout_margin="2dp"
android:src="#drawable/suppliance" />
<Button
android:id="#+id/S"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="scanCode"
android:layout_margin="5dp"
android:backgroundTint="#color/colorPrimary"
android:text="Scan" />
</RelativeLayout>
</LinearLayout>
SettingsActivity.java
package com.example.stage.absa;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
public class SettingsActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}
}
activity_settings.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
app:titleTextColor="#android:color/white"
android:layout_height="?attr/actionBarSize">
<ImageView
android:id="#+id/tv_header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginRight="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name"
android:textColor="#ffffff"
android:gravity="center"
android:id="#+id/brand_name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C Name :"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:textColor="#ffffff"
android:id="#+id/t1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name :"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:textColor="#ffffff"
android:id="#+id/t2" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<!-- Layout for content is here. This can be a RelativeLayout -->
<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="com.example.stage.absa.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Settings" />
</RelativeLayout>
</LinearLayout>
menu_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_back"
android:title="Back"
app:showAsAction="ifRoom" />
</menu>
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_settings"
android:title="Settings"
app:showAsAction="ifRoom" />
</menu>
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu1, menu);
return true;
}
Just pass different menu resources for the different activities ie R.id.menu1 or R.menu.menu2.
You can set custom toolbar layout for any activity like.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
app:titleTextColor="#android:color/white"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay">
<ImageView
android:id="#+id/tv_header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginRight="20dp"
android:src="#drawable/siyaram_logo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Name"
android:textColor="#ffffff"
android:gravity="center"
android:id="#+id/brand_name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C Name :"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:textColor="#ffffff"
android:id="#+id/t1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Name :"
android:layout_gravity="right"
android:layout_marginRight="20dp"
android:textColor="#ffffff"
android:id="#+id/t2" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
I am attempting to get Strings from 2 Edittexts within a Dialog Fragment, but my app keeps crashing when the activity starts. It is some sort of issue with being able to bind the edittexts from the dialog. I am not sure of the correct way to bind them.
Here is the crash:
09-24 11:34:30.366 16147-16147/com.epicodus.concertaid E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.epicodus.concertaid, PID: 16147
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.epicodus.concertaid/com.epicodus.concertaid.ui.UserProfileActivity}: java.lang.RuntimeException: Unable to bind views for com.epicodus.concertaid.ui.UserProfileActivity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Unable to bind views for com.epicodus.concertaid.ui.UserProfileActivity
at butterknife.ButterKnife.bind(ButterKnife.java:322)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at com.epicodus.concertaid.ui.UserProfileActivity.onCreate(UserProfileActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Required view 'userEmailEditText' with ID 2131558627 for field 'mUserEmailEditText' was not found. If this view is optional add '#Nullable' annotation.
at butterknife.ButterKnife$Finder.findRequiredView(ButterKnife.java:140)
at com.epicodus.concertaid.ui.UserProfileActivity$$ViewBinder.bind(UserProfileActivity$$ViewBinder.java:15)
at com.epicodus.concertaid.ui.UserProfileActivity$$ViewBinder.bind(UserProfileActivity$$ViewBinder.java:8)
at butterknife.ButterKnife.bind(ButterKnife.java:319)
at butterknife.ButterKnife.bind(ButterKnife.java:237)
at com.epicodus.concertaid.ui.UserProfileActivity.onCreate(UserProfileActivity.java:37)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
And here is the Activity:
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.epicodus.concertaid.Constants;
import com.epicodus.concertaid.R;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import butterknife.Bind;
import butterknife.ButterKnife;
public class UserProfileActivity extends AppCompatActivity implements View.OnClickListener {
#Bind(R.id.summaryTextView) TextView mSummaryTextView;
#Bind(R.id.deleteAccountButton) Button mDeleteAccountButton;
#Bind(R.id.userEmailEditText) EditText mUserEmailEditText;
#Bind(R.id.userPasswordEditText) EditText mUserPasswordEditText;
private Firebase mFirebaseRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
ButterKnife.bind(this);
mDeleteAccountButton.setOnClickListener(this);
//GET REFERENCE TO FIREBASE
mFirebaseRef = new Firebase(Constants.FIREBASE_URL);
//SETS FONT FOR TITLE
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MUSICNET.ttf");
mSummaryTextView.setTypeface(tf);
}
#Override
public void onClick(View view) {
if(view == mDeleteAccountButton) {
createAlertDialog();
}
}
public void deleteUser(String userEmail, String userPassword) {
Firebase.ResultHandler handler = new Firebase.ResultHandler() {
#Override
public void onSuccess() {
Toast.makeText(UserProfileActivity.this, "User deleted Successflly", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FirebaseError firebaseError) {
Toast.makeText(UserProfileActivity.this, "There was an error, please try again", Toast.LENGTH_LONG).show();
}
};
mFirebaseRef.removeUser(userEmail, userPassword, handler );
}
public void createAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(UserProfileActivity.this);
LayoutInflater inflater = UserProfileActivity.this.getLayoutInflater();
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
builder.setView(inflater.inflate(R.layout.delete_user_dialog, null));
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
String userEmail = mUserEmailEditText.getText().toString();
String userPassword = mUserPasswordEditText.getText().toString();
deleteUser(userEmail, userPassword);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
This is the layout for the activity
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.epicodus.concertaid.ui.UserProfileActivity"
android:id="#+id/relativeLayout">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageView"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:layout_centerHorizontal="true"
android:src="#drawable/background1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account Details"
android:textColor="#0288D1"
android:id="#+id/summaryTextView"
android:layout_alignParentTop="true"
android:textSize="30sp"
android:layout_centerInParent="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Photo"
android:textColor="#0288D1"
android:background="#drawable/buttonshape"
android:id="#+id/addPhotoButton"
android:layout_below="#+id/summaryTextView"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginTop="25dp" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/userPhotoImageView"
android:layout_marginTop="25dp"
android:layout_below="#+id/addPhotoButton"
android:layout_centerHorizontal="true"
android:contentDescription="#string/current_user_s_image"
android:src="#drawable/blank_user" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Email"
android:textColor="#0288D1"
android:id="#+id/changeEmailButton"
android:background="#drawable/buttonshape"
android:layout_below="#+id/userPhotoImageView"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginTop="25dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Password"
android:textSize="25sp"
android:textColor="#0288D1"
android:layout_marginTop="25dp"
android:background="#drawable/buttonshape"
android:id="#+id/changePasswordEmailButton"
android:layout_below="#+id/changeEmailButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Account"
android:textSize="25sp"
android:textColor="#0288D1"
android:layout_marginTop="25dp"
android:id="#+id/deleteAccountButton"
android:layout_below="#+id/changePasswordEmailButton"
android:layout_centerHorizontal="true"
android:background="#drawable/buttonshape" />
Here is the layout for the AlertDialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/userEmailEditText"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Enter Email" />
<EditText
android:id="#+id/userPasswordEditText"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="Enter Password"/>
</LinearLayout>
Any example to show how to correctly Bind the mUserEmailEditText and mUserPasswordEditText
Thank you
When you use bind like that is supposed to be from a existing ID in your activity layout, since you dont have it in your xml it will crash.. Dont bind the EditText and just create them in the AlertDialog, and im use setParams to change their layout
#Bind(R.id.userEmailEditText) EditText mUserEmailEditText;
#Bind(R.id.userPasswordEditText) EditText mUserPasswordEditText;
You can not bind the view from the xml which is not inflated as part of Activity's view. So remove #Bind from above line as follows.
EditText mUserEmailEditText;
EditText mUserPasswordEditText;
and inflate them while creating your dialog in createAlertDialog() method. So replace following 2 lines
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
builder.setView(inflater.inflate(R.layout.delete_user_dialog, null));
with
View rootView = inflater.inflate(R.layout.delete_user_dialog, null);
mUserEmailEditText = rootView.findViewById(R.id.userEmailEditText);
mUserPasswordEditText = rootView.findViewById(R.id.userPasswordEditText);
builder.setView(rootView);
So,
I am building a basic app which has 2 activivities. Now, the first one works well, the second one does not. It does not show any errors while I edit the codes, but when I run the app and when I am in the second acitivity it gives me an annoying error. Saying "Stops unexpectedly...." You know. I tried debugging but failed. In the second activity called Troll.java I should be able to check a checkbox and when I do that a text is supposed to change to something else. It says stops unexpe.....
main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:padding="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
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=".MainActivity" >
<include
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/resuable_pizza_layout" />
<TextView
android:id="#+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="105dp"
android:text="#string/display"
android:textColor="#FFFFFF"
android:textSize="18sp" />
<ImageView
android:id="#+id/lol"
android:onClick="onlol"
android:contentDescription="#string/lol2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/display"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:src="#drawable/lol" />
troll.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF69B4"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<CheckBox
android:id="#+id/box1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onChecked1"
android:text="#string/ch1"
android:textColor="#000000" />
<CheckBox
android:id="#+id/box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onChecked2"
android:text="#string/ch2"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:layout_weight="0.26" >
<TextView
android:id="#+id/textView1"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/trollFace"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
mainActitvity.java :
package com.example.pizza2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends Activity {
CheckBox pepp, cheese;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pepp = (CheckBox) findViewById(R.id.check1);
cheese = (CheckBox) findViewById(R.id.check2);
tv = (TextView) findViewById(R.id.display);
}
public void onShow(View view){
StringBuilder str = new StringBuilder();
if (pepp.isChecked() && cheese.isChecked()){
str.append("Pepperoni, extra cheese pizza!");
}
else if (pepp.isChecked()){
str.append("Pepperoni pizza!");
}
else if (cheese.isChecked()){
str.append("Extra cheese pizza!");
}
tv.setText(str);
}
public void onlol(View view){
Intent intent = new Intent(this, Troll.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Troll.java:
package com.example.pizza2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class Troll extends Activity{
CheckBox box11, box22;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.troll);
box11 = (CheckBox) findViewById(R.id.box1);
box22 = (CheckBox) findViewById(R.id.box2);
tv = (TextView) findViewById(R.id.display);
}
public void onChecked1(View view){
StringBuilder str = new StringBuilder();
if(box11.isChecked()){
str.append("Lol bro!");
}
tv.setText(str);
}//end of "onChecked1"
public void onChecked2(View view){
StringBuilder str = new StringBuilder();
if(box22.isChecked()){
str.append("XD XD Kaki");
}
tv.setText(str);
} //end of "onChecked2"
}
Please have a look at the following code
game_activity.xml
<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=".Game" >
//Please note the GUI Has been removed
<include layout="#layout/common_status_bar"/>
</RelativeLayout>
Game.java
package game.games;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
public class Game extends Activity {
private ImageView goToLanguageSelection;
private ImageView goToInternet;
private Button giveUp;
private LayoutInflater layoutInflater;
private View commonView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
layoutInflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
commonView = layoutInflater.inflate(R.layout.common_status_bar, null);
//Intializing instance variables
goToLanguageSelection = (ImageView)commonView.findViewById(R.id.backToLanguageSelectionButton);
goToInternet = (ImageView)commonView.findViewById(R.id.internetButton);
giveUp = (Button)commonView.findViewById(R.id.giveUpButton);
flag = getIntent().getBooleanExtra("LANGUAGE", true);
//Add listeners
goToLanguageSelection.setOnClickListener(goToLanguageSelectionClicked);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
//Will get activated when the ThunderBolt image is clicked
private OnClickListener goToLanguageSelectionClicked = new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(),LanguageSelector.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
};
}
common_status_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#373734"
android:orientation="horizontal" >
<ImageView
android:id="#+id/backToLanguageSelectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="15dp"
android:paddingTop="5dp"
android:src="#drawable/thunderbolt" />
<ImageView
android:id="#+id/internetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:src="#drawable/globe_small_2" />
<ImageView
android:id="#+id/justForFun"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingTop="5dp" />
<Button
android:id="#+id/giveUpButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Button" />
</LinearLayout>
Here, the common_status_bar.xml is a common layout. I have added this layout to game_activity.xml by <include layout="#layout/common_status_bar"/>.
It displays everything fine, no issue. But the case is I can't make any of it's elements to work. I have attached a OnClickListener to the first element of it, the goToLanguageSelection but it is not responding to the Click. I tested this by adding the click event to all other buttons, images as well (one at a time) and I got the same result. It is not responding to user click, or whatever.
Why this button and images in separate layout do not respond to the user events?
The commonView should not be created again.
If you want keep a reference of the common_status_bar.xml, you can give name it by an id, like the code below:
<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="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<include
android:id="#+id/common_status_bar"
layout="#layout/common_status_bar" />
</RelativeLayout>
Then, change this code:
commonView = layoutInflater.inflate(R.layout.common_status_bar, null);
to:
commonView = findViewById(R.id.common_status_bar);