I have created my first android application I have 4 pages, I have set one page to be the menu and it has 3 buttons on it to switch between the pages. However I seem to be getting an error in my code, if someone could take a look and see where I am going wrong with this.
So this is my menu page (activity_main)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ifmdb.MainActivity"
tools:ignore="MergeRootFrame,HardcodedText" >
<TextView
android:id="#+id/txtMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:text="#string/please_click_on_the_options_available"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/btnDOCare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtDOCare"
android:layout_below="#+id/txtDOCare"
android:layout_marginTop="30dp"
android:layout_gravity="center_vertical"
android:onClick="openDOCare"
android:text="#string/duty_of_care" />
<TextView
android:id="#+id/txtDOCare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnDock"
android:layout_marginLeft="202dp"
android:layout_marginTop="44dp"
android:layout_gravity="center"
android:text="#string/click_for_duty_of_care"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtVeCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtDOCare"
android:layout_below="#+id/txtMain"
android:layout_marginTop="36dp"
android:text="#string/click_for_vehicle_checklist"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/btnCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/btnDOCare"
android:layout_below="#+id/txtVeCheck"
android:layout_marginTop="37dp"
android:onClick="openChecklist"
android:text="#string/checklist" />
<TextView
android:id="#+id/txtDock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtDOCare"
android:layout_below="#+id/btnCheck"
android:layout_marginTop="36dp"
android:text="#string/click_for_docket"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/btnDock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/btnCheck"
android:layout_below="#+id/txtDock"
android:layout_marginTop="38dp"
android:onClick="openDocket"
android:text="#string/docket" />
</RelativeLayout>
and this is my Java class for it (Main Activity)
package com.example.ifmdb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHome = (Button)findViewById(R.id.btnHome);
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),
startActivityForResult(myIntent, 0);
}
});
}
}
This is my second page (checklist)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="com.example.ifmdb.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/vehicle_safety_checklist"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/please_check_each_box"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.58" >
<TextView
android:id="#+id/txtInt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="#string/interior"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtEx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/txtInt"
android:layout_alignBottom="#+id/txtInt"
android:layout_alignParentRight="true"
android:layout_marginRight="113dp"
android:text="#string/exterior"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/cboxFLev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtInt"
android:text="#string/fuel_level" />
<CheckBox
android:id="#+id/cboxTires"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxFLev"
android:layout_alignBottom="#+id/cboxFLev"
android:layout_alignLeft="#+id/txtEx"
android:text="#string/tires_wheels" />
<CheckBox
android:id="#+id/cboxWWash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxFLev"
android:text="#string/windscreen_washer" />
<CheckBox
android:id="#+id/cboxExhaust"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxWWash"
android:layout_alignBottom="#+id/cboxWWash"
android:layout_alignLeft="#+id/cboxTires"
android:text="#string/exhaust" />
<CheckBox
android:id="#+id/cboxSWheel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxWWash"
android:text="#string/steering_wheel" />
<CheckBox
android:id="#+id/cboxLights"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxSWheel"
android:layout_alignBottom="#+id/cboxSWheel"
android:layout_alignLeft="#+id/cboxExhaust"
android:text="#string/lights_reflectors" />
<CheckBox
android:id="#+id/cboxBrakes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxSWheel"
android:text="#string/brakes" />
<CheckBox
android:id="#+id/cboxELeaks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxBrakes"
android:layout_alignBottom="#+id/cboxBrakes"
android:layout_alignLeft="#string/_id_cboxlights"
android:text="#string/exterior_leaks" />
<CheckBox
android:id="#+id/cboxClutch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxBrakes"
android:text="#string/clutch" />
<CheckBox
android:id="#+id/cbocBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxClutch"
android:layout_alignBottom="#+id/cboxClutch"
android:layout_alignLeft="#string/_id_cboxeleaks"
android:text="#string/body" />
<CheckBox
android:id="#+id/cboxHorn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxClutch"
android:text="#string/horn" />
<TextView
android:id="#+id/txtEng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cbocBody"
android:layout_below="#+id/cboxHorn"
android:text="#string/engine"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/cboxHeater"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/txtEng"
android:text="#string/heater" />
<CheckBox
android:id="#+id/cboxOil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/cboxExhaust"
android:layout_below="#+id/cboxHeater"
android:text="#string/oil_level" />
<CheckBox
android:id="#+id/cboxSBelt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxOil"
android:layout_alignBottom="#+id/cboxOil"
android:layout_alignParentLeft="true"
android:text="#string/seat_belts" />
<CheckBox
android:id="#+id/cboxWLight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxCool"
android:layout_alignBottom="#+id/cboxCool"
android:layout_alignParentLeft="true"
android:text="#string/warning_lights" />
<CheckBox
android:id="#+id/cboxBelt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cboxCool"
android:layout_below="#+id/cboxCool"
android:text="#string/belts" />
<CheckBox
android:id="#+id/cboxMirrors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxWLight"
android:text="#string/mirrors" />
<CheckBox
android:id="#+id/cboxLeaks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cboxBelt"
android:layout_below="#+id/cboxBelt"
android:text="#string/engine_leaks" />
<CheckBox
android:id="#+id/cboxCool"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cboxOil"
android:layout_below="#+id/cboxOil"
android:text="#string/coolant_level" />
<TextView
android:id="#+id/txtOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cboxLeaks"
android:layout_alignParentLeft="true"
android:text="#string/other"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="#+id/cboxScrew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtEng"
android:layout_below="#+id/cboxLeaks"
android:text="#string/loose_bolts_screws" />
<CheckBox
android:id="#+id/cboxWTri"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cboxScrew"
android:layout_alignBottom="#+id/cboxScrew"
android:layout_alignParentLeft="true"
android:text="#string/warning_triangle" />
<CheckBox
android:id="#+id/cboxFire"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cboxWTri"
android:text="#string/fire_extinguisher_first_aid_kit" />
<TextView
android:id="#+id/txtAddCom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/additional_comments"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtAddComBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/cboxLights"
android:layout_below="#+id/txtAddCom"
android:layout_marginTop="29dp" />
<TextView
android:id="#+id/txtFault"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="27dp"
android:text="#string/in_the_event_of_serious_faults_do_not_drive_the_vehicle"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtDefects"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtAddComBox"
android:layout_marginTop="110dp"
android:text="#string/defects"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/txtDefectsBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/cboxScrew"
android:layout_below="#+id/txtDefects"
android:layout_marginTop="25dp" />
<Button
android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/cboxOil"
android:text="#string/home" />
</RelativeLayout>
</LinearLayout>
and this is that java class for that page (Checklist)
package com.example.ifmdb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Checklist extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checklist);
Button btnCheck = (Button) findViewById(R.id.btnCheck);
btnCheck.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
I have an error with a line of code
Intent myIntent = new Intent(view.getContext(),
startActivityForResult(myIntent, 0);
This is the error---
The constructor Intent(Context, void) is undefined
Syntax error, insert ")" to complete Variable Initializer
Try this code
public class MainActivity extends Activity {
private Activity mActivity = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActivity = this;
Button btnHome = (Button)findViewById(R.id.btnHome);
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(mActivity.getApplicationContext(),Checklist.class);
mActivity.startActivityForResult(myIntent, 0);
}
});
}
}
In the code its missing the syntax.
here is a tutorial
You can try this way. Hope it helps.
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(getActivity(),Checklist.class);
startActivity(myIntent);
}
});
I got it fixed, you were right Atul O Holic i was missing a part of the code.
heres the working solution
package com.example.ifmdb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnHome = (Button)findViewById(R.id.btnHome);
btnHome.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(),Checklist.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Related
My project won't open on my device, it say's "Unfortunately,myApp has stopped".
this is my manifest:
this is my activity java :
package bismillah.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Project2Activity extends Activity {
public static int a,b,c;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//coding start here!!
//input data
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
//button proses
Button button1=(Button)findViewById(R.id.Button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Spinner bibit = (Spinner)findViewById(R.id.spinner1);
TextView biaya = (TextView)findViewById(R.id.textView6);
if(bibit.getSelectedItem().toString().equals("Cabai Rp.100")){
a=pekerja*(lama*upah)+(jumlah*100);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Tomat Rp.150")){
a=pekerja*(lama*upah)+(jumlah*150);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Timun Rp.200")){
a=pekerja*(lama*upah)+(jumlah*200);
biaya.setText("Biaya Rp." + a);
}
}
}
}
});
//button reset
Button button2=(Button)findViewById(R.id.Button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView biaya = (TextView)findViewById(R.id.textView6);
EditText pek=(EditText)findViewById(R.id.editText1);
EditText lam=(EditText)findViewById(R.id.editText2);
EditText up=(EditText)findViewById(R.id.editText3);
EditText jum=(EditText)findViewById(R.id.editText4);
pek.setText("");
lam.setText("");
up.setText("");
jum.setText("");
biaya.setText("");
}
});
//button pindah activity
Button button3=(Button)findViewById(R.id.Button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i =new Intent(getApplicationContext(),activity2.class);
startActivity(i);
}
});
}
}
and this is my main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/bg"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="#string/hello"
android:textColor="#color/warna"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:entries="#array/list" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textColor="#color/warna2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
I tried your code and I had to delete some stuff from your XML file because it was creating some bugs. It might be related to me but here's your XML I was able to use:
But please do not change your XML file before trying the solution below. If it still doesn't work, you should consider the elements I removed.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="hello"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
Next I created your Project2Activity java class and activity2 (pretty much empty since you gave no info about it).
So when trying to run your application you get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application.so/com.example.application.so.Project2Activity}: java.lang.NumberFormatException: For input string: ""
and it is at :
at com.example.application.so.Project2Activity.onCreate(Project2Activity.java:24)
now if we consider this lines 23 and 24:
String value=pek.getText().toString(); // line 23
final int pekerja=Integer.parseInt(value); // line 24
The error is that you are trying to read (line 23) your editText Value at the creation of the activity, at that time, your editText is still blank which means value will be equal to "" (empty string).
What you want to do instead is to put your lines from 22 to 33 (the ones below) in a button click event, I tried putting them in your button 1 click event and your app doesn't crash any more:
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
So to summarize, the problem is that you are extracting empty strings from your editTexts and trying to make integers out of them. Instead try making that all reading and integer extracting process on the click of a button where your really need these inputs. just like get the inputs and the final moment.
You should pay attention to your logcat (error messages), it is quite helpful.
What you can also do is to verify that your editTexts are not empty while trying to extract strings from them and raise exception or Toast if they are empty. Nothing too complicated just something like:
if (value.equals("")):
// editText is empty so do something
You can refer to this link for further understanding of how to know when an editText is empty.
MainActivity.Java
package com.example.drexsprint.login;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1,b2;
EditText ed1,ed2;
TextView tx1;
int counter = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
ed1=(EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
b2=(Button)findViewById(R.id.button2);
tx1=(TextView)findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("1234")) {
Toast.makeText(getApplicationContext(), "Redirecting...",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}}
activity_main.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: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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arduino Security"
android:id="#+id/textView"
android:textColor="#ff7aff24"
android:textSize="35dp"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:hint="Enter Name"
android:focusable="true"
android:textColorHighlight="#ff7eff15"
android:textColorHint="#ffff25e6"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:textColorHint="#ffff299f"
android:hint="Password" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attempts Left:"
android:id="#+id/textView2"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView3"
android:layout_alignTop="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/textView2"
android:textSize="25dp"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:textColor="#ddec0d"
android:background="#0e17c2"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:background="#df0c0c"
android:textColor="#0dca07"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView"
android:layout_centerHorizontal="true" />
<TextView
android:text="Please Login:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:textSize="35dp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
Second Screen App
screen.Java
package com.example.drexsprint.login;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Screen extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
}
public void Left(View view) {
myWebView.loadUrl("http://admin:0000000AA#10.0.1.16/?button2");
}
public void Right (View view) {
myWebView.loadUrl("http://admin:0000000AA#10.0.1.16/?button3");
}
public void Temp (View view) {
myWebView.loadUrl("http://admin:0000000AA#10.0.1.16/?button1");
}
public void Hum (View view) {
myWebView.loadUrl("http://admin:0000000AA#10.0.1.16/button1");
}
public void Photo (View view) {
goToUrl("http://admin:0000000AA#10.0.1.6/image.jpg");
}
private void goToUrl(String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
/** public void Photo (View view) {
// myWebView.loadUrl("http://admin:0000000AA#10.0.1.6/image.jpg");
}**/
}}
screen_layout
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:visibility="invisible"
android:layout_above="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate Left"
android:id="#+id/button"
android:onClick="Left"
android:backgroundTint="#0f50e8"
android:textColor="#f4d318"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="#+id/button5"
android:layout_alignRight="#+id/button4"
android:layout_alignEnd="#+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate Right"
android:id="#+id/button2"
android:onClick="Right"
android:backgroundTint="#0f50e8"
android:textColor="#f4d318"
android:layout_below="#+id/webView"
android:layout_alignRight="#+id/webView"
android:layout_alignEnd="#+id/webView"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Temp."
android:id="#+id/button3"
android:onClick="Temp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:backgroundTint="#369d7c"
android:textColor="#f4d318" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Hum."
android:id="#+id/button4"
android:onClick="Hum"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:backgroundTint="#369d7c"
android:textColor="#f4d318" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Photo"
android:id="#+id/button5"
android:onClick="Photo"
android:backgroundTint="#589d36"
android:textColor="#f4d318"
android:layout_below="#+id/webView"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
<VideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/videoView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/webView"
android:background="#0a0a0a"
android:clickable="true"
android:contextClickable="true"
android:focusable="true" />
</RelativeLayout>
Okay! So I got two aplications, the first one is a login screen and the second one is a main menu of what the application should do, I want to find a way so that when a user insert the correct credentials and click on the Login button the app executes the second screen the main menu, how can i do this, Join the two apps together.
Wat i wanna achieve is on the the click of "Login Button" on the first app it Launches the Second app as a window! Thanks!! Kind of new in android any detailed help would be much appreciated.
Use startActivityForResult() to get the result from another activity.
By the help of android startActivityForResult() method, we can send information from one activity to another and vice-versa.
Intent intent=new Intent(this,Class2.class);
startActivityForResult(intent,10);
main activity.java
package abhilmohan.blogspot.com;
import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
EditText amount1;
EditText amount2;
EditText amount3;
EditText amount4;
Button calculate;
double w=0;
double x=0;
double y=0;
double z=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar= getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff0000")));
}
public void initcontrols() {
amount1=(EditText)findViewById(R.id.editText1);
amount2=(EditText)findViewById(R.id.editText2);
amount3=(EditText)findViewById(R.id.editText3);
amount4=(EditText)findViewById(R.id.editText4);
calculate=(Button)findViewById(R.id.button1);
}
public void calculate() {
w=Double.parseDouble(amount1.getText().toString());
x=Double.parseDouble(amount2.getText().toString());
y=w/12;
amount3.setText(Double.toString(y));
z=w*x/100;
amount4.setText(Double.toString(z));
}
public void gotoactivity (View v) {
Intent intent = new Intent(this,ResultPage.class);
calculate();
startActivity(intent);
}
am not getting result while calling calculator() void method on button click.i want my results to be published in two textviews created in reulst_page layout
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:gravity="left"
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="abhilmohan.blogspot.com.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:layout_marginRight="40dp"
android:text="#string/ctc"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="75dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="-15dp"
android:inputType="number"
android:background="#drawable/rounded_edit_text"
android:ems="10"
android:padding="20dp"
android:paddingBottom="50dp"
android:textColor="#000000" />
<TextView
android:id="#+id/textView2"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="90dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="-30dp"
android:text="#string/TDS"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="-70dp"
android:background="#drawable/rounded_edit_text"
android:inputType="number"
android:ems="10"
android:padding="20dp"
android:paddingBottom="50dp" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:background="#drawable/button_style"
android:text="#string/ok"
android:textColor="#ffffff"
android:onClick="gotoactivity" />
</LinearLayout>
</RelativeLayout>
resultpage.java
package abhilmohan.blogspot.com;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.app.ActionBar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
public class ResultPage extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_page);
getActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar bar= getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff0000")));
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()== android.R.id.home)
{
finish();
}
return super.onOptionsItemSelected(item);
}
}
result_page.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="#drawable/background"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:layout_marginRight="40dp"
android:text="#string/amount"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="75dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="20dp"
android:layout_marginTop="-15dp"
android:background="#drawable/rounded_edit_text"
android:ems="10"
android:inputType="number"
android:padding="20dp"
android:paddingBottom="50dp"
android:textColor="#000000"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView
android:id="#+id/textView2"
style="#style/text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="90dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="-30dp"
android:text="#string/tdsamount"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="-70dp"
android:background="#drawable/rounded_edit_text"
android:ems="10"
android:inputType="number"
android:padding="20dp"
android:paddingBottom="50dp"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button2"
android:layout_width="177dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:background="#drawable/button_style"
android:text="#string/rate"
android:textColor="#ffffff" />
</LinearLayout>
At the moment you are executing calculate() in the MainActivity.java second activity called ResultPage doesn't exist, therefore you can't change it's view (editText3and editText4).
In order to pass data to another activity you should fill your Intent with some extra data and then in your ResultPage activity's onCreate you would get underlying extras.
EDIT
MainActivity.java
public void gotoactivity (View v) {
calculate();
Intent intent = new Intent(this, ResultPage.class);
intent.putExtra("AMOUNT_3", y);
intent.putExtra("AMOUNT_4", z);
startActivity(intent);
}
ResultPage.java inside onCreate
Bundle extras = getIntent().getExtras();
if (extras != null) {
int amount3 = extras.getInt("AMOUNT_3");
int amount4 = extras.getInt("AMOUNT_4");
}
I have three fragments, Player1Tun, Player2Turn and TicTacToeLayout. Player1Turn and Player2Turn are inner-switching every time a player makes a move and TicTacToeLayout contains the tic tac toe table which update everytime the fragments switches. What I'm trying to do is to send the data from either player fragments to the host activity (while the program is still running) and then from there, I'll update the table in the TicTacToeLayout. Any ideas or method I can do it?
Player1:
package As2.packageTK;
//import android.app.Activity;
import java.util.ArrayList;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class PlayerTurn2 extends Fragment{
TextView p2Name;
TextView p2Icon;
Button doneP2;
Button resetP2;
EditText row;
EditText column;
TicTacToeLayout myObject2 = new TicTacToeLayout();
ArrayList<String> player2;
Bundle extras = new Bundle();
int turn = 2;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.playerturn2, container, false);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
extras = getArguments();
player2 = new ArrayList<String>(extras.getStringArrayList("player2"));
//Toast.makeText(getActivity(), player2.get(0), Toast.LENGTH_LONG).show();
//Toast.makeText(getActivity(), player2.get(1), Toast.LENGTH_LONG).show();
p2Name = (TextView) getActivity().findViewById(R.id.p2NameInfo);
p2Icon = (TextView) getActivity().findViewById(R.id.p2IconInfo);
row = (EditText) getActivity().findViewById(R.id.rowP2);
column = (EditText) getActivity().findViewById(R.id.columnP2);
doneP2 = (Button) getActivity().findViewById(R.id.doneP2);
//resetP2 = (Button) getActivity().findViewById(R.id.resetP2);
setPlayer(); //sets all the information of player 2, name, icon, image, etc
doneP2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
callPlayer1Fragment(); //switches with first player
}
});
}
public void callPlayer1Fragment()
{
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment Player1Frag = new PlayerTurn1();
Player1Frag.setArguments(extras);
ft.replace(R.id.fragment_container, Player1Frag);
ft.commit();
}
public boolean checkField()
{
if(row == null || column == null)
{
Toast.makeText(getActivity(), "Please input the row or column!", Toast.LENGTH_LONG).show();
return false;
}
else
return true;
}
public void setPlayer()
{
String name = player2.get(0);
if(!name.equals(""))
p2Name.setText("Player Name: " + name);
else
p2Name.setText("");
String icon = player2.get(1);
if(!icon.equals(""))
p2Icon.setText("Player Icon: " + icon);
else
p2Icon.setText("");
}
}
player 2 fragment is exactly the same code so I won't bother adding it.
TicTacToeLayout fragment class:
public class TicTacToeLayout extends Fragment {
TextView image1, image2, image3, image4, image5, image6, image7, image8, image9;
TextView[][] images;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tictactoe_layout, container, false);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
image1 = (TextView) getActivity().findViewById(R.id.Image1);
image2 = (TextView) getActivity().findViewById(R.id.Image2);
image3 = (TextView) getActivity().findViewById(R.id.Image3);
image4 = (TextView) getActivity().findViewById(R.id.Image4);
image5 = (TextView) getActivity().findViewById(R.id.Image5);
image6 = (TextView) getActivity().findViewById(R.id.Image6);
image7 = (TextView) getActivity().findViewById(R.id.Image7);
image8 = (TextView) getActivity().findViewById(R.id.Image8);
image9 = (TextView) getActivity().findViewById(R.id.Image9);
images = new TextView[][]{ {image1, image2, image3},
{image4, image5, image6},
{image7, image8, image9} };
toast();
}
public void toast()
{
Toast.makeText(getActivity(), images[0][0].getText().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), images[0][1].getText().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), images[0][2].getText().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), images[1][0].getText().toString(), Toast.LENGTH_LONG).show();
}
public void play(int row, int column, String icon)
{
images[row-1][column-1].setText(icon);
}
}
here's the XML layout for playerturn2
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="#+id/tablerow0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/player2Num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="100dp"
android:gravity="center_horizontal"
android:text="Player 2"
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="#+id/tablerow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/p2NameInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player Name: "
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="#+id/tablerow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/p2IconInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player icon: "
android:textSize="20dp" />
</TableRow>
<TableRow
android:id="#+id/tablerow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/p2PicInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player picture:"
android:textSize="20dp" />
</TableRow>
<ImageView
android:id="#+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#android:drawable/editbox_background" />
<TableRow
android:id="#+id/rowNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="50dp"
android:gravity="center_horizontal"
android:text="Row (1-3):"
android:textSize="20dp" />
<EditText
android:id="#+id/rowP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:width="50dp" />
</TableRow>
<TableRow
android:id="#+id/colNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginLeft="50dp"
android:gravity="center_horizontal"
android:text="Col (1-3):"
android:textSize="20dp" />
<EditText
android:id="#+id/columnP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:width="50dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" >
<Button
android:id="#+id/doneP2"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="Done"
/>
<Button
android:id="#+id/resetP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
/>
</TableRow>
</TableLayout>
here's the layout for tictactoelayout.xml
<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tictactoe"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" >
<TextView
android:id="#+id/Image1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="1"
android:textSize="70dp" />
<TextView
android:id="#+id/Image2"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="2"
android:textSize="70dp" />
<TextView
android:id="#+id/Image3"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="3"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/Image4"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="4"
android:textSize="70dp" />
<TextView
android:id="#+id/Image5"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="5"
android:textSize="70dp" />
<TextView
android:id="#+id/Image6"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="6"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/Image7"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="7"
android:textSize="70dp" />
<TextView
android:id="#+id/Image8"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="8"
android:textSize="70dp" />
<TextView
android:id="#+id/Image9"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="9"
android:textSize="70dp" />
</TableRow>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/win"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Winner"
android:textSize="20dp" />
<EditText
android:id="#+id/winner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:ems="10" />
</RelativeLayout>
</TableLayout>
and finally the host activity's xml layout: tictactoegame.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/frag2"
android:name="As2.packageTK.TicTacToeLayout"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
My friend did the same but with a implementing an interface he created in the fragments and having the main activity implement the interface. With that, he used a method to pass datas. Maybe it's not clear but if you know what I'm trying to say then you could clear it out for all of us :)
your friend is right, communicating with other fragments is nicely explained in the docs:
http://developer.android.com/training/basics/fragments/communicating.html
I'd like to capture the currently connected SSID - and display it in a TextView - how can this be done?
From what I can gather I'll need to use the following:
public void run() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String nameEt = info.getSSID();
}
But when I attempt to implement it - I cannot seem to display it on the screen.
JAVA:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import java.text.DecimalFormat;
public class AddEditDevice extends Activity {
private long rowID;
private EditText nameEt;
private EditText capEt;
private EditText codeEt;
private TimePicker timeEt;
private TextView ssid;
// DecimalFormat df = new DecimalFormat ("00");
// public String minutes = df.format(min);
// #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_country);
nameEt = (EditText) findViewById(R.id.nameEdit);
capEt = (EditText) findViewById(R.id.capEdit);
codeEt = (EditText) findViewById(R.id.codeEdit);
timeEt = (TimePicker) findViewById(R.id.timeEdit);
Bundle extras = getIntent().getExtras();
if (extras != null) {
rowID = extras.getLong("row_id");
nameEt.setText(extras.getString("name"));
capEt.setText(extras.getString("cap"));
codeEt.setText(extras.getString("code"));
String time = extras.getString("time");
String[] parts = time.split(":");
timeEt.setCurrentHour(Integer.valueOf(parts[0]));
timeEt.setCurrentMinute(Integer.valueOf(parts[1]));
timeEt.setIs24HourView(false);
}
Button saveButton = (Button) findViewById(R.id.saveBtn);
saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (nameEt.getText().length() != 0) {
AsyncTask<Object, Object, Object> saveContactTask =
new AsyncTask<Object, Object, Object>() {
#Override
protected Object doInBackground(Object... params) {
saveContact();
return null;
}
#Override
protected void onPostExecute(Object result) {
finish();
}
};
saveContactTask.execute((Object[]) null);
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(
AddEditDevice.this);
alert.setTitle(R.string.errorTitle);
alert.setMessage(R.string.errorMessage);
alert.setPositiveButton(R.string.errorButton, null);
alert.show();
}
}
});
}
public void run() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String ssid = info.getSSID();
TextView ssidTextView = (TextView) findViewById(R.id.wifiSSID);
ssidTextView.setText(ssid);
}
private void saveContact() {
DatabaseConnector dbConnector = new DatabaseConnector(this);
if (getIntent().getExtras() == null) {
dbConnector.insertContact(nameEt.getText().toString(), capEt
.getText().toString(), timeEt.getCurrentHour().toString() + ":"
+ timeEt.getCurrentMinute().toString(), codeEt.getText()
.toString());
} else {
dbConnector.updateContact(rowID, nameEt.getText().toString(), capEt
.getText().toString(), timeEt.getCurrentHour().toString() + ":"
+ timeEt.getCurrentMinute().toString(), codeEt.getText()
.toString());
}
}
}
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:layout_weight="1" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<EditText
android:id="#+id/nameEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/name_hint"
android:imeOptions="actionNext"
android:inputType="textPersonName|textCapWords" />
<TextView
android:id="#+id/wifiSSID"
style="#style/StyleText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/capEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/cap_hint"
android:imeOptions="actionNext"
android:inputType="textPersonName|textCapWords" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data Limit"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="left"
android:text="Unlimited Data"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:text="10MB"
android:textColor="#ffffff" />
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bandwidth Limit"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="left"
android:text="Unlimited Bandwidth"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:text="10kbs"
android:textColor="#ffffff" />
</LinearLayout>
<EditText
android:id="#+id/codeEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:ems="10"
android:gravity="center"
android:hint="#string/code_hint"
android:imeOptions="actionNext"
android:inputType="textUri"
android:lines="1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="Parental Controls"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="Block Streaming"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ToggleButton
android:id="#+id/ToggleButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="ToggleButton" />
<ToggleButton
android:id="#+id/ToggleButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="ToggleButton" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="Block File Type/Size"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="Banned Music/Video"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ToggleButton
android:id="#+id/ToggleButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="ToggleButton" />
<ToggleButton
android:id="#+id/ToggleButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="center"
android:text="ToggleButton" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceSmall" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:layout_weight="1.0"
android:text="WiFi Time Limit"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#ffffff" />
<TimePicker
android:id="#+id/timeEdit"
android:layout_width="286dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal" >
</LinearLayout>
<Button
android:id="#+id/saveBtn"
android:layout_width="199dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:text="#string/save_btn" />
</LinearLayout>
</ScrollView>
You aren't calling run() from anywhere, and that method is not trying to update a textview. Try doing this in your run method.
TextView ssidTextView = (TextView) findViewById(R.id.<your_textview_id>);
ssidTextView.setText(nameEt);
And thenmake sure you call run() whenever you want to update the ssid in the TextView.