Custom keyboard for android inside app - java

I make a programm for android and i have a problem. Also i used navigator drawer and fragments inside my app. I found a lot of information in the interner, but i did not find, what i wanted.
I want to make a custom keyboard inside my app.
I did this
<LinearLayout
android:visibility="gone"
android:id="#+id/keyBoard"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="bottom"
android:layout_gravity="bottom"
android:background="#color/colorPrimary"
android:orientation="vertical">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<Button
android:textAllCaps="false"
android:id="#+id/graphE"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_e"/>
<Button
android:textAllCaps="false"
android:id="#+id/graphPi"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_pi"/>
<Button
android:id="#+id/graphLeftBracket"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_bracket_left"/>
<Button
android:id="#+id/graphRightBracket"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_bracket_right"/>
<Button
android:id="#+id/graphLeftSquareBracket"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_bracket_square_left"/>
<Button
android:id="#+id/graphRightSquareBracket"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_bracket_square_right"/>
<Button
android:id="#+id/graphDegree"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_degree"/>
<Button
android:id="#+id/graphDelete"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:drawableBottom="#drawable/ic_clear"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<Button
android:textAllCaps="false"
android:id="#+id/graphAcos"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="2"
android:text="#string/key_acos"/>
<Button
android:textAllCaps="false"
android:id="#+id/graphCos"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_cos"/>
<Button
android:id="#+id/graphX"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/button_x"/>
<Button
android:id="#+id/graphOne"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_one"/>
<Button
android:id="#+id/graphTwo"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_two"/>
<Button
android:id="#+id/graphThree"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_three"/>
<Button
android:id="#+id/graphMultiply"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:drawableBottom="#drawable/ic_multiply"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<Button
android:textAllCaps="false"
android:id="#+id/graphAtan"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="2"
android:text="#string/key_atan"/>
<Button
android:textAllCaps="false"
android:id="#+id/graphTan"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_tan"/>
<Button
android:id="#+id/graphY"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/button_y"/>
<Button
android:id="#+id/graphFour"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_four"/>
<Button
android:id="#+id/graphFive"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_five"/>
<Button
android:id="#+id/graphSix"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_six"/>
<Button
android:id="#+id/graphSum"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:drawableBottom="#drawable/ic_add"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<Button
android:textAllCaps="false"
android:id="#+id/graphAsin"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="2"
android:text="#string/key_asin"/>
<Button
android:textAllCaps="false"
android:id="#+id/graphSin"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_sin"/>
<Button
android:id="#+id/graphModule"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_module"/>
<Button
android:id="#+id/graphSeven"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_seven"/>
<Button
android:id="#+id/graphEight"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_eight"/>
<Button
android:id="#+id/graphNine"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_nine"/>
<Button
android:id="#+id/graphMinus"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:drawableBottom="#drawable/ic_minus"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<Button
android:textAllCaps="false"
android:id="#+id/graphAcot"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="2"
android:text="#string/key_acot"/>
<Button
android:textAllCaps="false"
android:id="#+id/graphCot"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_cot"/>
<Button
android:textAllCaps="false"
android:id="#+id/graphLn"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_ln"/>
<Button
android:textAllCaps="false"
android:id="#+id/graphLog"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_log"/>
<Button
android:textAllCaps="false"
android:id="#+id/graphLg"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_lg"/>
<Button
android:id="#+id/graphZero"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/key_zero"/>
<Button
android:id="#+id/graphDivizion"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:text="#string/divizion"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp">
</LinearLayout>
</LinearLayout>
i make a math keyboard inside my programm.
Than i did editText, where will add a text from my keyboard
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<Button
android:theme="#style/Widget.AppCompat.Button.Colored"
android:layout_weight="1"
android:gravity="center"
android:id="#+id/buttonClick"
android:layout_width="10dp"
android:layout_height="45dp"
android:text="#string/Calc"/>
<EditText
android:layout_weight="1"
android:id="#+id/editGraphText"
android:layout_width="200dp"
android:layout_height="45dp"
android:inputType="textPersonName"
android:text=""/>
<Button
android:id="#+id/buttonClear"
android:layout_width="45dp"
android:layout_height="45dp"
android:gravity="center"
android:text="#string/Clear" />
</TableRow>
</TableLayout>
Then i need to hide my keyboard and open it again.
When i hide my keyboard and open it again, my code does't work.
I checked buttons. Button is work, but editText did't change.
Here code on java
keyBoard = (LinearLayout) view.findViewById(R.id.keyBoard);
editText = (EditText) view.findViewById(R.id.editGraphText);
editText.setInputType(InputType.TYPE_NULL);
buttonSin = (Button) view.findViewById(R.id.graphSin);
buttonCos = (Button) view.findViewById(R.id.graphCos);
buttonTan = (Button) view.findViewById(R.id.graphTan);
buttonCot = (Button) view.findViewById(R.id.graphCot);
buttonAsin = (Button) view.findViewById(R.id.graphAsin);
buttonAcos = (Button) view.findViewById(R.id.graphAcos);
buttonAcot = (Button) view.findViewById(R.id.graphAcot);
buttonAtan = (Button) view.findViewById(R.id.graphAtan);
buttonMultiply = (Button) view.findViewById(R.id.graphMultiply);
buttonSum = (Button) view.findViewById(R.id.graphSum);
buttonDegree = (Button) view.findViewById(R.id.graphDegree);
buttonMinus = (Button) view.findViewById(R.id.graphMinus);
buttonDivizion = (Button) view.findViewById(R.id.graphDivizion);
buttonLog = (Button) view.findViewById(R.id.graphLog);
buttonLg = (Button) view.findViewById(R.id.graphLg);
buttonPi = (Button) view.findViewById(R.id.graphPi);
buttonE = (Button) view.findViewById(R.id.graphE);
buttonLeftBracket = (Button) view.findViewById(R.id.graphLeftBracket);
buttonLeftSquareBracket = (Button) view.findViewById(R.id.graphLeftSquareBracket);
buttonRightSquareBracket = (Button) view.findViewById(R.id.graphRightSquareBracket);
buttonRightBracket = (Button) view.findViewById(R.id.graphRightBracket);
buttonOne = (Button) view.findViewById(R.id.graphOne);
buttonTwo = (Button) view.findViewById(R.id.graphTwo);
buttonThree = (Button) view.findViewById(R.id.graphThree);
buttonFour = (Button) view.findViewById(R.id.graphFour);
buttonFive = (Button) view.findViewById(R.id.graphFive);
buttonSix = (Button) view.findViewById(R.id.graphSix);
buttonSeven = (Button) view.findViewById(R.id.graphSeven);
buttonEight = (Button) view.findViewById(R.id.graphEight);
buttonNine = (Button) view.findViewById(R.id.graphNine);
buttonZero = (Button) view.findViewById(R.id.graphZero);
buttonModule = (Button) view.findViewById(R.id.graphModule);
buttonX = (Button) view.findViewById(R.id.graphX);
buttonY = (Button) view.findViewById(R.id.graphY);
buttonLn = (Button) view.findViewById(R.id.graphLn);
buttonClear = (Button) view.findViewById(R.id.graphDelete);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
keyBoard.setVisibility(View.VISIBLE);
}
});
buttonSin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finalText += "sin(";
editText.setText(finalText);
}
});
buttonCos.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finalText += "cos(";
editText.setText(finalText);
}
});
buttonTan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finalText += "tan(";
editText.setText(finalText);
}
});
buttonCot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finalText += "cot(";
editText.setText(finalText);
}
});
When i want to hide my keyboard i use this in my activity
#Override
public void onBackPressed() {
LinearLayout keyBoard = (LinearLayout) findViewById(R.id.keyBoard);
if (keyBoard.getVisibility() == View.INVISIBLE) {
Fragment fragment = null;
Class fragmentClass = null;
fragmentClass = GraphActivity.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} else {
keyBoard.setVisibility(View.INVISIBLE);
}
}
Other clickListener inside code, here is a part of code, i did't copy every clickListener.
How i can make to work my code?
I need to hide my keyboard and I want to open it again, but it did't work.
Where is my mistake?

Related

Button onClick() causing force stop

Hello StackOverFlowGeeks!
Currently, I'm learning how to write a basic Android app, that has few buttons.
I have four activities(Java Classes) and four xmls.
I have the RegisterScreen.java that has two buttons(Register and Back button). Currently, I am trying to get back from this RegisterScreen.java to SignInScreen.java. The problem is that when I click on the back button it causes a failure and the app is forced to shut down...
So I've uploaded this way:
1) RegisterScreen.java
2) SignInScreen.java
3) RegisterScreen XML file
4) SignInScreen XML file
Thanks in advance.
RegisterScreen:
public class RegisterScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_screen);
}
public void onClickRegisterButton(View view){
}
public void onClickBackButton(View view) {
Button backBtn = (Button) findViewById(R.id.backBtn);
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(RegisterScreen.this, SignInScreen.class);
RegisterScreen.this.startActivity(intent);
}
});
}
}
SignInScreen:
public class SignInScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_screen);
}
#SuppressLint("SetTextI18n")
public void onClickSignInButton(View view){
TextView username = (TextView)findViewById(R.id.username_text);
TextView password = (TextView)findViewById(R.id.password_text);
Button signIn = (Button)findViewById(R.id.sign_in_button);
if (username.getText().toString().equals("Ivan Simeonov") && password.getText().toString().equals("Ivan9603116245")){
signIn.setText("Welcome " + username.getText().toString());
signIn.setBackgroundColor(Color.YELLOW);
setContentView(R.layout.activity_logged_screen);
}else{
signIn.setText("The input data is incorrect! Try again!");
signIn.setBackgroundColor(Color.GREEN);
}
}
public void onClickRegisterButton(View view){
Button register = (Button)findViewById(R.id.register_button);
setContentView(R.layout.activity_register_screen);
}
public void onClickResetPasswordButton(View view){
Button resetPassword = (Button)findViewById(R.id.reset_password_button);
setContentView(R.layout.activity_reset_password_screen);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.megat0n.startproject.RegisterScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView_Register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/register_your_account_by_filling_up_the_following_data"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_username"
android:inputType="textPersonName" />
<EditText
android:id="#+id/first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_firstname"
android:inputType="textPersonName" />
<EditText
android:id="#+id/last_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_lastname"
android:inputType="textPersonName" />
<EditText
android:id="#+id/birth_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_birthdate"
android:inputType="date" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_email"
android:inputType="textPersonName" />
<EditText
android:id="#+id/email_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/confirm_email"
android:inputType="textPersonName" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_password"
android:inputType="textPassword" />
<EditText
android:id="#+id/password_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/confirm_password"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/reg_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/register"
android:onClick="onClickRegisterButton"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/backBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/back"
android:onClick="onClickBackButton"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.megat0n.startproject.SignInScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="#string/welcome_to_the_home_screen"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="#+id/username_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/username"
android:inputType="textPersonName"
android:textAlignment="center"
android:textStyle="bold" />
<EditText
android:id="#+id/password_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword"
android:textAlignment="center"
android:textStyle="bold" />
<Button
android:id="#+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickSignInButton"
android:text="#string/sign_in"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickRegisterButton"
android:text="#string/register"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/reset_password_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickResetPasswordButton"
android:text="#string/reset_password"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Use this onClickBackButton() instead of yours. If you use onClick attribute in your layout, you don't need creating button and click listener.
public void onClickBackButton(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
Try in RegisterScreen use onBackPressed() method:
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});

Showing null pointer exception even though no error is showing up [duplicate]

This question already has answers here:
What is a stack trace, and how can I use it to debug my application errors?
(7 answers)
Closed 4 years ago.
This is the code MainActivity.java file. I think there should be no mistake in this code. But it shows the message
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference.
I don't know why it is showing up there? Please help me to figure out.
package com.example.android.practiceset2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.*;
import android.view.*;`
public class MainActivity extends AppCompatActivity {
int score=0,wickets=0;
String value=null,number=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button b1=(Button)findViewById(R.id.b_i1);//b_i1 stands for increase in 1 run taken by team B and similarly other names are to the ids of different button.
Button b2=(Button)findViewById(R.id.b_i2);
Button b3=(Button)findViewById(R.id.b_i3);
Button b4=(Button)findViewById(R.id.b_i4);
Button b6=(Button)findViewById(R.id.b_i6);
Button bw=(Button)findViewById(R.id.b_w);
Button bwide=(Button)findViewById(R.id.b_wide);
Button a1=(Button)findViewById(R.id.a_i1);
Button a2=(Button)findViewById(R.id.a_i2);
Button a3=(Button)findViewById(R.id.a_i3);
Button a4=(Button)findViewById(R.id.a_i4);
Button a6=(Button)findViewById(R.id.a_i6);
Button aw=(Button)findViewById(R.id.a_w);
Button awide=(Button)findViewById(R.id.a_wide);
TextView ta= (TextView)findViewById(R.id.a);
TextView tb= (TextView)findViewById(R.id.b);
public void aorb(View view{
if(getResources().getResourceEntryName((view.getId())).startsWith("a"))
{ value="a";}
else{value="b";}
number=getResources().getResourceEntryName((view.getId())).substring(2);
score();}
public void a_enabled(Boolean b)
{
a1.setEnabled(b);
a2.setEnabled(b);
a3.setEnabled(b);
a4.setEnabled(b);
a6.setEnabled(b);
aw.setEnabled(b);
awide.setEnabled(b);
}
public void b_enabled(Boolean b)
{
b1.setEnabled(b);
b2.setEnabled(b);
b3.setEnabled(b);
b4.setEnabled(b);
b6.setEnabled(b);
bw.setEnabled(b);
bwide.setEnabled(b);
}
public void display(String s){
if(value.equals("a"))
{
ta.setText(s);
b_enabled(false);
a_enabled(true);
}
else {
tb.setText(s);
a_enabled(false);
b_enabled(true);
}
}
public void score(){
int i=0;
if(number.equals("i1"))
i=1;
if(number.equals("i2"))
i=2;
if(number.equals("i3"))
i=3;
if(number.equals("i4"))
i=4;
if(number.equals("i6"))
i=6;
if(number.equals("wide"))
i=1;
score+=i;
if(number.equals("w"))
wickets+=1;
if(wickets==10)//if a team loses all its 10 wickets , the score is finalised and next team is invited for play
{
display(score+"/"+wickets);
if(value.equals("a"))
{
value="b";
}
else{value="a";}
score=0;
wickets=0;
display(score+"/"+wickets);
}
display(score+"/"+wickets);
}
public void reset(View view){
ta.setText("0/0");
tb.setText("0/0");
a_enabled(true);
b_enabled(true);
}}
This is the code for my content_main.xml file.
<?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"
android:orientation="horizontal"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team A"
android:textAlignment="center"
android:fontFamily="sans-serif-medium"
android:textSize="20sp"
android:textColor="#616161"
android:layout_margin="16dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="0/0"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:textSize="56dp"
android:textColor="#000000"
android:id="#+id/a"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="aorb"
android:id="#+id/a_i1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:onClick="aorb"
android:id="#+id/a_i3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:onClick="aorb"
android:id="#+id/a_i6"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:onClick="aorb"
android:id="#+id/a_i2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:onClick="aorb"
android:id="#+id/a_i4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Out"
android:onClick="aorb"
android:id="#+id/a_w"
/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="Wide"
android:onClick="aorb"
android:id="#+id/a_wide"/>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team B"
android:textAlignment="center"
android:fontFamily="sans-serif-medium"
android:textSize="20sp"
android:textColor="#616161"
android:layout_margin="16dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="0/0"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:textSize="56dp"
android:textColor="#000000"
android:id="#+id/b"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="aorb"
android:id="#+id/b_i1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:onClick="aorb"
android:id="#+id/b_i3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:onClick="aorb"
android:id="#+id/b_i6"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:onClick="aorb"
android:id="#+id/b_i2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:onClick="aorb"
android:id="#+id/b_i4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Out"
android:onClick="aorb"
android:id="#+id/b_w"
/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="Wide"
android:onClick="aorb"
android:id="#+id/b_wide"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:onClick="reset"
android:layout_marginBottom="32dp"
/>
</RelativeLayout>
Here is the image of it should look like it is the screenshot the preview
All these findViewById must be included in your onCreate() method. You can keep the declarations out of onCreate(). Your views do not exist before setContentView(R.layout.activity_main);
I mean before onCreate() you can have Button b1;
and after setContentView(R.layout.activity_main); in onCreate() you must have b1=(Button)findViewById(R.id.b_i1);
for every view.
I have made the corrections in your and now the app is not crashing. Try using the below code snippet
MainActivity
public class MainActivity extends AppCompatActivity {
int score = 0, wickets = 0;
String value = null, number = null;
Button b1;
Button b2;
Button b3;
Button b4;
Button b6;
Button bw;
Button bwide;
Button a1;
Button a2;
Button a3;
Button a4;
Button a6;
Button aw;
Button awide;
TextView ta;
TextView tb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
b1 = (Button) findViewById(R.id.b_i1);//b_i1 stands for increase in 1 run taken by team B and similarly other names are to the ids of different button.
b2 = (Button) findViewById(R.id.b_i2);
b3 = (Button) findViewById(R.id.b_i3);
b4 = (Button) findViewById(R.id.b_i4);
b6 = (Button) findViewById(R.id.b_i6);
bw = (Button) findViewById(R.id.b_w);
bwide = (Button) findViewById(R.id.b_wide);
a1 = (Button) findViewById(R.id.a_i1);
a2 = (Button) findViewById(R.id.a_i2);
a3 = (Button) findViewById(R.id.a_i3);
a4 = (Button) findViewById(R.id.a_i4);
a6 = (Button) findViewById(R.id.a_i6);
aw = (Button) findViewById(R.id.a_w);
awide = (Button) findViewById(R.id.a_wide);
ta = (TextView) findViewById(R.id.a);
tb = (TextView) findViewById(R.id.b);
}
public void aorb(View view) {
if (getResources().getResourceEntryName((view.getId())).startsWith("a")) {
value = "a";
} else {
value = "b";
}
number = getResources().getResourceEntryName((view.getId())).substring(2);
score();
}
public void a_enabled(Boolean b) {
a1.setEnabled(b);
a2.setEnabled(b);
a3.setEnabled(b);
a4.setEnabled(b);
a6.setEnabled(b);
aw.setEnabled(b);
awide.setEnabled(b);
}
public void b_enabled(Boolean b) {
b1.setEnabled(b);
b2.setEnabled(b);
b3.setEnabled(b);
b4.setEnabled(b);
b6.setEnabled(b);
bw.setEnabled(b);
bwide.setEnabled(b);
}
public void display(String s) {
if (value.equals("a")) {
ta.setText(s);
b_enabled(false);
a_enabled(true);
} else {
tb.setText(s);
a_enabled(false);
b_enabled(true);
}
}
public void score() {
int i = 0;
if (number.equals("i1"))
i = 1;
if (number.equals("i2"))
i = 2;
if (number.equals("i3"))
i = 3;
if (number.equals("i4"))
i = 4;
if (number.equals("i6"))
i = 6;
if (number.equals("wide"))
i = 1;
score += i;
if (number.equals("w"))
wickets += 1;
if (wickets == 10)//if a team loses all its 10 wickets , the score is finalised and next team is invited for play
{
display(score + "/" + wickets);
if (value.equals("a")) {
value = "b";
} else {
value = "a";
}
score = 0;
wickets = 0;
display(score + "/" + wickets);
}
display(score + "/" + wickets);
}
public void reset(View view) {
ta.setText("0/0");
tb.setText("0/0");
a_enabled(true);
b_enabled(true);
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="sans-serif-medium"
android:text="Team A"
android:textAlignment="center"
android:textColor="#616161"
android:textSize="20sp" />
<TextView
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:fontFamily="sans-serif-light"
android:text="0/0"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="56dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/a_i1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="1" />
<Button
android:id="#+id/a_i3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="3" />
<Button
android:id="#+id/a_i6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/a_i2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="2" />
<Button
android:id="#+id/a_i4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="4" />
<Button
android:id="#+id/a_w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="Out" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/a_wide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="aorb"
android:text="Wide"
android:textAlignment="center" />
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="sans-serif-medium"
android:text="Team B"
android:textAlignment="center"
android:textColor="#616161"
android:textSize="20sp" />
<TextView
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:fontFamily="sans-serif-light"
android:text="0/0"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="56dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/b_i1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="1" />
<Button
android:id="#+id/b_i3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="3" />
<Button
android:id="#+id/b_i6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/b_i2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="2" />
<Button
android:id="#+id/b_i4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="4" />
<Button
android:id="#+id/b_w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="Out" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/b_wide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="aorb"
android:text="Wide"
android:textAlignment="center" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:onClick="reset"
android:text="Reset"
android:textAlignment="center" />
</RelativeLayout>
</RelativeLayout>
Hope this helps.

Several layouts in one xml layout - doesn't append text after button clicked

I was searching solution for my problem, but I didn't find any.
I'm creating android application and one activity layout has several activities:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".EkranGlowny"
android:id="#+id/aaa"
android:background="#ffff8000"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="130dp"
android:layout_height="540dp"
android:background="#ffff8000"
android:layout_gravity="right"
android:baselineAligned="false"
android:clickable="false"
android:focusable="false"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Krystian Pruchnik"
android:id="#+id/waiter_name"
android:textSize="20sp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID: 180881"
android:id="#+id/waiter_id"
android:textSize="20sp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zamówienie"
android:textSize="20sp"
android:id="#+id/order"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="130dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/viewOrder"
android:layout_marginRight="5dp"
android:background="#ffffffff" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="58dp"
android:layout_height="540dp"
android:background="#69ff8300"
android:weightSum="1"
android:baselineAligned="false">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="1"
android:id="#+id/button1" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="2"
android:id="#+id/button2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="3"
android:id="#+id/button3" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="4"
android:id="#+id/button4" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="5"
android:id="#+id/button5" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="6"
android:id="#+id/button6" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="7"
android:id="#+id/button7" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="8"
android:id="#+id/button8" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="9"
android:id="#+id/button9" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="0"
android:id="#+id/button0" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="#string/clear_button"
android:id="#+id/clear_button"
android:layout_gravity="top"
android:background="#ffff0000"
android:textColor="#ffffffff"
android:textSize="11sp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="540dp"
android:background="#69ff8300"
android:weightSum="1"
android:baselineAligned="false">
<ScrollView
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="480dp"
android:background="#ffff8000">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Zestawy"
android:id="#+id/zestawy"
android:textAlignment="center"
android:textIsSelectable="false"
android:layout_span="3"/>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa1"
android:id="#+id/meal1"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa2"
android:id="#+id/meal2"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa3"
android:id="#+id/meal3"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa4"
android:id="#+id/meal4"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa5"
android:id="#+id/meal5"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa6"
android:id="#+id/meal6"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa7"
android:id="#+id/meal7"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa8"
android:id="#+id/meal8"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa9"
android:id="#+id/meal9"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa10"
android:id="#+id/meal10"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa11"
android:id="#+id/meal11"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa12"
android:id="#+id/meal12"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa13"
android:id="#+id/meal13"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa14"
android:id="#+id/meal14"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#69ff8300"
android:weightSum="1"
android:baselineAligned="false">
<Button
android:layout_width="48dp"
android:layout_height="34dp"
android:text="VOID"
android:id="#+id/anuluj"
android:background="#ffff2800" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rozlicz"
android:id="#+id/rozlicz"
android:layout_weight="1.18" />
</LinearLayout>
</LinearLayout>
I want that if I click some button (from second and third linear layout) it'll put text in TextView (id: viewOrder in first linear layout). Whenever I try this, it's showing message: "Unfortunately WaiterApp is stopped" and application crashes.
My Main Activity in java:
public class EkranGlowny extends ActionBarActivity implements OnClickListener{
Button summary;
Context context;
Button but0;
Button but1;
Button but2;
Button but3;
Button but4;
Button but5;
Button but6;
Button but7;
Button but8;
Button but9;
Button butClr;
Button voiD;
Button meal1;
Button meal2;
Button meal3;
Button meal4;
Button meal5;
Button meal6;
Button meal7;
Button meal8;
Button meal9;
Button meal10;
Button meal11;
Button meal12;
Button meal13;
Button meal14;
TextView viewOrder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ekran_glowny);
setupVariables();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rozlicz:
context = getApplicationContext();
Intent intent = new Intent(context, ResumeScreen.class);
startActivity(intent);
break;
/*case R.id.button0:
viewOrder.append(but0.getText().toString());
break;
case R.id.button1:
viewOrder.append("1");
break;
case R.id.button2:
viewOrder.append("2");
break;*/
default:
viewOrder.append(((Button) v).getText().toString());
break;
}
}
#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, 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);
}
//implementacja zmiennych
private void setupVariables() {
summary = (Button) findViewById(R.id.rozlicz);
summary.setOnClickListener(this);
but0 = (Button) findViewById(R.id.button0);
but0.setOnClickListener(this);
but1 = (Button) findViewById(R.id.button1);
but1.setOnClickListener(this);
but2 = (Button) findViewById(R.id.button2);
but2.setOnClickListener(this);
but3 = (Button) findViewById(R.id.button3);
but3.setOnClickListener(this);
but4 = (Button) findViewById(R.id.button4);
but4.setOnClickListener(this);
but5 = (Button) findViewById(R.id.button5);
but5.setOnClickListener(this);
but6 = (Button) findViewById(R.id.button6);
but6.setOnClickListener(this);
but7 = (Button) findViewById(R.id.button7);
but7.setOnClickListener(this);
but8 = (Button) findViewById(R.id.button8);
but8.setOnClickListener(this);
but9 = (Button) findViewById(R.id.button9);
but9.setOnClickListener(this);
butClr = (Button) findViewById(R.id.clear_button);
butClr.setOnClickListener(this);
meal1 = (Button) findViewById(R.id.meal1);
meal1.setOnClickListener(this);
meal2 = (Button) findViewById(R.id.meal2);
meal2.setOnClickListener(this);
meal3 = (Button) findViewById(R.id.meal3);
meal3.setOnClickListener(this);
meal4 = (Button) findViewById(R.id.meal4);
meal4.setOnClickListener(this);
meal5 = (Button) findViewById(R.id.meal5);
meal5.setOnClickListener(this);
meal6 = (Button) findViewById(R.id.meal6);
meal6.setOnClickListener(this);
meal7 = (Button) findViewById(R.id.meal7);
meal7.setOnClickListener(this);
meal8 = (Button) findViewById(R.id.meal8);
meal8.setOnClickListener(this);
meal9 = (Button) findViewById(R.id.meal9);
meal9.setOnClickListener(this);
meal10 = (Button) findViewById(R.id.meal10);
meal10.setOnClickListener(this);
meal11 = (Button) findViewById(R.id.meal11);
meal11.setOnClickListener(this);
meal12 = (Button) findViewById(R.id.meal12);
meal12.setOnClickListener(this);
meal13 = (Button) findViewById(R.id.meal13);
meal13.setOnClickListener(this);
meal14 = (Button) findViewById(R.id.meal14);
meal14.setOnClickListener(this);
voiD = (Button) findViewById(R.id.anuluj);
voiD.setOnClickListener(this);
viewOrder = (TextView) findViewById(R.id.viewOrder);
}
}
I tried viewOrder.setText and viewOrder.append and nothing is working.
Is the problem is that TextView and buttons are in different layouts?
Could you tell me what I'm doing wrong?
edit:
In logcat I get:
06-07 14:17:40.290 7755-7755/com.example.krystian.waiterapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40da51f8)
06-07 14:17:40.290 7755-7755/com.example.krystian.waiterapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.krystian.waiterapp.EkranGlowny.onClick(EkranGlowny.java:78)
at android.view.View.performClick(View.java:3524)
at android.view.View$PerformClick.run(View.java:14194)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
at dalvik.system.NativeStart.main(Native Method)
Check your xml file name. It should be ekran_glowny.
My guess is you showed xml code from different xml file.

transform xml to java in android (dynamic UI)

hi to everyone i have this code in xml and i want transform this to java
<TableRow>
<ImageButton android:id="#+id/btnImg1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="75dp"
android:src="#drawable/one"/>
<ImageButton android:id="#+id/btnImg2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:src="#drawable/two"/>
<ImageButton android:id="#+id/btnImg13"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:src="#drawable/three"/>
</TableRow>
<TableRow>
<ImageButton android:id="#+id/btnImg4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:src="#drawable/four"/>
<ImageButton android:id="#+id/btnImg5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/five"/>
<ImageButton android:id="#+id/btnImg6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/six"/>
</TableRow>
<TableRow>
<ImageButton android:id="#+id/btnImg7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:src="#drawable/seven"
android:onClick="onClick"/>
<ImageButton android:id="#+id/btnImg8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/eight"
android:onClick="onClick2"/>
<ImageButton android:id="#+id/btnImg9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/nine"
android:onClick="onClick3"/>
</TableRow>
<TableRow>
<ImageButton android:id="#+id/btnImg10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="50dp"
android:src="#drawable/seven"/>
<ImageButton android:id="#+id/btnImg11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:src="#drawable/eight"/>
<ImageButton android:id="#+id/btnImg12"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:src="#drawable/nine"/>
</TableRow>
i write this in java but when i run all the imagebutton are duplicate on together:
public class GameActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LayoutParams params =
new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TableRow table = new TableRow(this);
table.setOrientation(TableRow.VERTICAL);
ImageButton ib = new ImageButton(this);
ib.setImageResource(R.drawable.one);
ib.setLayoutParams(params);
ib.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
Toast.makeText(getBaseContext(),"button clicked",Toast.LENGTH_SHORT).show();
}
});
ImageButton ib2 = new ImageButton(this);
ib.setImageResource(R.drawable.two);
ib.setLayoutParams(params);
ib.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
Toast.makeText(getBaseContext(),"button clicked",Toast.LENGTH_SHORT).show();
}
});
table.addView(ib);
table.addView(ib2);
TableRow.LayoutParams layoutParams=
new TableRow.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT );
this.addContentView(table, layoutParams);
}
this is just for 2 imagebutton but this hapend too...thank you

Returning values from a custom dialog view

I'm trying to make a custom dialog for my android application. I get null pointer exceptions when I attempt to access the fields from the 'ok' button method. It seems like my fields fade from existence as soon as the onclick listener fires. Is there a better way to do this?
addchars.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/scrollBox">
<RelativeLayout
android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/addChars">
<AutoCompleteTextView android:layout_height="wrap_content" android:id="#+id/inCharName" android:hint="#string/txtCharacterNameBox" android:layout_below="#+id/textView1" android:layout_centerHorizontal="true" android:layout_width="fill_parent">
<requestFocus></requestFocus>
</AutoCompleteTextView>
<TextView android:id="#+id/textView2" android:layout_height="wrap_content" android:gravity="center" android:layout_width="wrap_content" android:text="#string/txtHP" android:layout_below="#+id/inCharName" android:layout_alignParentLeft="true" android:layout_marginLeft="22dp"></TextView>
<TextView android:id="#+id/textView3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/txtTempHP" android:layout_below="#+id/inCharName" android:layout_alignParentRight="true" android:layout_marginRight="57dp"></TextView>
<EditText android:id="#+id/inHP" android:digits="-0123456789" android:minEms="3" android:gravity="center" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="3" android:layout_width="wrap_content" android:layout_below="#+id/textView2" android:layout_alignLeft="#+id/textView2">
</EditText>
<EditText android:id="#+id/inTempHP" android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="3" android:layout_width="wrap_content" android:layout_alignBaseline="#+id/inHP" android:layout_alignBottom="#+id/inHP" android:layout_alignLeft="#+id/textView3"></EditText>
<TextView android:id="#+id/TextView01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inHP" android:layout_alignLeft="#+id/inHP" android:text="#string/txtInitRoll"></TextView>
<TextView android:id="#+id/TextView02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inTempHP" android:layout_alignLeft="#+id/inTempHP" android:text="#string/txtInitBonus"></TextView>
<EditText android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:gravity="center" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView01" android:layout_alignLeft="#+id/TextView01" android:id="#+id/inInitRoll"></EditText>
<EditText android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:gravity="center" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView02" android:layout_alignLeft="#+id/TextView02" android:id="#+id/inInitBonus"></EditText>
<TextView android:id="#+id/TextView03" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/txtAC" android:layout_below="#+id/inInitRoll" android:layout_alignRight="#+id/textView2"></TextView>
<EditText android:id="#+id/inAC" android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView03" android:layout_alignLeft="#+id/TextView03"></EditText>
<TextView android:id="#+id/TextView04" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="#+id/inAC" android:layout_alignLeft="#+id/inInitBonus" android:text="#string/txtFortitude"></TextView>
<EditText android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView04" android:layout_alignLeft="#+id/TextView04" android:id="#+id/inFort"></EditText>
<TextView android:id="#+id/TextView06" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inFort" android:layout_alignLeft="#+id/inFort" android:text="#string/txtWill"></TextView>
<TextView android:id="#+id/TextView05" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inAC" android:layout_alignLeft="#+id/inAC" android:text="#string/txtRef"></TextView>
<EditText android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView05" android:layout_alignLeft="#+id/inInitRoll" android:id="#+id/inReflex"></EditText>
<EditText android:id="#+id/inWill" android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_above="#+id/chkRoll" android:layout_alignLeft="#+id/TextView06"></EditText>
<CheckBox android:text="#string/txtRollBox" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/chkRoll" android:layout_below="#+id/inReflex" android:layout_alignLeft="#+id/TextView05" android:layout_alignParentRight="true"></CheckBox>
</RelativeLayout>
</ScrollView>
And this is my alertdialog:
addBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//this block is to prep the layout for the alert dialog.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = inflater.inflate(R.layout.addchars, (ViewGroup) findViewById(R.id.scrollBox));
//taking this part out to try a better way.
new AlertDialog.Builder(initiative.this)
.setTitle("Add a Character")
.setView(addView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
//ui controls
AutoCompleteTextView characterName = (AutoCompleteTextView) findViewById(R.id.inCharName);
EditText hp = (EditText) findViewById(R.id.inHP);
EditText tempHp = (EditText) findViewById(R.id.inTempHP);
EditText initRoll = (EditText) findViewById(R.id.inInitRoll);
EditText initBonus = (EditText) findViewById(R.id.inInitBonus);
EditText ac = (EditText) findViewById(R.id.inAC);
EditText fort = (EditText) findViewById(R.id.inFort);
EditText reflex = (EditText) findViewById(R.id.inReflex);
EditText will = (EditText) findViewById(R.id.inWill);
CheckBox rollInit = (CheckBox) findViewById(R.id.chkRoll);
//variables for various checks
entriesOk=false;
needToRoll=false;
if(characterName.getText().length() > 0)
{
holdCharacterName = characterName.getText().toString();
if(hp.getText().length() > 0)
{
holdHp = hp.getText().toString();
if(initBonus.getText().length() > 0)
{
holdInitBonus = initBonus.getText().toString();
if(ac.getText().length() > 0)
{
holdAc = ac.getText().toString();
if(fort.getText().length() > 0)
{
holdFort = fort.getText().toString();
if(reflex.getText().length() > 0)
{
holdReflex = reflex.getText().toString();
if(will.getText().length() > 0)
{
holdWill = will.getText().toString();
if(tempHp.getText().length() == 0)
{
holdTempHp = "0";
tempHp.setText("0");
}
else
{
holdTempHp = tempHp.getText().toString();
}
if(rollInit.isChecked())
{
entriesOk = true;
needToRoll = true;
}
else
{
if(initRoll.getText().length() > 0)
{
holdInitRoll = initRoll.getText().toString();
entriesOk = true;
}
}
}
}
}
}
}
}
}
btnLogic();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
//do nothing
}
}).show();
addView.findViewById(R.id.inHP) instead of findViewById(R.id.inHP)
The problem is that you are calling addView.findViewById inside your onClick event, the onclick doesn't really happen until somebody clicks on it, I'll suggest that you make addView class member variable so that it can persist in memory. That'll solve your problem.

Categories

Resources