My android calculator app seems to be failing at the moment, can anyone please fix it? I searched everywhere, but none of them seem to be working.
AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.koki.customizablecalculator"
<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"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java file:
package com.example.koki.customizablecalculator;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText Scr;
private float NumberBf;
private String Operation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Scr = (EditText) findViewById(R.id.button0);
int idList[] = {R.id.button0,R.id.button2,R.id.button3,R.id.button4,R.id.button5,
R.id.button6,R.id.button7,R.id.button8,R.id.buttonDot,R.id.buttonMul,
R.id.buttonDiv,R.id.buttonAdd,R.id.buttonSub,
};
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
content_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button"
android:layout_marginTop="83dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/button3"
android:layout_alignTop="#+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/button4"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="#+id/button5"
android:layout_alignTop="#+id/button4"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/button6"
android:layout_alignBottom="#+id/button5"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="#+id/button7"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="#+id/button8"
android:layout_alignTop="#+id/button7"
android:layout_alignLeft="#+id/button5"
android:layout_alignStart="#+id/button5" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AC"
android:id="#+id/buttonC"
android:background="#FF0000"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/buttonSub"
android:layout_toStartOf="#+id/buttonSub" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:id="#+id/settings"
android:background="#10E0F8"
android:allowUndo="false"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/buttonAdd"
android:layout_toEndOf="#+id/buttonAdd" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="#+id/button0"
android:layout_below="#+id/button8"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/buttonAdd"
android:layout_marginBottom="38dp"
android:background="#FE9A2E"
android:layout_above="#+id/buttonC"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/buttonSub"
android:background="#FE9A2E"
android:layout_alignTop="#+id/buttonAdd"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="#+id/buttonDiv"
android:background="#FE9A2E"
android:layout_alignTop="#+id/buttonMul"
android:layout_toLeftOf="#+id/button0"
android:layout_toStartOf="#+id/button0" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="#+id/button9"
android:layout_below="#+id/button6"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:id="#+id/buttonMul"
android:background="#FE9A2E"
android:layout_alignTop="#+id/buttonSub"
android:layout_toLeftOf="#+id/buttonC"
android:layout_toStartOf="#+id/buttonC" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:id="#+id/buttonDot"
android:layout_below="#+id/button9"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/button0"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="#+id/buttonEq"
android:layout_below="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#1B9863" />
</RelativeLayout>
Any help would be greatly appreciated! :)
You are calling an EditText that you don't have.
Either in your XML change your TextView to an EditText
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/button0"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
OR Change
private EditText Scr;
To
private TextView Scr;
And
Scr = (EditText) findViewById(R.id.button0);
To
Scr = (TextView) findViewById(R.id.button0);
Related
So, I am trying to do an application which gives a user the ability to keep track of the score and number of fouls of two different teams playing football
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:text="Team A"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="#+id/team_a_goal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<TextView
android:id="#+id/team_a_foul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:onClick="addGoalForTeamA"
android:text="GOAL" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:onClick="addFoulForTeamA"
android:text="FOUL" />
</LinearLayout>
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:text="Team B"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:id="#+id/team_b_goal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<TextView
android:id="#+id/team_b_foul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:gravity="center_horizontal"
android:padding="4dp"
android:paddingBottom="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="0"
android:textColor="#000000"
android:textSize="56sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:onClick="addGoalForTeamB"
android:text="GOAL" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="8dp"
android:onClick="addFoulForTeamB"
android:text="FOUL" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:onClick="Reset"
android:text="Reset" />
and java code
package com.example.android.scorekeeper;
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int goalTeamA = 0;
int foulTeamA = 0;
int goalTeamB = 0;
int foulTeamB = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addGoalForTeamA(View view) {
goalTeamA = +1;
displayGoalTeamA(goalTeamA);
}
public void addFoulForTeamA(View view) {
foulTeamA = +1;
displayFoulTeamA(foulTeamA);
}
public void addGoalForTeamB(View view) {
goalTeamB = +1;
displayGoalTeamB(goalTeamB);
}
public void addFoulForTeamB(View view) {
foulTeamB = +1;
displayFoulTeamB(foulTeamB);
}
public void Reset(View view) {
goalTeamA = 0;
foulTeamA = 0;
goalTeamB = 0;
foulTeamB = 0;
displayGoalTeamA(goalTeamA);
displayFoulTeamA(foulTeamA);
displayGoalTeamB(goalTeamB);
displayFoulTeamB(foulTeamB);
}
public void displayGoalTeamA(int score) {
TextView scoreView = findViewById(R.id.team_a_goal);
scoreView.setText(String.valueOf(score));
}
public void displayFoulTeamA(int score) {
TextView scoreView = findViewById(R.id.team_a_foul);
scoreView.setText(String.valueOf(score));
}
public void displayGoalTeamB(int score) {
TextView scoreView = findViewById(R.id.team_b_goal);
scoreView.setText(String.valueOf(score));
}
public void displayFoulTeamB(int score) {
TextView scoreView = findViewById(R.id.team_b_foul);
scoreView.setText(String.valueOf(score));
} }
The code seems fine to me and Android Studio doesn't report any errors but it did't launch on my android
make sure you have added your activity to your manifest file
<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
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 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);
}
});
}
}
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