i'm making a kind of a form app for my end work for school,
but as you can see in the picture, it ain't going verry well.
I'm trying to save all the date from the textviews into a toast message so i can later on put it into a text file.
But now the Toast file says that all my data is "false"
please help me..
http://prntscr.com/7b4how
package com.jan.energyservice;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintStream;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Nieuw_huis1 extends ActionBarActivity {
EditText tekst;
String s = "jantest";//AlgemeneGegevens.getFilename();
String uitvoer;
private String data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nieuw_huis1);
setupVolgendePagina();
}
private void SlaOp(String String, String String2, String String3, String String4, String String5,
String String6, String String7, String String8, int rdbgrouphuisstatus, String String10,
String String11, String String12, String String13, String String14, String String15,
String String16, String String17, String Titel) {
try{
uitvoer += Titel + "\n";
save(String,String10);
save(String2,String11);
save(String3,String12);
save(String4,String13);
save(String6,String15);
save(String7,String16);
save(String8,String17);
save(rdbgrouphuisstatus);
toonresultaat();
}
catch (Exception e) {
}
}
private void toonresultaat() {
// TODO Auto-generated method stub
Toast.makeText(this, uitvoer,Toast.LENGTH_LONG ).show();
}
private void save(int rdbgrouphuisstatus) {
// TODO Auto-generated method stub
}
private void save(String invoerTekst, String label) {
try {
uitvoer += label + "\n" + invoerTekst;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void setupVolgendePagina() {
Button Movebutton = (Button) findViewById(R.id.btnVolgendePagina1);
// 2 click listener to run code
Movebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent =(new Intent(Nieuw_huis1.this,Nieuw_huis2.class));
SlaOp(getText(R.id.txtProjectNaam).toString(),getText(R.id.txtKenmerk).toString(),getText(R.id.txtAdres).toString(),
getText(R.id.txtGemeente).toString(),getText(R.id.txtKlantNaam).toString(),getText(R.id.txtDatumBezoek).toString(),
getText(R.id.txtNaamAdviseur).toString(),getText(R.id.txtHuisnummer).toString(),R.id.rdbgroupHuisStatus,
getText(R.id.tvProjectnaam).toString(),getText(R.id.tvKenmerk).toString(),getText(R.id.tvAdres).toString(),
getText(R.id.tvGemeente).toString(),getText(R.id.tvKlantnaam).toString(),getText(R.id.tvDatumBezoek).toString(),
getText(R.id.tvNaamAdviseur).toString(),getText(R.id.tvHuisnummer).toString(),getText(R.id.tvAdresgegevens).toString());
startActivity(intent);
}
});
}
public void save(View view){
//data = tekst;
try {
FileOutputStream fOut = openFileOutput(s,Context.MODE_PRIVATE);
new PrintStream(fOut).println("Naam van eerste ding");
fOut.write(data.getBytes());
fOut.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.nieuw_huis1, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is my xml file
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/TableLayout1"
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.jan.energyservice.Nieuw_huis1" >
<TextView
android:id="#+id/tvAdresgegevens"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/adresgegevens"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tvProjectnaam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/projectnaam"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtProjectNaam"
android:layout_width="340dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/tvKenmerk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/kenmerk"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtKenmerk"
android:layout_width="365dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
<TextView
android:id="#+id/tvAdres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/adres"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtAdres"
android:layout_width="370dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text|textPersonName" />
<TextView
android:id="#+id/tvGemeente"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gemeente"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtGemeente"
android:layout_width="356dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date|text" />
<TextView
android:id="#+id/tvKlantnaam"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/klantnaam"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtKlantNaam"
android:layout_width="356dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="#+id/tvDatumBezoek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/datumbezoek"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtDatumBezoek"
android:layout_width="357dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date" />
<TextView
android:id="#+id/tvNaamAdviseur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/naamadviseur"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/txtNaamAdviseur"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="#+id/tvHuisnummer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Huisnummer" />
<EditText
android:id="#+id/txtHuisnummer"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<RadioGroup
android:id="#+id/rdbgroupHuisStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rdbVerhuur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/Verhuur" />
<RadioButton
android:id="#+id/rdbVerkoop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/Verkoop" />
<RadioButton
android:id="#+id/rdbImmo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/Immo" />
</RadioGroup>
<Button
android:id="#+id/btnVolgendePagina1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/VolgendePagina" />
</TableLayout>
this is what i edited after your responses.. but still not working
public void onClick(View v) {
Intent intent =(new Intent(Nieuw_huis1.this,Nieuw_huis2.class));
EditText txtProjectnaam = (EditText) findViewById(R.id.txtProjectNaam);
EditText txtKenmerk = (EditText) findViewById(R.id.txtKenmerk);
EditText txtAdres = (EditText) findViewById(R.id.txtAdres);
EditText txtNaamAdviseur = (EditText) findViewById(R.id.txtNaamAdviseur);
EditText txtHuisnummer = (EditText) findViewById(R.id.txtHuisnummer);
EditText txtGemeente = (EditText) findViewById(R.id.txtGemeente);
EditText txtKlantnaam = (EditText) findViewById(R.id.txtKlantNaam);
EditText txtDatumBezoek = (EditText) findViewById(R.id.txtDatumBezoek);
TextView tvProjectnaam = (TextView) findViewById(com.jan.energyservice.R.string.projectnaam);
TextView tvKenmerk = (TextView) findViewById(com.jan.energyservice.R.string.kenmerk);
TextView tvAdres = (TextView) findViewById(com.jan.energyservice.R.string.projectnaam);
TextView tvGemeente = (TextView) findViewById(com.jan.energyservice.R.string.gemeente);
TextView tvKlantnaam = (TextView) findViewById(com.jan.energyservice.R.string.klantnaam);
TextView tvDatumBezoek = (TextView) findViewById(com.jan.energyservice.R.string.datumbezoek);
TextView tvNaamAdviseur = (TextView) findViewById(com.jan.energyservice.R.string.naamadviseur);
TextView tvHuisnummer = (TextView) findViewById(com.jan.energyservice.R.string.Huisnummer);
TextView tvAdresGegevens = (TextView) findViewById(com.jan.energyservice.R.string.adresgegevens);
SlaOp(txtProjectnaam.getText().toString(),txtKenmerk.getText().toString(),txtAdres.getText().toString(),
txtGemeente.getText().toString(),txtKlantnaam.getText().toString(),txtDatumBezoek.getText().toString(),
txtNaamAdviseur.getText().toString(),txtHuisnummer.getText().toString(),R.id.rdbgroupHuisStatus,
tvProjectnaam.getText().toString(),tvKenmerk.getText().toString(),tvAdres.getText().toString(),
tvGemeente.getText().toString(),tvKlantnaam.getText().toString(), tvDatumBezoek.getText().toString(),
tvNaamAdviseur.getText().toString(),tvHuisnummer.getText().toString(),
tvAdresGegevens.getText().toString());
startActivity(intent);
}
});
05-30 19:26:34.383: E/AndroidRuntime(898): FATAL EXCEPTION: main
05-30 19:26:34.383: E/AndroidRuntime(898): java.lang.NullPointerException
05-30 19:26:34.383: E/AndroidRuntime(898): at com.jan.energyservice.Nieuw_huis1$1.onClick(Nieuw_huis1.java:115)
05-30 19:26:34.383: E/AndroidRuntime(898): at android.view.View.performClick(View.java:4204)
05-30 19:26:34.383: E/AndroidRuntime(898): at android.view.View$PerformClick.run(View.java:17355)
05-30 19:26:34.383: E/AndroidRuntime(898): at android.os.Handler.handleCallback(Handler.java:725)
05-30 19:26:34.383: E/AndroidRuntime(898): at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 19:26:34.383: E/AndroidRuntime(898): at android.os.Looper.loop(Looper.java:137)
05-30 19:26:34.383: E/AndroidRuntime(898): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-30 19:26:34.383: E/AndroidRuntime(898): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 19:26:34.383: E/AndroidRuntime(898): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 19:26:34.383: E/AndroidRuntime(898): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-30 19:26:34.383: E/AndroidRuntime(898): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-30 19:26:34.383: E/AndroidRuntime(898): at dalvik.system.NativeStart.main(Native Method)
05-30 19:26:37.943: E/Trace(3262): error opening trace file: No such file or directory (2)
It says something about line 115 which is in SlaOp from the moment i start with the textviews
Inject your view before you use it.
Do this with all view you wanna to use/modify:
EditText txtKenmerk = (EditText) findViewById(R.id.txtKenmerk)
Then:
txtKenmerk.getText().toString();
I check your latest code and you have a problem with your onClick method starting from your TextView's allocations.
It's an error to cast findViewById(com.jan.energyservice.R.string.projectnaam)
to a TextView since it's not a TextView.
What you should really do is (TextView) findViewById(R.id.projectnaam). that what you configured in your activity xml code. (use R.id for all your objects)
R.string is a special location (string.sml) were you can save all your strings.
The null exception that you got is because you tried to get a view that doesn't exist and therefore you textview was null. afterwards you tried to read from a null function and the exception popped.
Related
I'm new to Android developing. This is an assignment which was given to me. I have to convert the value and the converted value must be shown in the next activity. But when I press the submit button its crashes.I don;t know what is the problem. Is there a problem with my RadioButtons. Please help me...
This is the second activity(ConvertActivity)
package com.gihan.temperatureconverter;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
public class ConvertActivity extends ActionBarActivity {
//RadioButton cel=(RadioButton) findViewById(R.id.rCel);
//RadioButton fah=(RadioButton) findViewById(R.id.rFah);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_convert);
}
#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_convert, 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);
}
public void calculation(View view) {
EditText val=(EditText) findViewById(R.id.editText);
int value = Integer.valueOf(val.getText().toString()).intValue();
int ans=0;
int fahval=0;
int celval=0;
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.rCel:
if (checked){
ans=((value-32)*5/9);
fahval=value;
celval=ans;}
break;
case R.id.rFah:
if (checked){
ans=((value*9)/5)+32;
celval=value;
fahval=ans;}
break;
}
/*if (cel.isChecked()){
ans=((value-32)*5/9);
fahval=value;
celval=ans;
}
if (fah.isChecked()){
ans=((value*9)/5)+32;
celval=value;
fahval=ans;
}*/
Intent intent = new Intent(ConvertActivity.this, LastActivity.class);
intent.putExtra("celval", getText(celval));
intent.putExtra("fahval", getText(fahval));
ConvertActivity.this.startActivity(intent);
}
}
This is the 2nd XML(activity_convert)
<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.gihan.temperatureconverter.ConvertActivity"
android:background="#drawable/ic_background">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp"
android:hint="Enter Value"
android:textSize="20dp"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioGroup"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true">
<RadioButton
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="To Celsius"
android:id="#+id/rCel"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:checked="false"
android:gravity="fill_horizontal"
android:textColor="#ffc80301"
android:textSize="25sp"/>
<RadioButton
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="To Fahrenheit"
android:id="#+id/rFah"
android:checked="false"
android:gravity="fill_horizontal"
android:textColor="#ffc80301"
android:textSize="25sp"
android:layout_below="#+id/rCel"
android:layout_alignLeft="#+id/rCel"
android:layout_alignStart="#+id/rCel" />
</RadioGroup>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/sub"
android:onClick="calculation"
android:layout_below="#+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp" />
</RelativeLayout>
This is the 3rd activity(LastActivity)
package com.gihan.temperatureconverter;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class LastActivity extends ActionBarActivity {
TextView vFah=(TextView)findViewById(R.id.vFah);
TextView vCel=(TextView)findViewById(R.id.vCel);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last);//use whatever layout you want.
Bundle extras = getIntent().getExtras();
int celval=extras.getInt("celval");
int fahval=extras.getInt("fahval");
//String cel=String.valueOf(celval);
//String fah=String.valueOf(fahval);
vCel.setText(Integer.toString(celval));
vFah.setText(Integer.toString(fahval));
}
#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_convert, 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);
}
public void again(View view) {
Intent intent = new Intent(LastActivity.this, ConvertActivity.class);
LastActivity.this.startActivity(intent);
}
public void home(View view) {
Intent intent = new Intent(LastActivity.this, MainActivity.class);
LastActivity.this.startActivity(intent);
}
}
This is the 3rd XML(activity_last)
<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="#drawable/ic_background"
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.gihan.temperatureconverter.LastActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Successfully Converted"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textColor="#ffff0004"
android:textSize="30sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:gravity="center_horizontal"
android:textSize="25sp"
android:onClick="home"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Calculate again"
android:id="#+id/button3"
android:layout_marginBottom="45dp"
android:gravity="center_horizontal"
android:layout_above="#+id/button2"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:onClick="again"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Fahrenheit"
android:id="#+id/textView3"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textColor="#ff0010ff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Celsius"
android:id="#+id/textView4"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp"
android:textColor="#ff0010ff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="20sp"
android:id="#+id/vFah"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:textColor="#ff9300ff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="20sp"
android:id="#+id/vCel"
android:layout_below="#+id/textView4"
android:layout_centerHorizontal="true"
android:textColor="#ff9300ff"/>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gihan.temperatureconverter" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ConvertActivity"
android:label="#string/app_name">
</activity>
<activity
android:name=".LastActivity"
android:label="#string/app_name">
</activity>
</application>
</manifest>
Logcat
--------- beginning of crash
06-09 21:05:58.413 2381-2381/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.gihan.temperatureconverter, PID: 2381
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.RadioButton
at com.gihan.temperatureconverter.ConvertActivity.calculation(ConvertActivity.java:56)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Some Comments were some ways I tried. It's also failed.
And also this is Android Studio Project.
Your Error Solved Check this code
mainactivity.java
public class MainActivity extends ActionBarActivity {
EditText val;
public static int ans;
public static String fahval="";
public static String celval="";
RadioButton Box1,Box2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
val =(EditText) findViewById(R.id.editText);
}
public void calculation(View v)
{
//get value from edit text box and convert into double
double a=Double.parseDouble(String.valueOf(val.getText()));
Box1 = (RadioButton) findViewById(R.id.rCel);
Box2 = (RadioButton) findViewById(R.id.rFah);
//check which radio button is checked
if(Box1.isChecked())
{
//display conversion
celval = String.valueOf(f2c(a));
// Toast.makeText(getApplicationContext(), celval,Toast.LENGTH_LONG).show();
Box1.setChecked(true);
}
else
{
fahval = String.valueOf(c2f(a));
// Toast.makeText(getApplicationContext(), fahval,Toast.LENGTH_LONG).show();
Box2.setChecked(true);
}
Intent intent = new Intent(this, LastActivity.class);
intent.putExtra("celval", celval);
intent.putExtra("fahval", fahval);
startActivity(intent);
}
//Celcius to Fahrenhiet method
private double c2f(double c)
{
return (c*9)/5+32;
}
//Fahrenhiet to Celcius method
private double f2c(double f)
{
return (f-32)*5/9;
}
}
LastActivity.java
public class LastActivity extends ActionBarActivity {
TextView vFah;
TextView vCel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.last);//use whatever layout you want.
vFah=(TextView)findViewById(R.id.vFah);
vCel=(TextView)findViewById(R.id.vCel);
Bundle extras = getIntent().getExtras();
String celval=extras.getString("celval");
String fahval=extras.getString("fahval");
//String cel=String.valueOf(celval);
//String fah=String.valueOf(fahval);
vCel.setText(celval);
vFah.setText(fahval);
}
public void again(View view) {
Intent intent = new Intent(LastActivity.this, MainActivity.class);
LastActivity.this.startActivity(intent);
}
public void home(View view) {
Intent intent = new Intent(LastActivity.this, MainActivity.class);
LastActivity.this.startActivity(intent);
}
}
I was building some apps this week, and couldn't figure out why I couldn't navigate from the Main Activity to a second activity and from there to a 3rd level activity (that would pop to 2nd activity), and app kept crashing when I added the 3rd. Finally looked in the Logcat to figure out what's wrong, and then read "Cannot Navigate from 'second activity'.... This View will not accept Navigation". Searched through code to find errors, but Never thought to look there before.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I am writing an android application that consists of a alertdialog and i am getting a null pointer exception.I cant find any exceptions in my code but it was showing null pointer exception please tell me where i did the mistake
Here i used the library downloaded from here for material design for buttons here.this is my activity
package com.developer.milanandroid;
import java.io.IOException;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.gc.materialdesign.views.ButtonFloatSmall;
import com.gc.materialdesign.widgets.SnackBar;
import com.milan.paperbuttons.signoutoptionsbuttons;
public class Modes extends Activity {
public static final String PREFS_NAME = "LoginPreferences";
Button auto,manual,adminsettings;
ActionBar action_bar;
TextView welcome_headder;
String welcome_headder_string;
ButtonFloatSmall options;
signoutoptionsbuttons signout,shutdown,restart;
Dialog sign_out_dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.modes);
action_bar = getActionBar();
action_bar.hide();
welcome_headder = (TextView)findViewById(R.id.welcome_notation);
Intent welcome_headder_intent = getIntent();
welcome_headder_string= welcome_headder_intent.getStringExtra("USERNAME");
welcome_headder.setText("Welcome, "+welcome_headder_string);
options = (ButtonFloatSmall)findViewById(R.id.options_signout);
options.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Testing",Toast.LENGTH_LONG).show();
sign_out_dialog = new Dialog(Modes.this);
sign_out_dialog.setTitle("Signout options");
sign_out_dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.shutdown);
sign_out_dialog.setContentView(R.layout.signout_options);
signout = (signoutoptionsbuttons)sign_out_dialog.findViewById(R.id.signout);
signout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.remove("logged");
editor.commit();
System.exit(0);
}
});
restart = (signoutoptionsbuttons)sign_out_dialog.findViewById(R.id.restart);
restart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new SnackBar(Modes.this, "This will Signout and restart the panel.Are yout sure you want to Restart the panel !","Yes",new OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.remove("logged");
editor.commit();
try
{
Process proc = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "reboot" });
}
catch(Exception e)
{
Toast.makeText(Modes.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}).show();
}
});
shutdown = (signoutoptionsbuttons)sign_out_dialog.findViewById(R.id.signout_shutdown);
shutdown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new SnackBar(Modes.this, "Are you sure you want to shut down the panel", "yes", new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.remove("logged");
editor.commit();
try
{
Process proc = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "reboot -p" });
}catch (IOException e)
{
Toast.makeText(Modes.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}).show();
}
});
}
});
sign_out_dialog.show();
auto = (Button)findViewById(R.id.button_auto);
auto.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent auto = new Intent(Modes.this,Auto.class);
auto.putExtra("USERNAME",welcome_headder_string);
startActivity(auto);
}
});
manual = (Button)findViewById(R.id.button_manualmode);
manual.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent manual = new Intent(Modes.this,Manual.class);
startActivity(manual);
}
});
adminsettings = (Button)findViewById(R.id.button_Adminsettings);
savedInstanceState = getIntent().getExtras();
if(savedInstanceState!=null){
String value_username = savedInstanceState.getString("USERNAME"); //getting username key,value pairs sent from previous intents
String value_password = savedInstanceState.getString("PASSWORD"); //getting password key,value pairs sent from previous intents
if(value_username.equals("medequip") && value_password.equals("medequip")){ //Comparing username and password sent from previous intent activity to display admin settings button or not...
adminsettings.setVisibility(View.VISIBLE); //setting visibility for a button
}
else
adminsettings.setVisibility(View.GONE);
}
adminsettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent retriving_database = new Intent (Modes.this,AdminSettings.class);
startActivity(retriving_database);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.modes, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is my XMl:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.developer.milanandroid.Modes" >
<RelativeLayout
android:id="#+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="#FFC107">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="MODES"
android:textColor="#FFFFFF"
android:textSize="75dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/welcome_notation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="152dp"
android:layout_toRightOf="#+id/textView1"
android:gravity="right"
android:textColor="#FFFFFF" />
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativelayout"
android:layout_marginTop="44dp"
android:layout_weight="1"
android:orientation="vertical"
android:padding="6dip" >
<LinearLayout
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:orientation="horizontal" >
<Button
android:id="#+id/button_auto"
style="#style/HomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/automode"
android:text="#string/automode" />
<Button
android:id="#+id/button_manualmode"
style="#style/HomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/manualmode"
android:text="#string/manualmode" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="#+id/buttoncalibrationmode"
style="#style/HomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/calibration"
android:text="#string/calibration" />
<Button
android:id="#+id/button_manualmode_2"
style="#style/HomeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/manualmode"
android:text="#string/manualmode" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="150dp"
android:orientation="horizontal" >
<Button
android:id="#+id/button_Review"
style="#style/HomeButton"
android:layout_width="380dp"
android:layout_height="wrap_content"
android:drawableTop="#drawable/review"
android:text="#string/Review" />
<Button
android:id="#+id/button_Adminsettings"
style="#style/HomeButton"
android:layout_width="380dp"
android:layout_height="wrap_content"
android:drawableTop="#drawable/adminsettings"
android:text="#string/Adminsettings" />
</LinearLayout>
</LinearLayout>
<com.gc.materialdesign.views.ButtonFloatSmall
android:id="#+id/options_signout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="72dp"
android:layout_marginTop="128dp"
android:background="#FF8F00"
android:drawable="#drawable/usr" >
</com.gc.materialdesign.views.ButtonFloatSmall>
<!-- <com.gc.materialdesign.view.ButtonFloatSmall
android:id="#+id/options_sign_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="75dp"
android:layout_marginTop="128dp"
android:background="#FF8F00"></com.gc.materialdesign.view.ButtonFloatSmall>-->
</RelativeLayout>
This is my custom dialog layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:widget="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.milan.paperbuttons.signoutoptionsbuttons
android:id="#+id/signout"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
widget:paper_text="Signout"
/>
<com.milan.paperbuttons.signoutoptionsbuttons
android:id="#+id/restart"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="118dp"
widget:paper_text="Signout and Restart " />s
<com.milan.paperbuttons.signoutoptionsbuttons
android:id="#+id/signout_shutdown"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/restart"
android:layout_below="#+id/restart"
android:layout_marginTop="22dp"
widget:paper_text="Signout and Shutdown" />
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/dialog_return"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/signout_shutdown"
android:layout_marginRight="96dp"
android:layout_marginTop="23dp"
android:text="#string/back"
android:textColor="#ffffff" >
</com.gc.materialdesign.views.ButtonFlat>
</RelativeLayout>
This is my logcat:
01-02 10:22:30.070: E/AndroidRuntime(13445): FATAL EXCEPTION: main
01-02 10:22:30.070: E/AndroidRuntime(13445): Process: com.developer.milanandroid, PID: 13445
01-02 10:22:30.070: E/AndroidRuntime(13445): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.developer.milanandroid/com.developer.milanandroid.Modes}: java.lang.NullPointerException
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.os.Handler.dispatchMessage(Handler.java:102)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.os.Looper.loop(Looper.java:136)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-02 10:22:30.070: E/AndroidRuntime(13445): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 10:22:30.070: E/AndroidRuntime(13445): at java.lang.reflect.Method.invoke(Method.java:515)
01-02 10:22:30.070: E/AndroidRuntime(13445): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-02 10:22:30.070: E/AndroidRuntime(13445): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-02 10:22:30.070: E/AndroidRuntime(13445): at dalvik.system.NativeStart.main(Native Method)
01-02 10:22:30.070: E/AndroidRuntime(13445): Caused by: java.lang.NullPointerException
01-02 10:22:30.070: E/AndroidRuntime(13445): at com.developer.milanandroid.Modes.onCreate(Modes.java:132)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.Activity.performCreate(Activity.java:5231)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-02 10:22:30.070: E/AndroidRuntime(13445): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-02 10:22:30.070: E/AndroidRuntime(13445): ... 11 more
You initialized the dialog in the onClickListener(). Initialize it outside of it. That should fix it.
On a side note, don't add so much code in onCreate(). Try to have multiple methods. Will help in code readability.
Edit:
Create one method maybe named setupDialog(); remove the following lines which are present in your options variable's onClickListener implementation and add these to this newly created method.
sign_out_dialog = new Dialog(Modes.this);
sign_out_dialog.setTitle("Signout options");
sign_out_dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.shutdown);
sign_out_dialog.setContentView(R.layout.signout_options);
signout = (signoutoptionsbuttons)sign_out_dialog.findViewById(R.id.signout);
signout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.remove("logged");
editor.commit();
System.exit(0);
}
});
Now, call the newly created before your options.setOnClickListener line.
Hi I am fairly new to android development and generally seem to be understanding this, I've been following a tutorial on youtube and it has all worked fine until now I keep getting the error message that the app has stopped unexpectedly.
right now I have two Java files:
Game.java
MainMenu.java
And I have four XML files:
activity_game.xml
activity_main_menu.xml
pause_menu.xml
pause.xml
Here is the code for Game.java:
package com.example.deepseadiver;
import android.support.v7.app.ActionBarActivity;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class Game extends ActionBarActivity {
View Pause;
View Pause_Menu;
RelativeLayout Rel_main_game;
//Option for user selecting continue
OnClickListener Continue_List = new OnClickListener() {
#Override
public void onClick(View v) {
//Make the pause menu invisible
Pause_Menu.setVisibility(View.GONE);
//Make the game Visible
Pause.setVisibility(View.VISIBLE);
}
};
//Option for user selecting Main Menu
OnClickListener Main_Menu_List = new OnClickListener() {
#Override
public void onClick(View v) {
Game.this.finish();
}
};
OnClickListener Pause_Click = new OnClickListener() {
#Override
public void onClick(View v) {
Pause.setVisibility(View.GONE);
Pause_Menu.setVisibility(View.VISIBLE);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Rel_main_game = (RelativeLayout) findViewById(R.id.Game_Screen);
//Gets the size of the device Screen
DisplayMetrics Size = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(Size);
#SuppressWarnings("unused")
//Sets the screen Width & Height in pixels
final int Screen_Height = Size.heightPixels;
final int Screen_Width = Size.widthPixels;
//Sets the Pause button Layout
#SuppressWarnings("static-access")
LayoutInflater myInflater = (LayoutInflater) getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
Pause = myInflater.inflate(R.layout.pause, null, false);
Pause.setX(Screen_Width - 250);
Pause.setY(0);
Rel_main_game.addView(Pause);
Pause.setOnClickListener(Pause_Click);
//Sets the Height and Width
Pause.getLayoutParams().height=250;
Pause.getLayoutParams().width=250;
Pause = myInflater.inflate(R.layout.pause_menu, null, false);
Rel_main_game.addView(Pause_Menu);
Pause_Menu.setVisibility(View.GONE);
ImageView Continue = (ImageView)Pause_Menu.findViewById(R.id.Continue);
ImageView Return_Main = (ImageView)Pause_Menu.findViewById(R.id.Return_Main);
Continue.setOnClickListener(Continue_List);
Return_Main.setOnClickListener(Main_Menu_List);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the code for MainMenu.java:
package com.example.deepseadiver;
//Imports the Required Android libaries
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
//Declaration of the Main Menu
public class MainMenu extends ActionBarActivity {
//Creates the Code Views
MediaPlayer MainMenuMusic;
RelativeLayout Start;
ImageView ImageButton;
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
//Creates a code representation of a graphical object on activity_main_menu
Start = (RelativeLayout) findViewById(R.id.Button_Start);
ImageButton = (ImageView) findViewById(R.id.Image_Button);
txt = (TextView) findViewById(R.id.Text_Start);
//Imprts a Custom Font
Typeface Adventure = Typeface.createFromAsset(getAssets(), "Adventure.ttf");
txt.setTypeface(Adventure);
//Detects the user touch on Main Menu and changes button appearance
Start.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
//Detects a user Click on Main Menu and starts the next activity
Start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent Start_Game = new Intent(MainMenu.this, Game.class);
startActivity(Start_Game);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the code for activity_game.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/Game_Screen"
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.deepseadiver.Game" >
</RelativeLayout>
Here is the code for activity_main_menu.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:background="#drawable/background"
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.deepseadiver.MainMenu" >
<ImageView
android:id="#+id/Continue"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/title_image" />
<RelativeLayout
android:id="#+id/Button_Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:minHeight="150px"
android:minWidth="350px" >
<ImageView
android:id="#+id/Image_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:src="#drawable/button_off" />
<TextView
android:id="#+id/Text_Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:text="#string/Start"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</RelativeLayout>
Here is the code for pause_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Rel"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/Continue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/button_off" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="179dp"
android:text="#string/Continue"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="102dp" >
<ImageView
android:id="#+id/Return_Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:src="#drawable/button_off" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Return_Main"
android:layout_centerVertical="true"
android:layout_marginLeft="178dp"
android:text="#string/Main"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Here is the code for pause.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Pause"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<ImageView
android:id="#+id/Continue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/button_off" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/Pause"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</RelativeLayout>
And here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deepseadiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
>
<activity
android:name="MainMenu"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="Game"
android:label="#string/title_activity_game">
</activity>
</application>
</manifest>
Sorry I know that's a lot of code but I have spent hours looking at it and cannot find the problem any help is much appreciated.
Log Cat:
04-06 20:44:04.300: E/AndroidRuntime(5657): FATAL EXCEPTION: main
04-06 20:44:04.300: E/AndroidRuntime(5657): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.deepseadiver/com.example.deepseadiver.MainMenu}: java.lang.RuntimeException: native typeface cannot be made
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.os.Looper.loop(Looper.java:150)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.ActivityThread.main(ActivityThread.java:4277)
04-06 20:44:04.300: E/AndroidRuntime(5657): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 20:44:04.300: E/AndroidRuntime(5657): at java.lang.reflect.Method.invoke(Method.java:507)
04-06 20:44:04.300: E/AndroidRuntime(5657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-06 20:44:04.300: E/AndroidRuntime(5657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-06 20:44:04.300: E/AndroidRuntime(5657): at dalvik.system.NativeStart.main(Native Method)
04-06 20:44:04.300: E/AndroidRuntime(5657): Caused by: java.lang.RuntimeException: native typeface cannot be made
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.graphics.Typeface.<init>(Typeface.java:147)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.graphics.Typeface.createFromAsset(Typeface.java:121)
04-06 20:44:04.300: E/AndroidRuntime(5657): at com.example.deepseadiver.MainMenu.onCreate(MainMenu.java:34)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
04-06 20:44:04.300: E/AndroidRuntime(5657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
04-06 20:44:04.300: E/AndroidRuntime(5657): ... 11 more
You are getting exception from this line-
Typeface Adventure = Typeface.createFromAsset(getAssets(), "Adventure.ttf");
only probable cause is your font path param "Adventure.ttf" is not correct. make sure you have put the ttf file in assets/Adventure.ttf path of your eclipse project.
I can't understand why I'm getting this error message. My method IS in the activity class and the spelling is correct.
02-09 18:23:57.211: E/AndroidRuntime(19939): FATAL EXCEPTION: main
02-09 18:23:57.211: E/AndroidRuntime(19939): Process: stacy.example.assignment3_stacy_v1, PID: 19939
02-09 18:23:57.211: E/AndroidRuntime(19939): java.lang.IllegalStateException: Could not find a method startRhythmandAnimation(View) in the activity class stacy.example.assignment3_stacy_v1.Assignment3MainActivity for onClick handler on view class android.widget.Button with id 'startbutton'
MainActivity.java
package stacy.example.assignment3_stacy_v1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
public class Assignment3MainActivity extends Activity {
private View mMileTimeGoal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_assignment3_main);
mMileTimeGoal = findViewById(R.id.miletimegoal);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.assignment3_main, menu);
return true;
}
public void startRhythmandAnimation () {
String MileTime = mMileTimeGoal.getContext().toString();
String[] time_array = MileTime.split(":");
int hours = Integer.parseInt(time_array[0]);
int minutes = Integer.parseInt(time_array[1]);
int seconds = Integer.parseInt(time_array[2]);
int duration = 3600 * hours + 60 * minutes + seconds;
int steps_per_second = 3;
int running_rate = duration * steps_per_second;
View rightfoot = findViewById(R.id.rightfoot);
View leftfoot = findViewById(R.id.leftfoot);
rightfoot.setVisibility(View.VISIBLE);
Animation anim = AnimationUtils.makeInChildBottomAnimation(this);
rightfoot.startAnimation(anim);
leftfoot.setVisibility(View.VISIBLE);
leftfoot.startAnimation(anim);
}
public void resetTimetoZeroes () {
String MileTime = mMileTimeGoal.getContext().toString();
//Int MileTime = 0;
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Assignment3MainActivity" >
<ImageView
android:id="#+id/leftfoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/rightfoot"
android:layout_toLeftOf="#+id/rightfoot"
android:src="#drawable/leftfoot" />
<EditText
android:id="#+id/miletimegoal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:ems="10"
android:inputType="time"
android:hint="Mile Time Goal?" />
<ImageView
android:id="#+id/rightfoot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="74dp"
android:layout_marginRight="36dp"
android:src="#drawable/rightfoot" />
<Button
android:id="#+id/startbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/leftfoot"
android:layout_alignRight="#+id/leftfoot"
android:onClick="startRhythmandAnimation"
android:text="#string/start_button" />
<Button
android:id="#+id/resetbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/startbutton"
android:layout_alignBottom="#+id/startbutton"
android:layout_alignLeft="#+id/rightfoot"
android:text="#string/reset_button"
android:onClick="resetTimetoZeroes" />
You need to include a single View parameter for the system to find your method:
public void startRhythmandAnimation (View view)
StockLedgerRpt Class showing null pointer exception. This class displays a list in spinner view to select a stock item.
package com.manacle.umma.activity;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.manacle.umma.java.Constants;
import com.manacle.umma.java.DatabaseHelper;
import com.manacle.umma.java.Header;
import com.manacle.umma.java.Validation;
public class StockLedgerRep extends ListActivity {
Button srch_btn;
TextView stckldgr_header, stckldgr_cntr, frm_date, to_date,
stck_item;
EditText to_dt_edit, frm_dt_edit;
Spinner add_stcitm_spin;
RelativeLayout stckldgr_relative;
LinearLayout stckldgr_linearLayout;
HeaderDb headerDb;
ListAdapter adapter;
Header headerClass;
HashMap<String, String> map, categoryMap, unitMap;
String value, stockId, voucherId, code;
String stockItemsname[] = null;
String batchId = null;
DatabaseHelper databaseHelper;
Map stockItemMap;
SharedPreferences preferences;
String itemname;
String firstSelection;
private TextToSpeech tts;
String batchIDD;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stck_ldgr_rpt);
final HeaderDb db = new HeaderDb(getApplicationContext());
headerDb = new HeaderDb(getApplicationContext());
stckldgr_relative = (RelativeLayout) findViewById(R.id.stckldgr_relative_layout);
stckldgr_linearLayout = (LinearLayout) findViewById(R.id.stckldgr_linear_layout);
stckldgr_header = (TextView) findViewById(R.id.stckldgr_header);
stckldgr_cntr = (TextView) findViewById(R.id.stckldgr_txt);
frm_date = (TextView) findViewById(R.id.stckldgr_frmdate_txt);
to_date= (TextView) findViewById(R.id.stckldgr_todate_txt);
stck_item=(TextView) findViewById(R.id.stckldgr_stockItem_txt);
add_stcitm_spin=(Spinner) findViewById(R.id.stckldgr_stock_item_spinner);
frm_dt_edit = (EditText) findViewById(R.id.stckldgr_frmdate_edit);
to_dt_edit = (EditText) findViewById(R.id.stckldgr_todate_edit);
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
frm_dt_edit.setText(dateFormat.format(date));
frm_dt_edit.setFocusable(false);
to_dt_edit.setText(dateFormat.format(date));
to_dt_edit.setFocusable(false);
String dat = preferences.getString("STOCK_CODE", "");
if (!dat.equalsIgnoreCase("")) {
// Toast.makeText(getApplicationContext(), "Selected date="+dat,
// Toast.LENGTH_LONG).show();
stckldgr_cntr.setText(dat);
}
final OnDateSetListener odsl = new OnDateSetListener() {
#Override
public void onDateSet(DatePicker arg0, int year, int month, int day) {
String date_format = Validation.dateFormat(month, day, year);
frm_dt_edit.setText(date_format);
to_dt_edit.setText(date_format);
}
};
frm_dt_edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
DatePickerDialog datePickDiag = new DatePickerDialog(
StockLedgerRep.this, odsl, cal.get(Calendar.YEAR), cal
.get(Calendar.MONTH), cal
.get(Calendar.DAY_OF_MONTH));
datePickDiag.show();
}
});
to_dt_edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
DatePickerDialog datePickDiag = new DatePickerDialog(
StockLedgerRep.this, odsl, cal.get(Calendar.YEAR), cal
.get(Calendar.MONTH), cal
.get(Calendar.DAY_OF_MONTH));
datePickDiag.show();
}
});
com.manacle.umma.java.SpinnerAdapter adapter1 = new com.manacle.umma.java.SpinnerAdapter(
this, android.R.layout.simple_spinner_item, getStockItem());
add_stcitm_spin.setAdapter(adapter1);
add_stcitm_spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Object item = arg0.getItemAtPosition(arg2);
if (item.equals("")) {
} else {
firstSelection = add_stcitm_spin.getSelectedItem()
.toString();
// tToast(""+firstSelection);
if (firstSelection.equals("All")) {
if (headerDb.getVouDateTransIdfromVouTableforAllItem(value, batchId).size() > 0) {
//showAllName_Id();
} else {
// tToast("No Record, Please add new Record.");
//showAllName_Id();
}
} else {
List stockItemList = headerDb
.getAllIRMStockItem(headerDb
.getAllIRMStockcategoryID());
Map mapItem = (Map) stockItemList.get(arg2 - 1);
code = (String) mapItem.get(add_stcitm_spin
.getSelectedItem().toString());
}
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}); }
private void tToast(String s) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, s, duration);
toast.show();
}
public String[] getStockItem() {
String arr_name[] = null;
List listCategoryId = headerDb.getAllIRMStockcategoryID();
List stockList = headerDb.getAllIRMStockItem(listCategoryId);
Iterator memberItemsItr = headerDb.getAllIRMStockItem(listCategoryId)
.iterator();
int i = 0;
arr_name = new String[stockList.size() + 1];
arr_name[0] = "All";
i++;
// tToast("ARRSIZE="+listCategoryId.size());
while (memberItemsItr.hasNext()) {
stockItemMap = (Map) memberItemsItr.next();
Iterator<?> iter = stockItemMap.entrySet().iterator();
// String arr_code[]=new String[memberItemsMap.size()+1];
while (iter.hasNext()) {
Map.Entry mEntry = (Map.Entry) iter.next();
// tToast(mEntry.getKey() + " : " + mEntry.getValue());
String stockName = (String) mEntry.getKey();
String stockCode = (String) mEntry.getValue();
arr_name[i] = stockName;
i++;
}
}
return arr_name;
}
public void showAllName_Id() {
adapter = new SimpleAdapter(this, headerDb.getVouDateTransIdfromVouTableforAllItem(value, batchId),
R.layout.stock_list_item, new String[] {
Constants.Bat_Acc_Voch_VOUCHER_DATE,
Constants.Bat_Acc_Voch_Trans_Id }, new int[] {
R.id.stock_list_code, R.id.stock_list_name });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// lv.setBackgroundColor(Integer.parseInt(headerDb.getBgColor("KD125"),
// 16)+0xFF000000);
// lv.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
try {
// lv.setSelector(R.drawable.sel);
} catch (Exception e) {
// TODO: handle exception
tToast("Error" + e);
}
// lv.setSelector(Color.RED);
lv.setOnTouchListener(new ListView.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
// v.setBackgroundResource(R.drawable.sel);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
// v.setBackgroundResource(R.drawable.sel);
break;
}
// Handle ListView touch events.
v.onTouchEvent(event);
return true;
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name = ((TextView) view
.findViewById(R.id.stock_list_code)).getText()
.toString();
voucherId = ((TextView) view.findViewById(R.id.stock_list_name))
.getText().toString();
String Vid[] = voucherId.split("-");
String vid = Vid[1];
String mobtrans = Vid[0];
// tToast("Voucher Id="+vid);
// tToast("Selected Value="+name+""+code);
Intent in = new Intent(getApplicationContext(),
IRM_add_preview.class);
in.putExtra("VOU_ID", vid);
in.putExtra("VALUE", value);
in.putExtra("MOBTRANS", mobtrans);
startActivity(in);
}
});
}
}
stck_ldgr Xml this xml shows three things one is the spinner stock items column and the other two are select dates. I am unable to get the error. pls help. even added in androidmanifest.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:focusableInTouchMode="true"
android:id="#+id/stckldgr_relative_layout"
>
<TextView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="10dp"
/>
<TextView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageView2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1"
android:orientation="vertical"
android:id="#+id/stckldgr_linear_layout"
>
<!-- -->
<ScrollView
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:paddingTop="6dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Stock Ledger Report"
android:textStyle="bold"
android:gravity="center"
android:id="#+id/stckldgr_header"
/>
<View
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#ff106510"
android:layout_marginLeft="6dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="12dip" />
<LinearLayout
android:id="#+id/linearLayout_login_4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:id="#+id/stckldgr_txt"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Stock Center Code-Name"
android:gravity="center"
/>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:text="Stock Item"
android:height="40dp"
android:width="150dp"
android:layout_marginTop="10dip"
android:textStyle="bold"
android:id="#+id/stckldgr_stockItem_txt"
android:layout_gravity="center_horizontal|bottom"
></TextView>
<Spinner
android:id="#+id/stckldgr_stock_item_spinner"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginRight="6dip"
android:saveEnabled="true" >
</Spinner>
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
>
<TextView
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginTop="10dip"
android:text="From Date"
android:textStyle="bold"
android:id="#+id/stckldgr_frmdate_txt"
/>
<EditText
android:id="#+id/stckldgr_frmdate_edit"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="0dip"
android:layout_marginRight="10dip"
android:editable="false"
>
<requestFocus />
</EditText>
</TableRow>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
>
<TextView
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginTop="10dip"
android:text="To Date"
android:textStyle="bold"
android:id="#+id/stckldgr_todate_txt"
/>
<EditText
android:id="#+id/stckldgr_todate_edit"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="0dip"
android:layout_marginRight="10dip"
android:editable="false"
>
<requestFocus />
</EditText>
</TableRow>
</TableLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:divider="#color/divider"
android:listSelector="#369659"
android:cacheColorHint="#369659"
android:dividerHeight="2px"
android:choiceMode="singleChoice"
android:gravity="center" >
</ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>
Logcat
10-10 10:44:55.832: D/AndroidRuntime(2639): Shutting down VM
10-10 10:44:55.865: W/dalvikvm(2639): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-10 10:44:56.023: E/AndroidRuntime(2639): FATAL EXCEPTION: main
10-10 10:44:56.023: E/AndroidRuntime(2639): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.manacle.umma.activity/com.manacle.umma.activity.StockLedgerRep}: java.lang.NullPointerException
10-10 10:44:56.023: E/AndroidRuntime(2639): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-10 10:44:56.023: E/AndroidRuntime(2639): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-10 10:44:56.023: E/AndroidRuntime(2639): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-10 10:44:56.023: E/AndroidRuntime(2639): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-10 10:44:56.023: E/AndroidRuntime(2639): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 10:44:56.023: E/AndroidRuntime(2639): at android.os.Looper.loop(Looper.java:137)
10-10 10:44:56.023: E/AndroidRuntime(2639): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-10 10:44:56.023: E/AndroidRuntime(2639): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 10:44:56.023: E/AndroidRuntime(2639): at java.lang.reflect.Method.invoke(Method.java:511)
10-10 10:44:56.023: E/AndroidRuntime(2639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-10 10:44:56.023: E/AndroidRuntime(2639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-10 10:44:56.023: E/AndroidRuntime(2639): at dalvik.system.NativeStart.main(Native Method)
10-10 10:44:56.023: E/AndroidRuntime(2639): Caused by: java.lang.NullPointerException
10-10 10:44:56.023: E/AndroidRuntime(2639): at com.manacle.umma.activity.StockLedgerRep.onCreate(StockLedgerRep.java:96)
10-10 10:44:56.023: E/AndroidRuntime(2639): at android.app.Activity.performCreate(Activity.java:5104)
10-10 10:44:56.023: E/AndroidRuntime(2639): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-10 10:44:56.023: E/AndroidRuntime(2639): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-10 10:44:56.023: E/AndroidRuntime(2639): ... 11 more
Your preferences object is null.
You need to initialise it before you use it. Something like this:
preferences = PreferenceManager.getDefaultSharedPreferences(this);
String dat = preferences.getString("STOCK_CODE", "");
Because you didn't initialize preferences. Here is the problem:
String dat = preferences.getString("STOCK_CODE", "");
If you use Eclipse or any other IDE, you should double-click on the error line so it will get you right on the source of the exception...
10-10 10:44:56.023: E/AndroidRuntime(2639): at com.manacle.umma.activity.StockLedgerRep.onCreate(StockLedgerRep.java:96)
Should be on the preferences thing like other users suggest...
Can you write this code before line 96.
preferences= getApplicationContext.getSharedPreferences("blablabla", Context.MODE_PRIVATE);