New Binary XML file line #19: Error inflating class activity - java

This is my messenger app i'm working on. I've looked around and could not find a problem of the same brand. I disabled and re-enabled the messenger app, and since then it doesn't work. Please, help!
part of the create section
package com.danny4help.gchat;
import android.content.Intent;
import android.inputmethodservice.ExtractEditText;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class GChatCreateActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gchat_create);
}
public void send(View view){
ExtractEditText eet = (ExtractEditText) findViewById(R.id.eet);
String message = eet.getText().toString();
Intent intent = new Intent(this, GChatReceiveActivity.class);
intent.putExtra(GChatReceiveActivity.EXTRA_MESSAGE,message);
String title = getString(R.string.chooser);
// Intent chosenIntent = Intent.createChooser(intent,title);
startActivity(intent);
}
}
Part of receive section
package com.danny4help.aolcreate;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class AOLReceive extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "message";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aolreceive);
Intent intent = getIntent();
String messageSent = intent.getStringExtra(EXTRA_MESSAGE);
TextView messageView = (TextView) findViewById(R.id.message);
messageView.setText(messageSent);
}
}
create xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/send"
android:id="#+id/send"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="sendMessage"/>
<android.inputmethodservice.ExtractEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/eet"
android:layout_above="#+id/send"
android:layout_centerHorizontal="true"
android:layout_marginBottom="75dp"
android:ems="10"/>
<activity android:name="SendActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
<data android:mimeType="image/*"/>
</intent-filter>
</activity>
</RelativeLayout>
receive 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: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.danny4help.aolcreate.AOLReceive">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/message"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

As I catch the error is Inside your Button you have define onClick method name is sendMessage and in java code you are create method send which is not the same as sendMessage. So change your java code to below way.
public void sendMessage(View view){
ExtractEditText eet = (ExtractEditText) findViewById(R.id.eet);
String message = eet.getText().toString();
Intent intent = new Intent(this, GChatReceiveActivity.class);
intent.putExtra(GChatReceiveActivity.EXTRA_MESSAGE,message);
String title = getString(R.string.chooser);
// Intent chosenIntent = Intent.createChooser(intent,title);
startActivity(intent);
}
In Short :
you have to change the method Name as you declare in the xml.

Related

How can I make one button display the contents of two text boxes on the next screen?

I am very new to coding, this being only my 2nd semester in community college and therefore I have a lot to learn.
In my Android Development course, I have been tasked to use the program I've created from the Android Development lesson here - https://developer.android.com/training/basics/firstapp/starting-activity#java - as the basis for a new program which instead has 2 textboxes and which displays the content the user inputs into those textboxes, onto the display screen, by clicking the 1 send button.
I am stuck.
I've been at this since last Tuesday when the challenge was issued and I've been Google searching, DuckDuckGo searching, searching through stackoverflow, searching through here - https://developer.android.com/docs/ - and I've tried many different approaches but I can't seem to find out how to accomplish this.
!!For the record: I'm not asking someone to code the program/app for me. I'm simply trying to understand how to get this 1 button to send 2 textbox contents to the display - I want to learn!!
//* activity_main.xml */
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/ColorActivity"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:ems="10"
android:gravity="center"
android:layout_toRightOf="#+id/Number"
android:hint="edit_name_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_below="#+id/Name"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_centerHorizontal="true"
android:text="button_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Number" />
<EditText
android:id="#+id/Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:ems="10"
android:gravity="center"
android:hint="edit_phone_message"
android:inputType="phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Name" />
</RelativeLayout>
//* DisplayMessge.java */
package com.example.testapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.example.testapp.R;
public class DisplayMessage extends Activity {
private TextView name;
private TextView number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_activity);
name = findViewById(R.id.NameText);
number = findViewById(R.id.NumberText);
Intent getText = getIntent();
String TheName = getText.getStringExtra("Name");
String TheNumber = getText.getStringExtra("Number");
name.setText(TheName);
number.setText(TheNumber);
}
}
//* MainActivity.java */
package com.example.testapp;
import android.content.Intent;
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.Toast;
import com.example.testapp.DisplayMessage;
import com.example.testapp.R;
public class MainActivity extends AppCompatActivity {
Button button;
private EditText name;
private EditText number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialization of the EditText and the Button
name = (EditText) findViewById(R.id.Name);
number = (EditText) findViewById(R.id.Number);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), DisplayMessage.class);
String mName = name.getText().toString();
String mNumber = number.getText().toString();
//Checking if the Entries are empty
if (mName != null && mNumber != null) {
intent.putExtra("Name", mName);
intent.putExtra("Number", mNumber);
startActivity(intent);
finish();
} else {
Toast.makeText(getApplicationContext(), "Text Entries Missing", Toast.LENGTH_SHORT).show();
}
}
});
}
}
//* AndroidManifest.xml */
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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=".DisplayMessage">
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
</intent-filter>
</activity>
</application>
</manifest>
//* display_activity.xml */
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:layout_gravity="center"
android:id="#+id/NameText"
android:layout_weight="1"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/NumberText"
android:layout_gravity="center"
android:textSize="18sp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
You have to implement the onClick function and send the intent from there.
public class MainActivity extends AppCompatActivity {
Button button;
private EditText name;
private EditText number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialization of the EditText and the Button
name = (EditText) findViewById(R.id.Name);
number = (EditText) findViewById(R.id.Number);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), DisplayMessage.class);
String mName = name.getText().toString();
String mNumber = number.getText().toString();
//Checking if the Entries are empty
if(mName!=null&&mNumber!=null) {
intent.putExtra("Name", mName);
intent.putExtra("Number", mNumber);
startActivity(intent);
finish();
}else{
Toast.makeText(getApplicationContext(),"Text Entries Missing",Toast.LENGTH_SHORT).show();
}
}
});
and the Display Class:
public class DisplayMessage extends Activity {
private TextView name;
private TextView number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_activity);
name = findViewById(R.id.NameText);
number = findViewById(R.id.NumberText);
Intent getText = getIntent();
String TheName =getText.getStringExtra("Name");
String TheNumber = getText.getStringExtra("Number");
name.setText(TheName);
number.setText(TheNumber);
}
Also don't forget to add your displayActivity to the AndroidManifest.xml
<activity android:name=".DisplayMessage">
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
</intent-filter>
</activity>
Now you have to create a second user interface for the DisplayMessageActivity go to res/layout, right click on the layout folder and create a new layout named display_activity. This is my code for the display_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:layout_gravity="center"
android:id="#+id/NameText"
android:layout_weight="1"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/NumberText"
android:layout_gravity="center"
android:textSize="18sp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Lastly this is the activity_main layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/ColorActivity"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:ems="10"
android:gravity="center"
android:layout_toRightOf="#+id/Number"
android:hint="edit_name_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_below="#+id/Name"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_centerHorizontal="true"
android:text="button_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Number" />
<EditText
android:id="#+id/Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:ems="10"
android:gravity="center"
android:hint="edit_phone_message"
android:inputType="phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Name" />
</RelativeLayout>
Just reduce your two functions (snedNumber and sendName) to only one function. Inside this function you get the text of both textfields and put them into your intentextra. then simply start your new activity
Do it like this
public void sendInfo(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText name = (EditText) findViewById(R.id.Name);
String message = name.getText().toString();
EditText number = (EditText) findViewById(R.id.Number);
String message2 = number.getText().toString();
intent.putExtra("firstString", message);
intent.putExtra("secondString", message2);
startActivity(intent);
}
And in your DisplayMessageActivity class onCreate method get it like
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
Bundle extra = getIntent().getExtras();
if (extra != null){
String message = extra.getString("firstString");
String message2 = extra.getString("secondString");
}
}

passing user input (EditText) between activities, and then using that in formula

I want from user to input his name in one EditText widget, then to input his weight in other EditText widget, and after that to proceed that data from one activity to another one.
Weight input should later be included in one formula, and name input should be settled in TextView widget with result of the formula.
But, when i start my app, at the end Android Studio shows me NullPointerException. I suppose that err is somewhere in data sharing between activities.
So, here is the code. If you can, pls help me :)
ActivityOne (name of activity is InformacijeM) XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_informacije_m"
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="com.example.alkometar.InformacijeM"
android:background="#drawable/activitybackground">
<TextView
android:text="Ukucaj svoje ime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:textSize="25dp"
android:textColor="#android:color/black"
android:textStyle="bold"
android:id="#+id/UkucajIme" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/strelamanja"
android:id="#+id/strelica"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/ImeInput"
android:layout_marginTop="12dp"
android:layout_below="#+id/UkucajIme"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/MasaInput"
android:layout_marginBottom="53dp"
android:layout_above="#+id/strelica"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<TextView
android:text="Ukucaj svoju masu u kg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/UkucajMasu"
android:textSize="25dp"
android:textColor="#android:color/black"
android:textStyle="bold"
android:layout_marginBottom="30dp"
android:layout_above="#+id/MasaInput"
android:layout_centerHorizontal="true" />
</RelativeLayout>
ActivityOne (InformacijeM) Java file:
package com.example.alkometar;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class InformacijeM extends AppCompatActivity {
EditText ImeTxt, MasaTxt ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_informacije_m);
// INICIRANJE VREDNOSTI
EditText ImeTxt = (EditText)findViewById(R.id.ImeInput);
final String str1 = ImeTxt.getText().toString();
EditText MasaTxt = (EditText)findViewById(R.id.MasaInput);
final String str2 = MasaTxt.getText().toString();
ImageButton btn = (ImageButton)findViewById(R.id.strelica);
// PREBACIVANJE AKTIVITIJA
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(InformacijeM.this, RezultatM.class);
intent1.putExtra("keyIme", str1);
intent1.putExtra("keyMasa", str2);
Intent intent = new Intent(InformacijeM.this, StanjeM.class);
startActivity(intent);
}
});
}}
ActivityTwo (Name of activity is RezultatM) XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_rezultat_m"
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="com.example.alkometar.RezultatM"
android:background="#drawable/activitybackground">
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="97dp"
android:id="#+id/rezultatTekst" />
<Button
android:text="Rezultat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:id="#+id/button" />
</RelativeLayout>
ActivityTwo (RezultatM) Java file:
package com.example.alkometar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class RezultatM extends AppCompatActivity {
TextView rezultat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rezultat_m);
rezultat = (TextView) findViewById(R.id.rezultatTekst);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Bundle extras = getIntent().getExtras();
{
String str1 = extras.getString("keyIme");
String str2 = extras.getString("keyMasa");
double masad = Double.valueOf(str2);
double nekibroj = 0.8;
double result = masad * nekibroj;
rezultat.setText(str1 + "Nivo alkohola u tvojoj krvi je:" + result);
}
};
});
}}
Strings.xml file:
<resources>
<string name="app_name">Alkometar</string>
<string name="ImeInput">ImeInput</string>
<string name="MasaInput">MasaInput</string>
</resources>
AndroidManifest XML file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alkometar">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Pol">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".InformacijeM" />
<activity android:name=".StanjeM" />
<activity android:name=".InformacijeZ" />
<activity android:name=".StanjeZ" />
<activity android:name=".PijemM" />
<activity android:name=".RezultatM"></activity>
</application>
</manifest>
And here is the exception in logCat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.alkometar, PID: 30732
java.lang.NullPointerException
at com.example.alkometar.RezultatM$1.onClick(RezultatM.java:28)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Thank you very much once again! :)))
Change your Activity Two class to:
public class RezultatM extends AppCompatActivity {
TextView rezultat;
String str1,str2;
double masad;
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rezultat_m);
rezultat = (TextView) findViewById(R.id.rezultatTekst);
Button button = (Button) findViewById(R.id.button);
//Intent intent = getIntent();
//str1 = intent.getStringExtra("keyIme");
//str2 = intent.getStringExtra("keyMasa");
//getting values from shared preferences
SharedPreferences shared = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
str1=shared.getString("keyIme", "");
str2=shared.getString("keyMasa", "");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(str2!=null){
masad = Double.parseDouble(str2);
}
double nekibroj = 0.8;
double result = masad * nekibroj;
rezultat.setText(str1 + "Nivo alkohola u tvojoj krvi je:" + result);
};
});
}}
Also in your Activity one you are starting wrong intent
Intent intent1 = new Intent(InformacijeM.this, RezultatM.class);
intent1.putExtra("keyIme", str1);
intent1.putExtra("keyMasa", str2);
Intent intent = new Intent(InformacijeM.this, StanjeM.class);
// startActivity(intent); //here you are starting another class..check it
//change it to
startActivity(intent1);
change your InformacijeM.java class:
public class InformacijeM extends AppCompatActivity {
EditText ImeTxt, MasaTxt ;
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_informacije_m);
sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// INICIRANJE VREDNOSTI
EditText ImeTxt = (EditText)findViewById(R.id.ImeInput);
final String str1 = ImeTxt.getText().toString();
EditText MasaTxt = (EditText)findViewById(R.id.MasaInput);
final String str2 = MasaTxt.getText().toString();
save_data(str1,str2);
ImageButton btn = (ImageButton)findViewById(R.id.strelica);
// PREBACIVANJE AKTIVITIJA
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(InformacijeM.this, RezultatM.class);
intent1.putExtra("keyIme", str1);
intent1.putExtra("keyMasa", str2);
Intent intent = new Intent(InformacijeM.this, StanjeM.class);
startActivity(intent);
}
});
}
public void save_data(String s1,String s2){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("keyIme", s1);
editor.putString("keyMasa", s2);
editor.commit();
}
}

Unfortunately app stopped work

As I press the "Start Activity" button in my app it won't working and forced to stop its working.
Here is 1st xml file (get.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/etget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Enter Your Gender" />
<Button
android:id="#+id/bget1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/etget"
android:text="Start Activity" />
<Button
android:id="#+id/bget2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/etget"
android:layout_toLeftOf="#id/bget1"
android:text="Start Activity for Result " />
<TextView
android:id="#+id/tvget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/bget2"
android:text="TextView" />
</RelativeLayout>
Here is java file, Get.java:
package example.katta;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Get extends Activity implements OnClickListener {
Button bg1, bg2;
TextView tv;
TextView etg;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.get);
etg = (TextView) findViewById(R.id.etget);
bg1 = (Button) findViewById(R.id.bget1);
bg2 = (Button) findViewById(R.id.bget2);
tv = (TextView) findViewById(R.id.tvget);
bg1.setOnClickListener(this);
bg2.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.bget1:
String bread = etg.getText().toString();
Bundle basket = new Bundle();
basket.putString("key", bread);
Intent a = new Intent(Get.this, Send.class);
a.putExtras(basket);
startActivity(a);
break;
case R.id.bget2:
break;
}
}
}
Here is 2nd xml file(send.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/etsend"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TextView>
<RadioGroup
android:id="#+id/rgsend"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rb1send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="#+id/rb2send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<TextView
android:id="#+id/tvsend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/buttonsend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
Here is java file(Send.java) :
package example.katta;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class Send extends Activity implements OnClickListener, OnCheckedChangeListener {
Button bts;
TextView tvs,ets;
RadioGroup selection;
String gotbread;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
Initialize();
Bundle gotbasket = getIntent().getExtras();
gotbread = gotbasket.getString("key");
tvs.setText(gotbread);
}
private void Initialize() {
// TODO Auto-generated method stub
ets = (TextView) findViewById(R.id.etsend);
bts = (Button) findViewById(R.id.buttonsend);
tvs = (TextView) findViewById(R.id.tvsend);
selection = (RadioGroup) findViewById(R.id.rgsend);
bts.setOnClickListener(this);
selection.setOnCheckedChangeListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case R.id.rb1send:
break;
case R.id.rb2send:
break;
}
}
}
The error is:
android.content.ActivityNotFoundException: Unable to find explicit activity class {example.katta/example.katta.Send}; have you declared this activity in your AndroidManifest.xml?
The solution is to add Send Activity in your AndroidManifest.xml:
<activity
android:name=".Send" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You should declarate every activities in your manifest, also I think you can create a better code.
I'll give some recommendations to help you.
1- You can explicit in naming their views.
<TextView
android:id="#+id/txt_view_send"
... >
</TextView>
2- Use camelNotation java code
Bundle gotBasket ...
Future large projects is of great help!
3- Use match_parent un your views
FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent.
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
4- Bundle is used only when you have many arguments. You can do something like this in your example.
//you can add a variable to get editText value or pass direct the value in to putExtras()
String bread = etg.getText().toString();
Intent a = new Intent(Get.this, Send.class);
a.putExtras("key", bread);
startActivity(a);
http://developer.android.com/reference/android/content/Intent.html
4-validating can help avoid exceptions.
//example
if(getIntent().getExtras() != null){
tvs.setText(getIntent().getExtras().getString("key"));
}
//your example
Bundle gotbasket = getIntent().getExtras();
if(gotbasket != null){
gotbread = gotbasket.getString("key");
if(gotbread != null{
tvs.setText(gotbread);
}
It can produce NullPointerException.
I hope it helps you!!
Cheers
programming continues!

app crash when i click sign in buttom

here is my my signupactivity & main activity and their xml
public class SignupActivity extends AppCompatActivity {
protected EditText mUsername;
protected EditText mPassword;
protected EditText mEmail;
protected Button nbutton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
mUsername = (EditText) findViewById(R.id.usernamefield);
mPassword = (EditText) findViewById(R.id.passwordtextfield);
mEmail = (EditText) findViewById(R.id.emailtextfield);
nbutton = (Button) findViewById(R.id.signbutton);
nbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
// Retrieve the text entered from the EditText
String usernametxt = mUsername.getText().toString();
String password = mPassword.getText().toString();
String email= mEmail.getText().toString();
// Force user to fill up the form
if (usernametxt.equals("") && password.equals("")) {
Toast.makeText(getApplicationContext(),
"Please complete the sign up form",
Toast.LENGTH_LONG).show();
} else {
// Save new user data into Parse.com Data Storage
ParseUser newUser = new ParseUser();
newUser.setUsername(usernametxt);
newUser.setPassword(password);
newUser.setEmail(email);
newUser.signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
//SUCESS
if (e!= null){
AlertDialog.Builder builder=new AlertDialog.Builder(SignupActivity.this);
builder.setMessage(e.getMessage()).setTitle(R.string.signup_error_title).setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
else {
Intent intent= new Intent(SignupActivity.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
});
}
}
});
}
it was suppose to get the information abt users and go to main activity from signupactivity.the app crashes when it get in put from users and press sign in...
here is the 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="com.josephvarkey996gmail.test1.SignupActivity" >
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/usernamefield"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="89dp"
android:hint="#string/username_hint"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/passwordtextfield"
android:layout_centerHorizontal="true"
android:layout_below="#+id/usernamefield"
android:hint="#string/password_hint"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/emailtextfield"
android:hint="#string/email_hint"
android:layout_below="#+id/passwordtextfield"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/signup_hint"
android:id="#+id/signbutton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
here is the main activity code
package com.josephvarkey996gmail.test1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseUser;
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.enableLocalDatastore(this);
Parse.initialize(this, "XFigOgliUYKi9h5RanLfLkuKU14AG2f2NFQXADKI", "sMQBPkhZV5b74MEGpP3PdQ6eePWo5Y9O8lRcQvBP");
ParseAnalytics.trackAppOpenedInBackground(getIntent());
ParseUser currentUser = ParseUser.getCurrentUser();
if(currentUser==null) {
navigatetologin();
}
if(currentUser!=null)
{
Log.i(TAG ,currentUser.getUsername());
}
// Enable Local Datastore.
}
#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;
}
private void navigatetologin() {
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
mainactivity xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity" >
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
android manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.josephvarkey996gmail.test1" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat" >
<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>
<activity
android:name=".SignupActivity"
android:label="#string/title_activity_signup" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
</application>
</manifest>
We cannot help you until u post your logcat. As I don't have enough reputation to comment, I am writing this in answer.
Most common reason I saw people getting this error is:
They copy -paste this line from another class
R.id.usernamefield
So it will try to run the usernamefield of their other class which is not present here(note: eclipse will automatically import so they didn't get compilation errors). So to resolve your error you should delete the import and write proper R.id.xyz
But this is only shot in the dark until we see your logcat.

Unknown Crashing

I am currently trying to develop a simple feature for switching from one page to another but every time I launch the app it crashes.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Screen One......"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/scan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me to another screen" />
</LinearLayout>
MainActivity.java
package com.example.mdpmk1;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.scan);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, ScanScreen.class);
startActivity(intent);
}
});
}
}
ScanScreen.java
package com.example.mdpmk1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class ScanScreen extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scan_screen);
}
}
scan_screen.xml
<LinearLayout 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:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You have done it!!"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Im quite new to this sort of stuff but i am doing an app for my major at school and would like to have it actually working. Any help would be lovely. Thanks
You forgot to write following statement in your MainActivity's onCreate() method,
setContentView(R.layout.activity_main);
Write here,
public class MainActivity extends Activity
{
Button button;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // You forgot this line
addListenerOnButton();
}
Also change following line of code,
public void onClick(View arg0)
{
Intent intent = new Intent( MainActivity.this, ScanScreen.class ); // Change here
startActivity(intent);
}
Try editing intent statement in class MainActivity to:
Intent intent = new Intent(MainActivity.this, ScanScreen.class);
startActivity(intent);
You must also add:
setContentView(R.layout.activity_main);
in your ActivityMain class onCreate function to set a view.
setContentView(R.your_layout) doesnt exists in your MainActivity's onCreate(). Try providing your activity a layout.
In your MainActivity onCreate you need to add
setContentView(R.layout.activity_main);
First of all, add SetContentView(R.layout.activity_main) in your MainActivity.OnCreate method befor call to addListenerOnButton.
then be sure that you add your activities entries in your manifest.xml and make MainActivity as your Main Application Activity, like below:
<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=".ScanScreen"/>

Categories