Android simple program interest calculator - java

I am in the very first lessons of programming for Android and for my first attempt I tried to develop an interest calculator. The app starts but then crashes. The code follows:
package com.example.vitor.precojusto;
import com.example.vitor.precojusto.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
public class SICalculatorActivity extends Activity {
private TextView PA;
private TextView Interest_Rate;
private TextView Years;
private EditText PA_bar;
private EditText IR_bar;
private SeekBar year_bar;
private Button calculate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sicalculator);
PA = (TextView) findViewById(R.id.PA);
Interest_Rate = (TextView) findViewById(R.id.Interest_Rate);
Years= (TextView) findViewById(R.id.Years);
PA_bar= (EditText) findViewById(R.id.PA_bar);
IR_bar= (EditText) findViewById(R.id.IR_bar);
year_bar=(SeekBar) findViewById(R.id.year_bar);
calculate=(Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
}
});
}
}
sicalculator.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp"
tools:context=".SICalculatorActivity" >
<TextView
android:id="#+id/Years"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/IR_bar"
android:layout_centerVertical="true"
android:text="2 Year(s)"
android:textSize="20sp" />
<SeekBar
android:id="#+id/year_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Years"
android:layout_below="#+id/Years"
android:layout_marginTop="21dp" />
<Button
android:id="#+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/year_bar"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/year_bar"
android:text="Calculate" />
<EditText
android:id="#+id/IR_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Years"
android:layout_marginBottom="14dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/Interest_Rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/IR_bar"
android:layout_alignLeft="#+id/IR_bar"
android:layout_marginBottom="15dp"
android:text="Interest Rate"
android:textSize="20sp" />
<TextView
android:id="#+id/PA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/PA_bar"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="Principal Amount"
android:textSize="20sp" />
<EditText
android:id="#+id/PA_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Interest_rate"
android:layout_below="#+id/PA"
android:layout_marginTop="17dp"
android:ems="10"
android:inputType="number" />
</RelativeLayout>
Do you guys have any guess why the app crashes? I tried to run it on an emulation of nexus on Android Studio (Android 5.0 Lollipop).

You have to change your manifest.xml to declare this activity as the main one.
<activity android:name=".SICalculatorActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Dont forget to post the stacktrace, you can find it in the Android Monitor, and it shows the error that happened.

Related

Android Studio: Using ImageButton to start new activity crashes the app

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)

How to get rid of Bar in Android

I am new to developing but I have a problem
Here is the code of the stuff I believe you need to know
Main Activity
package com.mayubrand.basiccalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
private Button circumferenceB, areaB, diameterB;
private TextView resultTxt, radiusTxt, headingTxt;
private EditText radiusTF ;
public void onCreate(){
this.requestWindowFeature(Window.FEATURE_NO_TITLE);}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
//Buttons
circumferenceB = (Button) findViewById(R.id.circumferenceB);
areaB = (Button) findViewById(R.id.areaB);
diameterB = (Button) findViewById(R.id.diameterB);
//Text Field (Edit Text)
radiusTF = (EditText) findViewById(R.id.radiusTF);
//Text (Text View)
resultTxt = (TextView) findViewById(R.id.resultTxt);
radiusTxt =(TextView) findViewById(R.id.radiusTxt);
headingTxt = (TextView) findViewById(R.id.radiusTxt);
//==============
//Listeners
circumferenceB.setOnClickListener(this);
areaB.setOnClickListener(this);
diameterB.setOnClickListener(this);
}
public void onClick(View view) {
String radiusEntered = radiusTF.getText().toString();
double pi = Math.PI;
int two = 2;
if(radiusEntered.equals("")) {
radiusTF.setText("");
}else{
switch (view.getId()){
case R.id.circumferenceB:
double circumference = Integer.parseInt(radiusEntered) * pi * two;
resultTxt.setText(String.valueOf(circumference));
break;
case R.id.areaB:
double area = pi * Integer.parseInt(radiusEntered) *Integer.parseInt(radiusEntered);
resultTxt.setText(String.valueOf(area));
break;
case R.id.diameterB:
double diameter = Integer.parseInt(radiusEntered) * 2;
resultTxt.setText(String.valueOf(diameter));
break;
}
}
}
}
Main Activity XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="MayU Circle Calulator"
android:id="#+id/headingTxt"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Radius"
android:id="#+id/radiusTxt"
android:layout_below="#+id/headingTxt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="36dp"
android:textSize="23dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/radiusTF"
android:layout_alignBottom="#+id/radiusTxt"
android:layout_alignRight="#+id/headingTxt"
android:layout_alignEnd="#+id/headingTxt" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/radiusTxt"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Find Circumference"
android:id="#+id/circumferenceB"
android:layout_weight="0.08" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Find Area"
android:id="#+id/areaB"
android:layout_weight="0.08" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Find Diameter"
android:id="#+id/diameterB"
android:layout_gravity="center_horizontal"
android:layout_weight="0.08" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Result"
android:id="#+id/resultTxt"
android:layout_gravity="center_horizontal"
android:layout_weight="0.08"
android:textSize="40dp" />
</LinearLayout>
Android Manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Circle Calculator"
android:theme="#style/AppTheme"
>
<activity
android:name=".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>
When I'm in the layout area where you arrange all your buttons and text and any other component you have visually it looks fine, however as soon as I launch it in the emulator or on my phone there is a bar black/brown bar that just stays there. How do I remove that?
One other thing on the side, how do I change the default color of buttons
My guess is that you are referring to the action bar.
If that is the case in your styles.xml change the AppTheme theme to NoActionBar.
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
</style>
To change the default color of your buttons, you can use styles as well.

Android app crashes upon startup

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

Cannot run Eclipse / Android SDK Project - Errors of Unknown Origin

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

Android NullPointer Exception in Activity's onCreate

I have a simple application to multiply two numbers from text boxes and display result in third box. There is no any syntax errors in the code but when I am running an application i get this error: application has stopped unexpectedly.
Here is the java code:
package c.example.rectangle;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
EditText l = (EditText) findViewById(R.id.length);
EditText w = (EditText) findViewById(R.id.width);
TextView a = (TextView) findViewById(R.id.lblarea);
Button b = (Button) findViewById(R.id.calculate);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b.setOnClickListener(this);
}
public void onClick(View v) {
calculateRectangle(l.getText().toString(), w.getText().toString());
}
private void calculateRectangle(String clength, String cwidth){
int area = Integer.parseInt(clength)*Integer.parseInt(cwidth);
b.setText(String.valueOf(area));
}}
And here is my XML file.
<?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="#8B4513"
android:orientation="vertical" >
<TextView
android:id="#+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:gravity="center"
android:text="#string/rect"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:background="#2F4F4F"
android:gravity="center"
android:text="#string/cm"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/length"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="50dp"
android:background="#2F4F4F"
android:ems="10"
android:gravity="center"
android:inputType="number" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#2F4F4F"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="#string/breadth"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/width"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="33dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:inputType="number"
android:ems="10"
android:gravity="center"
>
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="#+id/calculate"
android:layout_width="fill_parent"
android:layout_marginLeft="100dip"
android:layout_marginRight="100dip"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:layout_marginTop="20dp" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#8B4513"
android:orientation="horizontal" >
<TextView
android:id="#+id/label4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:text="#string/area"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblarea"
android:layout_width="110dp"
android:layout_height="21dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="20dp"
android:background="#2F4F4F"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
Please help.
I know, that you got correct answer, but I just want to explain you why you need to write code as #Mr.Me says.
You describe your View elements in start of class, and trying to initialize them there. It is not correct. Because you have not attached layout file to activity at the moment when constructor will run your initialization of Views objects. As you can see, you are using findViewById() method, but before use it you should call setContentView().
For better understanding, read Activity Lifecycle, pay attention to rendering proccess.
Rearrange your code to look like this:
EditText l;
EditText w;
TextView a;
Button b;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (EditText) findViewById(R.id.length);
w = (EditText) findViewById(R.id.width);
a = (TextView) findViewById(R.id.lblarea);
b = (Button) findViewById(R.id.calculate);

Categories