All the files look fine to me - anyone have any ideas/suggestions?
Thanks in advance!
Amani Swann
ERRORS:
Description Resource Path Location Type
Content is not allowed in trailing section. strings.xml /Linking Manager/res/values line 7 Android XML Format Problem
invalid resource directory name AndroidManifest.xml /Linking Manager/bin/res line 1 Android AAPT Problem
error: Error parsing XML: not well-formed (invalid token) strings.xml /Linking Manager/res/values line 7 Android AAPT Problem
Error generating final archive: java.io.FileNotFoundException: C:\Users\User\Desktop\Android Backups\3.1.2013 #1111pm\MyAndroidApp\bin\resources.ap_ does not exist Linking Manager Unknown Android Packaging Problem
main.xml file:
<?xml version="1.0" encoding="utf-8"?>
main2.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:id="#+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:textAppearance="?android:textAppearanceLarge" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="User Settings:" />
<TextView android:textAppearance="?android:textAppearanceLarge" android:id="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<TextView android:textAppearance="?android:textAppearanceMedium" android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Data Limit" />
<TextView android:textAppearance="?android:textAppearanceSmall" android:id="#+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<SeekBar android:id="#+id/seekBar1" android:layout_width="fill_parent" android:layout_height="wrap_content" />
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:gravity="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="10MB" android:layout_weight="1.0" />
<TextView android:gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Unlimited Data" android:layout_weight="1.0" />
</LinearLayout>
<TextView android:textAppearance="?android:textAppearanceSmall" android:id="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<TextView android:textAppearance="?android:textAppearanceMedium" android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bandwidth Limit" />
<TextView android:textAppearance="?android:textAppearanceSmall" android:id="#+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<SeekBar android:id="#+id/seekBar1" android:layout_width="fill_parent" android:layout_height="wrap_content" />
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:gravity="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="10kbs" android:layout_weight="1.0" />
<TextView android:gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Unlimited Bandwidth" android:layout_weight="1.0" />
</LinearLayout>
<TextView android:textAppearance="?android:textAppearanceSmall" android:id="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:textAppearance="?android:textAppearanceMedium" android:id="#+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="WiFi Time Limit" />
<TextView android:textAppearance="?android:textAppearanceSmall" android:id="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<TimePicker android:id="#+id/timePicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50.0dip" android:layout_weight="1.0" />
<TextView android:textAppearance="?android:textAppearanceSmall" android:id="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<EditText android:id="#+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="Blocked Sites - [ex: www.xxx.com]" android:ems="10" />
</LinearLayout>
AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mkyong.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".SplashScreen"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".main2" >
</activity>
<activity android:name=".home" >
</activity>
<activity android:name=".App2Activity" >
</activity>
<activity
android:name=".AppActivity"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
AppActivity.java (primary java file)
package com.mkyong.android;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
public class AppActivity extends Activity {
final Context context = this;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Settings Menu");
// set dialog message
alertDialogBuilder
.setMessage("Link or Delete?")
.setCancelable(false)
.setPositiveButton("Link",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//start new activity
Intent intentApp2Activity = new Intent(AppActivity.this, App2Activity.class);
startActivity(intentApp2Activity);
// if this button is clicked, close
// current activity
AppActivity.this.finish();
}
})
.setNegativeButton("Delete",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}}
App2Activity.java
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Linking Manager</string>
<string name="button1">Button1</string>
<string name="button2">Button2</string>
<string name="button3">Button3</string>
</resources>
ERRORS: (shown above at the top of this thread)
All the files look fine to me - anyone have any ideas/suggestions?
Thanks in advance!
Amani Swann
Try this.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:textAppearance="?android:textAppearanceLarge" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="NFC Linking Manager" />
<Button android:id="#+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Linksys Router (Home)" android:onClick="onPopupBtClick" />
<Button android:id="#+id/Button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Netgear Router (Office)" />
<Button android:id="#+id/Button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Cisco Router (Office)" />
</LinearLayout>
I edited your layout like android:id="#+id/Button3" in every components
Do same for main2.xml
Related
I'm developing an app for my school with a timetable and a little calculator
Here's the manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.markwitt.schul_app">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:launchMode="singleTop"
android:minSdkVersion="20"
android:supportsRtl="true"
android:targetSdkVersion="25"
android:theme="#style/AppTheme">
<activity android:name=".Stundenplan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Vertretungsplan" />
<activity android:name=".Rechner" />
<activity android:name=".Overlay"></activity>
</application>
Here's the Launcher activity activity_stundenplan.xml:
<?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:background="#color/white">
<Button
android:text="Rechner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonrechner"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="44dp"
android:layout_marginStart="44dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonvertr"
android:text="Vertretungsplan"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/Tabelle"
android:layout_alignEnd="#+id/Tabelle" />
<TableLayout
android:id="#+id/Tabelle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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.markwitt.schul_app.Stundenplan"
android:background="#color/white"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/r10"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r20"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r30"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r40"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r50"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r60"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/r11"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView android:text="#string/r21" android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r31"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r41"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r51"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r61"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/r12"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r22"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r32"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r42"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r52"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r62"
android:padding="10dp"
android:background="#drawable/cell_shape"
//more TableRows
</TableLayout>
</Relativelayout>
I've made an Intent in Stundenplan.java:
rechner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(Stundenplan.this, Rechner.class));
}
});
The Problem is that everytime I debug-launch the app on my Xperia Z3 and press the button to open the "Rechner"-Activity, the app closes and it shows me the following error:(sry for the bad format)
I/OpenGLRenderer: Initialized EGL, version 1.4
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy#f97be6e time:6061206
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy#f97be6e time:6065998
I/Timeline: Timeline: Activity_launch_request id:com.example.markwitt.schul_app time:6074313
D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.markwitt.schul_app, PID: 22562
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.markwitt.schul_app/com.example.markwitt.schul_app.Rechner}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2375)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:203)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193)
at com.example.markwitt.schul_app.Rechner.<init>(Rechner.java:16)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/Process: Sending signal. PID: 22562 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'
My Rechner.java and my activity_rechner look like this:
package com.example.markwitt.schul_app;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class Rechner extends AppCompatActivity {
private RadioGroup radioGroup;
private RadioButton checked;
private Button los;
public String mode;
EditText number1 = (EditText) findViewById(R.id.nummer1);
EditText number2 = (EditText) findViewById(R.id.nummer2);
EditText ergebnis = (EditText) findViewById(R.id.ergebnis);
public int text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechner);
addListenerOnButton();
}
public void addListenerOnButton() {
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
los = (Button) findViewById(R.id.button5);
los.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = radioGroup.getCheckedRadioButtonId();
checked = (RadioButton) findViewById(selectedId);
switch (checked.getId()){
case R.id.plus: mode = "+";
case R.id.minus: mode = "-";
case R.id.mal: mode = "*";
case R.id.geteilt: mode = ":";
case R.id.fakultaet: mode = "fak";
}
text1 = Integer.parseInt(number1.getText().toString());
int text2 = Integer.parseInt(number2.getText().toString());
switch (mode){
case "+": ergebnis.setText(text1 + text2);
case "-": ergebnis.setText(text1 - text2);
case "*": ergebnis.setText(text1 * text2);
case ":": ergebnis.setText(text1 / text2);
case "fak": ergebnis.setText(Long.toString(fakultaet(text1)));
}
}
});
}
static long fakultaet(int n) {
int ergebnis = 1;
for (int i = 1; i <= n; i++) {
ergebnis = ergebnis * i;
}
return ergebnis;
}
}
...
<?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:id="#+id/activity_durchschnitt"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.markwitt.schul_app.Rechner">
<EditText
android:id="#+id/nummer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:layout_weight="1"
android:ems="10"
android:hint="Nummer 1"
android:inputType="number"
android:maxLines="1"
android:textAlignment="center" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/ergebnis"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:text="=" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/nummer1"
android:layout_marginEnd="42dp"
android:layout_marginRight="42dp"
android:layout_marginTop="69dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/mal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/geteilt"
android:layout_alignBottom="#+id/geteilt"
android:layout_toLeftOf="#+id/geteilt"
android:layout_toStartOf="#+id/geteilt"
android:layout_weight="1"
android:text="* "
android:textAllCaps="true"
android:textStyle="bold" />
<RadioButton
android:id="#+id/fakultaet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/mal"
android:layout_alignBottom="#+id/mal"
android:layout_toLeftOf="#+id/mal"
android:layout_toStartOf="#+id/mal"
android:layout_weight="1"
android:text="Fakultät "
android:textAllCaps="true"
android:textStyle="bold" />
<RadioButton
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/minus"
android:layout_alignBottom="#+id/minus"
android:layout_toEndOf="#+id/nummer1"
android:layout_toRightOf="#+id/nummer1"
android:layout_weight="1"
android:text="+ "
android:textAllCaps="true"
android:textStyle="bold" />
<RadioButton
android:id="#+id/geteilt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/radioGroup"
android:layout_toEndOf="#+id/radioGroup"
android:layout_toRightOf="#+id/radioGroup"
android:layout_weight="1"
android:text=": "
android:textAllCaps="true"
android:textStyle="bold" />
<RadioButton
android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/geteilt"
android:layout_alignBottom="#+id/geteilt"
android:layout_toLeftOf="#+id/plus"
android:layout_toStartOf="#+id/plus"
android:layout_weight="1"
android:text="- "
android:textAllCaps="true"
android:textStyle="bold" />
</RadioGroup>
<EditText
android:id="#+id/ergebnis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nummer2"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/nummer2"
android:layout_marginBottom="61dp"
android:ems="10"
android:hint="Ergebnis"
android:inputType="number"
android:textAlignment="center" />
<EditText
android:id="#+id/nummer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nummer1"
android:layout_alignStart="#+id/nummer1"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Nummer 2"
android:inputType="number"
android:textAlignment="center" />
</RelativeLayout>
Can you please give me an answer what to do? It does the same on my phone and on my emulator
If you have questions, contact me please!
PS: I have to finish it till the 12th December
You cannot use findviewbyid before you set the content view. It returns null and that's your exception.
Change your code to something like that:
EditText number1;
EditText number2;
EditText ergebnis;
public int text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechner);
number1 = (EditText) findViewById(R.id.nummer1);
number2 = (EditText) findViewById(R.id.nummer2);
ergebnis = (EditText) findViewById(R.id.ergebnis);
addListenerOnButton();
}
Also, Ill recommend you to read the android code style recommendations.
https://source.android.com/source/code-style.html
I am designing an app that has a home screen with 6 image buttons, that all start new activities.
Currently when I press the button, the app crashes. This is strange however as I have done this project with regular buttons and it works fine, and I have also made it so when the image button is pressed it just prints "Clicked!" on thee screen and this also works fine, so the problem is starting a new activity.
MainActivity.java
package com.example.darren1.homemanagementsystem;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.View.OnClickListener;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findAllViewsById();
}
private void findAllViewsById(){
ImageButton lightButton = (ImageButton) findViewById(R.id.lightButton);
lightButton.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){
Intent intent = new Intent(MainActivity.this, LightingActivity.class);
startActivity(intent);
}
});
}
}
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"
android:background="#feae5e"
android:clickable="true">
<GridLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:background="#feae5e"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true">
<ImageButton
android:layout_width="137dp"
android:layout_height="133dp"
android:id="#+id/cameraButton"
android:layout_row="0"
android:layout_column="0"
android:src="#drawable/rsz_cameraicon"
android:background="#feae5e" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/alarmButton"
android:layout_row="0"
android:layout_column="13"
android:src="#drawable/rsz_alarmicon"
android:background="#feae5e" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fireButton"
android:layout_row="1"
android:layout_column="0"
android:src="#drawable/rsz_fireicon"
android:background="#feae5e"
android:layout_marginTop="40dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lightButton"
android:layout_row="1"
android:layout_column="13"
android:src="#drawable/rsz_lightbulbicon"
android:background="#feae5e"
android:layout_marginTop="40dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thermoButton"
android:layout_row="2"
android:layout_column="0"
android:src="#drawable/rsz_1thermometericon"
android:background="#feae5e"
android:layout_marginTop="40dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvButton"
android:layout_row="2"
android:layout_column="13"
android:src="#drawable/rsz_1tvicon"
android:background="#feae5e"
android:layout_marginTop="40dp" />
</GridLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.darren1.homemanagementsystem" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LightingActivity"
android:label="Light" >
</activity>
<activity android:name=".TelevisionActivity"
android:label="TV">
</activity>
</application>
activity_lighting.xml
<?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: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="com.example.darren1.homemanagementsystem.LightingActivity"
android:background="#beddeb">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="false"
android:id="#+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/lighting"
android:id="#+id/lightingView"
android:layout_gravity="center_horizontal"
android:textSize="32dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_below="#+id/linearLayout"
android:layout_alignParentStart="true"
android:layout_marginTop="66dp"
android:id="#+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/kitchen"
android:id="#+id/kitchenLight" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/sittingroom"
android:id="#+id/sittingroomLight"
android:layout_marginTop="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/bedroom"
android:id="#+id/bedroomLight"
android:layout_marginTop="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/outside"
android:id="#+id/outsideLight"
android:layout_marginTop="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_alignTop="#+id/linearLayout2"
android:layout_alignEnd="#+id/linearLayout"
android:id="#+id/linearLayout3">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/switch1" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/switch2"
android:layout_marginTop="20dp" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/switch3"
android:layout_marginTop="20dp" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/switch4"
android:checked="false"
android:layout_marginTop="20dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="#string/backButton"
android:id="#+id/backButton"
android:layout_gravity="center_horizontal"
android:background="#010101"
android:textColor="#fefdfd"
android:textStyle="bold"
android:textSize="20dp" />
</LinearLayout>
LightingActivity.java
package com.example.darren1.homemanagementsystem;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View.OnClickListener;
public class LightingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("Light");
setContentView(R.layout.activity_lighting);
onClickListenerButton();
}
public void onClickListenerButton(){
Intent i = getIntent();
}
}
The problem I guess comes with this two lines :
getActionBar().setTitle("Light");
setContentView(R.layout.activity_lighting);
You should put frist the setContentView() and then the Title of your ActionBar
If I were you I'd start using [Toolbar](http://developer.android.com/reference/android/widget/Toolbar.html)
Do thesetTitle()` as follows :
ActionBar actionBar = getActionBar();
actionBar.setTitle("Light");
See this answer for more detail
Not correct:
setContentView(R.layout.activity_light); -> activity_lighting.xml
Different name.
But you have another different name:
setContentView(R.layout.activity_main); -> activity_mainn.xml
(Maybe typing error)
I am using eclipse for developing a game for android. The game is pretty much finished but it won't launch correctly on my emulator of choice (BlueStacks).
It only shows a white screen and the infobar for about 0.5 seconds before crashing. App is still open in background after crashing but can't do or display anything.
I can compile and install the application and the syntax is correct... I assume that my mistake is somewhere in the manifest.
here is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.ralfkraemer.starsandstrikes"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="de.ralfkraemer.starsandstrikes.MainMenu"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The following is my main activity class
package de.ralfkraemer.starsandstrikes;
import java.io.IOException;
import java.util.Random;
import de.ralfkraemer.starsandstrikes.R;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainMenu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymainmenu);
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
ActionBar actionBar = getActionBar();
actionBar.hide();
randomizeFact();
}
// RANDOM FACT
String[] factresource = getResources().getStringArray(R.array.factres);
protected void randomizeFact(){
TextView randomFact = (TextView)findViewById(R.id.text1);
Random random = new Random();
int maxIndex = factresource.length;
int generatedIndex = random.nextInt(maxIndex);
randomFact.setText(factresource[generatedIndex]);
}
// START GAME
public void startGame(){
Game currentGame = new Game();
currentGame.setScore(0);
setContentView(R.layout.activitygame);
currentGame.running = true;
}
// PLAY STORE (RATE)
public void openPlayStore(){
Uri uri = Uri.parse("url");
Intent intent = new Intent (Intent.ACTION_VIEW, uri);
startActivity(intent);
}
public void goToAbout(){
setContentView(R.layout.activityabout);
}
public void goToHowToPlay(){
setContentView(R.layout.activityhowtoplay);
}
public void goToHighscores() throws IOException{
Highscores highscores = new Highscores();
setContentView(R.layout.activityhighscores);
}
}
And here is the main activity xml
<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:orientation="vertical"
android:background="#drawable/background"
tools:context="${linearPackage}.${activityClass}" >
<ImageView
android:id="#+id/header"
android:contentDescription="#string/header_content"
android:src="#drawable/logo_mainmenu"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="10dp" />
<Button
android:id="#+id/button_play"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="65dp"
android:layout_marginRight="65dp"
android:text="#string/mm_button_play"
android:textSize="22sp"
android:onClick="startGame"
android:background="#drawable/button_default" />
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="#string/mm_randomfact_placeholder"
android:textSize="16sp"
android:gravity="center"/>
<Button
android:id="#+id/button_randomfact"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="65dp"
android:layout_marginRight="65dp"
android:text="#string/mm_button_randomfact"
android:textSize="18sp"
android:onClick="randomizeFact"
android:background="#drawable/button_default" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="vertical"
android:baselineAligned="false" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="#+id/button_highscores"
android:layout_width="0dip"
android:layout_height="55dp"
android:layout_weight="1"
tools:ignore="NestedWeights"
android:text="#string/mm_button_highscores"
style="android:attr/buttonBarButtonStyle"
android:background="#drawable/button_default"
android:onClick="goToHighscores" />
<Button
android:id="#+id/button_howtoplay"
android:layout_width="0dip"
android:layout_height="55dp"
android:layout_weight="1"
tools:ignore="NestedWeights"
android:text="#string/mm_button_howtoplay"
style="android:attr/buttonBarButtonStyle"
android:background="#drawable/button_default"
android:onClick="goToHowToPlay" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:id="#+id/button_about"
android:layout_width="0dip"
android:layout_height="55dp"
android:layout_weight="1"
tools:ignore="NestedWeights"
android:text="#string/mm_button_about"
style="android:attr/buttonBarButtonStyle"
android:background="#drawable/button_default"
android:onClick="goToAbout" />
<Button
android:id="#+id/button_rate"
android:layout_width="0dip"
android:layout_height="55dp"
android:layout_weight="1"
tools:ignore="NestedWeights"
android:text="#string/mm_button_rate"
style="android:attr/buttonBarButtonStyle"
android:background="#drawable/button_default"
android:onClick="openPlayStore" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
You are calling getResources while the activity is instantiating on this line here:
// RANDOM FACT
String[] factresource = getResources().getStringArray(R.array.factres);
and the implied Activity.this object does not have a valid context, so it crashes. You need to call this in onCreate or in another method that is called after the object instantiates.
I am writing an app that will eventually use Bluetooth to get a value for two devices' proximity to one another. I am currently trying to use buttons and onClick/onclicklistener etc to bring switch screens from the main UI to a log in or sign up page. Now my app is forcing close on start up and I'm not sure why. Thanks for any help!!
MainActivity:
package com.example.chirp;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter() ;
String status; {
if (bluetooth != null)
{
if (bluetooth.isEnabled())
{
String mydeviceaddress = bluetooth.getAddress();
String mydevicename = bluetooth.getName();
status = mydevicename + ":" + mydeviceaddress;
}
else
{
status = "Bluetooth is not Enabled.";
}
}
Toast.makeText(this, status, Toast.LENGTH_LONG).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loginbutton = (Button) findViewById(R.id.button1);
loginbutton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
setContentView(R.layout.loginscreen);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void newMessage(View v){
Intent intent = new Intent(this,loginscreen.class);
startActivity(intent);
}
}
activity_main.xml
<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:orientation="vertical"
android:background="#7FFFD4"
android:padding="20dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<!-- <public void loginhere(View view) {
android:
}/>-->
<TextView
android:id="#+id/textView1"
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/hello_world"
android:textSize="50sp"
android:typeface="serif" />
<SeekBar
android:id="#+id/seekBar1"
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:layout_gravity="center_horizontal"
android:text="Slide to Quick Find"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/button2"
android:layout_width="396dp"
android:layout_height="100dp"
android:layout_marginLeft="75dp"
android:layout_marginTop="300dp"
android:textSize="50sp"
android:text="Sign Up" />
<Button
android:id="#+id/button1"
android:layout_width="396dp"
android:layout_height="100dp"
android:layout_marginLeft="75dp"
android:layout_marginTop="50dp"
android:textSize="50sp"
android:onClick="onclick"
android:text="Log In" />
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chirp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<supports-screens
android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"
android:requiresSmallestWidthDp="320"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.chirp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.chirp.loginscreen"
android:label="CHIRP"
/>
</application>
</manifest>
loginscreen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#7FFFD4"
android:padding="20dp"
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=".login" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
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="textEmailAddress" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
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="textPassword" >
<requestFocus />
</EditText>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Username" />
/
</LinearLayout>
Move this code inside your onCreate method:
if (bluetooth != null) {
if (bluetooth.isEnabled()) {
String mydeviceaddress = bluetooth.getAddress();
String mydevicename = bluetooth.getName();
status = mydevicename + ":" + mydeviceaddress;
} else {
status = "Bluetooth is not Enabled.";
}
//etc...
Currently this code block is outside of any method of your activity and that is for sure not what you want to have.
I am new to programming and after a while I finally fixed all my errors but now the app crashes on startup! I have not written all of this code by myself but I have edited it. Also when I still received errors the app could start (but not work obviously).
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tk.iWeld.iweld"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="tk.iWeld.iweld.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity_Main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="#+id/editText2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText2"
android:layout_alignBottom="#+id/editText2"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="Text2"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText2"
android:layout_centerHorizontal="true"
android:text="X"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/holo_orange_dark" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:text="_________________________"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/holo_orange_dark" />
<EditText
android:id="#+id/Text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Text3"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/Text3"
android:layout_marginTop="22dp"
android:text="="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/holo_orange_dark"
android:textColorHint="#android:color/holo_orange_dark"
android:textSize="30sp" />
<TextView
android:id="#+id/textRes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignParentRight="true"
android:clickable="false"
android:longClickable="false"
android:text="Result"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/resultbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignTop="#+id/textView3"
android:text="Result" />
<EditText
android:id="#+id/editText1"
android:layout_width="89dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_alignLeft="#+id/textView2"
android:ems="10"
android:hint="Text1"
android:inputType="number" />
And the MainActivity
package tk.iWeld.iweld;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.menu.main);
}
public void calculateClickHandler(View view) {
// make sure we handle the click of the calculator button
if (view.getId() == R.id.resultbutton) {
// get the references to the widgets
EditText text1Text = (EditText)findViewById(R.id.editText1);
EditText text2Text = (EditText)findViewById(R.id.editText2);
EditText text3Text = (EditText)findViewById(R.id.Text3);
TextView resultText = (TextView)findViewById(R.id.textRes);
// get the users values from the widget references
float text1 = Float.parseFloat(text1Text.getText().toString());
float text2 = Float.parseFloat(text2Text.getText().toString());
float text3 = Float.parseFloat(text3Text.getText().toString());
// calculate the result value
float totalresult = calculateRESULT(text1, text2, text3);
// now set the value in the result text
resultText.setText("" + totalresult);
}
}
// the formula to calculate the result index
private float calculateRESULT (float text1, float text2, float text3) {
return (float) (text1 * text2 / text3);
}
}
There is a mistake in the second line in onCreate():
setContentView(R.menu.main);
You cannot set a menu as view. If you want to use the main layout, use this:
setContentView(R.layout.main);
This...
android:targetSdkVersion="22" />
is SCIENCE FICTION!
The maximum API level today is 19