java.lang.runtimeexception unable to start activity componentinfo java.lang.nullpointerexception android - java

im making a simple calories calculator and it gives me that error, i already reviewed the code and searched here for the solution but im not able to see why is not running.
I thought it was because i had not initialized the variable, so i did it and still got the same error, maybe is something with the spinners, im new on using spinners
here is the code:
public class CaloriesCalculator extends ActionBarActivity {
EditText etAge, etWeight, etHeight;
Button btnCalculate;
TextView tvResult;
Spinner spinnerGender, spinnerActivity;
String itemGender, itemActivity;
int Height=0;
int Weight=0;
int Age=0;;
double bmr=0.0;
double tdee=0.0;
String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.caloriescalculator);
spinnerGender=(Spinner)findViewById(R.id.spinnerGender);
spinnerActivity=(Spinner)findViewById(R.id.spinnerActivity);
etAge=(EditText)findViewById(R.id.etAge);
etWeight=(EditText)findViewById(R.id.etWeight);
etHeight=(EditText)findViewById(R.id.etHeight);
tvResult=(TextView)findViewById(R.id.tvResult);
List<String> list = new ArrayList<String>();
list.add("Male");
list.add("Female");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, list );
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerGender.setAdapter(dataAdapter);
List<String> list2 = new ArrayList<String>();
list.add("Sedentary");
list.add("Lightly Active");
list.add("Moderalety Active");
list.add("Very Active");
list.add("Extremely Active");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, list2 );
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerActivity.setAdapter(dataAdapter2);
btnCalculate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
spinnerGender.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
spinnerActivity.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
itemGender=String.valueOf(spinnerGender.getSelectedItem());
itemActivity=String.valueOf(spinnerActivity.getSelectedItem());
if(itemGender=="Male"){
Weight=Integer.parseInt(etWeight.getText().toString());
Height=Integer.parseInt(etHeight.getText().toString());
Age=Integer.parseInt(etAge.getText().toString());
if(itemActivity=="Sedentary"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.2;
}
else if(itemActivity=="Lightly Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.375;
}
else if(itemActivity=="Moderalety Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.55;
}
else if(itemActivity=="Very Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.725;
}
else if(itemActivity=="Extremely Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.9;
}
}
else if(itemGender=="Female") {
Weight=Integer.parseInt(etWeight.getText().toString());
Height=Integer.parseInt(etHeight.getText().toString());
Age=Integer.parseInt(etAge.getText().toString());
if(itemActivity=="Sedentary"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.2;
}
else if(itemActivity=="Lightly Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.375;
}
else if(itemActivity=="Moderalety Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.55;
}
else if(itemActivity=="Very Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.725;
}
else if(itemActivity=="Extremely Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.9;
}
}
result=Double.toString(tdee);
tvResult.setText(result);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#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);
}
}
logcat:
11-28 17:20:05.501: E/AndroidRuntime(1455): FATAL EXCEPTION: main
11-28 17:20:05.501: E/AndroidRuntime(1455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.calculadoracalorias/com.app.calculadoracalorias.CaloriesCalculator}: java.lang.NullPointerException
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.os.Looper.loop(Looper.java:137)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 17:20:05.501: E/AndroidRuntime(1455): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:20:05.501: E/AndroidRuntime(1455): at java.lang.reflect.Method.invoke(Method.java:525)
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 17:20:05.501: E/AndroidRuntime(1455): at dalvik.system.NativeStart.main(Native Method)
11-28 17:20:05.501: E/AndroidRuntime(1455): Caused by: java.lang.NullPointerException
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:67)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.Activity.performCreate(Activity.java:5133)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 17:20:05.501: E/AndroidRuntime(1455): ... 11 more
11-28 17:24:42.341: D/AndroidRuntime(1507): Shutting down VM
11-28 17:24:42.341: W/dalvikvm(1507): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-28 17:24:42.371: E/AndroidRuntime(1507): FATAL EXCEPTION: main
11-28 17:24:42.371: E/AndroidRuntime(1507): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.calculadoracalorias/com.app.calculadoracalorias.CaloriesCalculator}: java.lang.NullPointerException
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.os.Looper.loop(Looper.java:137)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 17:24:42.371: E/AndroidRuntime(1507): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:24:42.371: E/AndroidRuntime(1507): at java.lang.reflect.Method.invoke(Method.java:525)
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 17:24:42.371: E/AndroidRuntime(1507): at dalvik.system.NativeStart.main(Native Method)
11-28 17:24:42.371: E/AndroidRuntime(1507): Caused by: java.lang.NullPointerException
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:70)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.Activity.performCreate(Activity.java:5133)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 17:24:42.371: E/AndroidRuntime(1507): ... 11 more

Yout btnCalculate is null, you should initialize before doing the onclickListener:
btnCalculate = (Button)findViewById(R.id.btnCalculate); //your id
Also note that you are initializing the dataAdapter2 but actually you are using dataAdapert, and list2 dont have any elements, because you declare it but u are filling always list1

Related

App crash after image capture from webview

try to capture image from webview.Its working fine but in galaxy note 3 operating on android lollipop 5.0 it crashes after clicking select button.
webView.setWebChromeClient(new WebChromeClient() {
// For Android 5.0
public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams) {
//qrCodeReaderView.stopCamera();
mFilePathCallback = filePath;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
mCapturedImageURI = Uri.fromFile(photoFile);
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI
);
} else {
}
startActivityForResult(takePictureIntent, INPUT_FILE_REQUEST_CODE);
return true;
}
});
Logcat:
11-28 01:00:54.473 D/Activity(13624): performCreate Call secproduct feature valuefalse
11-28 01:00:54.473 D/Activity(13624): performCreate Call debug elastic valuetrue
11-28 01:00:54.473 E/Main (13624): cancel timer
11-28 01:00:54.893 E/Main (13624): Invoking logout timer
11-28 01:00:54.963 E/WindowManager(13624):
11-28 01:00:54.963 E/WindowManager(13624): android.view.WindowLeaked: Activity com.shashank.gaintz.WebViewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{1de4327 V.E..... R......D 0,0-144,144} that was originally added here
11-28 01:00:54.963 E/WindowManager(13624): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:465)
11-28 01:00:54.963 E/WindowManager(13624): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:277)
11-28 01:00:54.963 E/WindowManager(13624): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-28 01:00:54.963 E/WindowManager(13624): at android.app.Dialog.show(Dialog.java:312)
11-28 01:00:54.963 E/WindowManager(13624): at com.shashank.gaintz.WebViewActivity$2.onPageStarted(WebViewActivity.java:327)
11-28 01:00:54.963 E/WindowManager(13624): at com.android.webview.chromium.WebViewContentsClientAdapter.onPageStarted(WebViewContentsClientAdapter.java:190)
11-28 01:00:54.963 E/WindowManager(13624): at org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:20)
11-28 01:00:54.963 E/WindowManager(13624): at android.os.Handler.dispatchMessage(Handler.java:102)
11-28 01:00:54.963 E/WindowManager(13624): at android.os.Looper.loop(Looper.java:145)
11-28 01:00:54.963 E/WindowManager(13624): at android.app.ActivityThread.main(ActivityThread.java:5938)
11-28 01:00:54.963 E/WindowManager(13624): at java.lang.reflect.Method.invoke(Native Method)
11-28 01:00:54.963 E/WindowManager(13624): at java.lang.reflect.Method.invoke(Method.java:372)
11-28 01:00:54.963 E/WindowManager(13624): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
11-28 01:00:54.963 E/WindowManager(13624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
11-28 01:00:55.023 D/Activity(13624): performCreate Call secproduct feature valuefalse
11-28 01:00:55.023 D/Activity(13624): performCreate Call debug elastic valuetrue
11-28 01:00:55.023 E/Main (13624): cancel timer
11-28 01:00:55.133 E/ViewRootImpl(13624): sendUserActionEvent() mView == null
11-28 01:00:55.133 I/Timeline(13624): Timeline: Activity_idle id: android.os.BinderProxy#260d2f13 time:22633932
11-28 01:00:55.133 I/Timeline(13624): Timeline: Activity_idle id: android.os.BinderProxy#260d2f13 time:22633932
11-28 01:00:55.553 D/AndroidRuntime(13624): Shutting down VM
11-28 01:00:55.553 E/ReportHandler(13624): FATAL EXCEPTION: main
11-28 01:00:55.553 E/ReportHandler(13624): java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{1de4327 V.E..... R......D 0,0-144,144} not attached to window manager
11-28 01:00:55.553 E/ReportHandler(13624): at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:402)
11-28 01:00:55.553 E/ReportHandler(13624): at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:328)
11-28 01:00:55.553 E/ReportHandler(13624): at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
11-28 01:00:55.553 E/ReportHandler(13624): at android.app.Dialog.dismissDialog(Dialog.java:431)
11-28 01:00:55.553 E/ReportHandler(13624): at android.app.Dialog.dismiss(Dialog.java:414)
11-28 01:00:55.553 E/ReportHandler(13624): at com.shashank.gaintz.WebViewActivity$2.onPageFinished(WebViewActivity.java:307)
11-28 01:00:55.553 E/ReportHandler(13624): at com.android.webview.chromium.WebViewContentsClientAdapter.onPageFinished(WebViewContentsClientAdapter.java:199)
11-28 01:00:55.553 E/ReportHandler(13624): at org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:72)
11-28 01:00:55.553 E/ReportHandler(13624): at android.os.Handler.dispatchMessage(Handler.java:102)
11-28 01:00:55.553 E/ReportHandler(13624): at android.os.Looper.loop(Looper.java:145)
11-28 01:00:55.553 E/ReportHandler(13624): at android.app.ActivityThread.main(ActivityThread.java:5938)
11-28 01:00:55.553 E/ReportHandler(13624): at java.lang.reflect.Method.invoke(Native Method)
11-28 01:00:55.553 E/ReportHandler(13624): at java.lang.reflect.Method.invoke(Method.java:372)
11-28 01:00:55.553 E/ReportHandler(13624): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
11-28 01:00:55.553 E/ReportHandler(13624): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
11-28 01:00:55.563 I/Timeline(13624): Timeline: Activity_launch_request id:com.shashank.gaintz time:22634360
11-28 01:00:55.633 I/Process (13624): Sending signal. PID: 13624 SIG: 9
11-28 01:00:55.803 E/SELinux (14806): [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
11-28 01:00:55.803 I/art (14806): Late-enabling -Xcheck:jni
11-28 01:00:55.823 D/TimaKeyStoreProvider(14806): TimaSignature is unavailable
11-28 01:00:55.823 D/ActivityThread(14806): Added TimaKeyStore provider
11-28 01:00:55.863 E/SELinux (14823): [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
11-28 01:00:55.863 I/art (14823): Late-enabling -Xcheck:jni
11-28 01:00:55.873 D/ResourcesManager(14806): creating new AssetManager and set to /data/app/com.shashank.gaintz-1/base.apk
11-28 01:00:55.893 D/TimaKeyStoreProvider(14823): TimaSignature is unavailable
11-28 01:00:55.893 D/ActivityThread(14823): Added TimaKeyStore provider

Checkbox in the popupMenu

I have popupMenu and CheckBox. I need make write status CheckBox to boolean.
This code not working:
MenuItem fast_result;
boolean fast=false;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.FastResult:
fast_result = item.getSubMenu().getItem(R.id.FastResult);//This is 182 line
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;
}
}
It is errors:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.alexvsalex.HelpforMath.RootsActivity.onOptionsItemSelected(RootsActivity.java:182)
at android.app.Activity.onMenuItemSelected(Activity.java:2502)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
at android.widget.AbsListView$1.run(AbsListView.java:3168)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
What to do?
The problem is solved:
case R.id.FastResult:
fast_result = item; //There was an error
fast_result.setChecked(!fast_result.isChecked());
fast=fast_result.isChecked();
return true;

Android - NullPointerException on ListView adapter when using SimpleDateFormat

I'm having trouble when displaying a custom ListView on a tab (fragment). If I just set the adapter (which is a separate class that extends BaseAdapter) and the fields downloaded from the server, it works just fine. If I set the application to use my custom DateFormat class and format the date taken from the server in dd/MM/aaaa, I get a NullPointerException related to "childMeasure" (full logcat of the error below).
IndicatorsListAdapter.java (the adapter for the ListView)
package android.palharini.myhealth.fragments.tabs.adapters;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.palharini.myhealth.R;
import android.palharini.myhealth.date_time.DateFormat;
import android.palharini.myhealth.db.entities.Indicator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class IndicatorsListAdapter extends BaseAdapter {
static class ViewHolder {
public TextView tvMeasure;
public TextView tvUnit;
public TextView tvDate;
public TextView tvTime;
public int position;
}
private ViewHolder holder;
private Context context;
private Indicator indicator;
private List<Indicator> lsIndicators;
private DateFormat df;
public IndicatorsListAdapter(Context context, List<Indicator> lsIndicators) {
this.context = context;
this.lsIndicators = lsIndicators;
}
#Override
public int getCount() {
return lsIndicators.size();
}
#Override
public Object getItem(int id) {
return lsIndicators.get(id);
}
#Override
public long getItemId(int id) {
return lsIndicators.get(id).getId();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.list_indicators, parent, false);
holder = new ViewHolder();
holder.tvMeasure = (TextView) convertView.findViewById(R.id.lvTvMeasure);
holder.tvUnit = (TextView) convertView.findViewById(R.id.lvTvUnit);
holder.tvDate = (TextView) convertView.findViewById(R.id.lvTvDate);
holder.tvTime = (TextView) convertView.findViewById(R.id.lvTvTime);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
indicator = lsIndicators.get(position);
if (indicator != null) {
holder.tvMeasure.setText(indicator.getMedida1().toString());
holder.tvUnit.setText(indicator.getUnidade());
String stDate = df.getDataAndroid(indicator.getData());
holder.tvDate.setText(stDate);
holder.tvTime.setText(indicator.getHora());
}
return convertView;
}
}
WeekTab.java (a tab which has to display the ListView, along with an AndroidPlot graph)
package android.palharini.myhealth.fragments.tabs;
import java.util.Arrays;
import java.util.List;
import com.androidplot.ui.SizeLayoutType;
import com.androidplot.ui.SizeMetrics;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.PointLabelFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYSeries;
import com.androidplot.xy.XYStepMode;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.palharini.myhealth.R;
import android.palharini.myhealth.activities.edit.IndicatorEdit;
import android.palharini.myhealth.activities.register.IndicatorRegister;
import android.palharini.myhealth.date_time.DateFormat;
import android.palharini.myhealth.db.dao.IndicatorDAO;
import android.palharini.myhealth.db.entities.Indicator;
import android.palharini.myhealth.fragments.tabs.adapters.IndicatorsListAdapter;
import android.palharini.myhealth.session.SessionManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
public class WeekTab extends Fragment {
private static Integer difData = 7;
private static String periodo = "DAY";
private SessionManager sessao;
private DateFormat ts;
private IndicatorDAO indDAO;
private ListView lvIndicadores;
private Button btCadastrarInd;
private Integer intTipoIndicador;
private int x, y=0;
private String stDataBusca;
private Double dbMedia, vtMedias1[], vtMedias2[];
private Integer intIdUsuario, vtDatas[];
private List<Indicator> lsIndicators;
private IndicatorsListAdapter adIndicators;
private XYPlot grafico;
private XYSeries grafSerie1, grafSerie2;
private LineAndPointFormatter grafFormat;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_tab, container, false);
sessao = new SessionManager(getActivity());
ts = new DateFormat();
indDAO = new IndicatorDAO();
lvIndicadores = (ListView) view.findViewById(R.id.listViewInd);
btCadastrarInd = (Button) view.findViewById(R.id.btCadastrarInd);
grafico = (XYPlot) view.findViewById(R.id.xyPlot);
grafico.getBackgroundPaint().setColor(Color.WHITE);
grafico.getGraphWidget().getBackgroundPaint().setColor(Color.WHITE);
grafico.getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE);
grafico.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 1);
grafico.setPlotMargins(0, 0, 0, 0);
grafico.setPlotPadding(25, 10, 20, 0);
grafico.setGridPadding(30, 30, 30, 10);
grafico.getGraphWidget().setSize(new SizeMetrics(0, SizeLayoutType.FILL, 0, SizeLayoutType.FILL));
grafico.getLayoutManager().remove(grafico.getLegendWidget());
grafico.getLayoutManager().remove(grafico.getDomainLabelWidget());
grafico.getLayoutManager().remove(grafico.getRangeLabelWidget());
grafico.getLayoutManager().remove(grafico.getTitleWidget());
grafico.getLayoutManager().getPaddingPaint().setColor(Color.TRANSPARENT);
grafico.setTicksPerRangeLabel(5);
grafico.setTicksPerDomainLabel(1);
grafico.getGraphWidget().setMarginBottom(15);
Intent intent = getActivity().getIntent();
intTipoIndicador = intent.getIntExtra("tipoSelecionado", 0);
intIdUsuario = sessao.getIdUsuario();
stDataBusca = ts.getDataAtualBusca();
vtMedias1 = new Double[difData+1];
vtMedias2 = new Double[difData+1];
vtDatas = new Integer[difData+1];
for (x = difData; x>=0; x--) {
dbMedia = indDAO.buscarMedia1Periodo(
intTipoIndicador, intIdUsuario, periodo, stDataBusca, x);
if (dbMedia > 0) {
vtMedias1[x] = dbMedia;
vtDatas[x] = x;
y = x;
}
else {
vtMedias1[x] = vtMedias1[y];
vtDatas[x] = x;
}
}
grafSerie1 = new SimpleXYSeries (Arrays.asList(vtDatas), Arrays.asList(vtMedias1), "Médias");
grafFormat = new LineAndPointFormatter(
Color.RED,
Color.RED,
Color.TRANSPARENT,
null);
grafFormat.setPointLabelFormatter(new PointLabelFormatter());
grafFormat.configure(getActivity(), R.xml.formato_serie_medias);
grafico.addSeries(grafSerie1, grafFormat);
if (intTipoIndicador == 3) {
for (int x = difData; x>=0; x--) {
dbMedia = indDAO.buscarMedia2Periodo(
intTipoIndicador, intIdUsuario, periodo, stDataBusca, x);
if (dbMedia > 0) {
vtMedias2[x] = dbMedia;
vtDatas[x] = x;
y = x;
}
else {
vtMedias2[x] = vtMedias2[y];
vtDatas[x] = x;
}
}
grafSerie2 = new SimpleXYSeries (Arrays.asList(vtDatas), Arrays.asList(vtMedias2), "Médias");
grafico.addSeries(grafSerie2, grafFormat);
}
lsIndicators = indDAO.buscarIndicadoresPeriodoTipo(
intIdUsuario, intTipoIndicador, periodo, stDataBusca, difData);
adIndicators = new IndicatorsListAdapter(getActivity(), lsIndicators);
lvIndicadores.setAdapter(adIndicators);
lvIndicadores.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
// TODO Auto-generated method stub
Indicator indicSelecionado = new Indicator();
indicSelecionado = (Indicator) lvIndicadores.getItemAtPosition(position);
Intent irTelaEdicaoIndicador = new Intent(getActivity(), IndicatorEdit.class);
irTelaEdicaoIndicador.putExtra("idIndicador", indicSelecionado.getId());
startActivity(irTelaEdicaoIndicador);
}
});
btCadastrarInd.setOnClickListener(new Button.OnClickListener () {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent irTelaCadIndicador = new Intent(getActivity(), IndicatorRegister.class);
irTelaCadIndicador.putExtra("tipoSelecionado", intTipoIndicador);
startActivity(irTelaCadIndicador);
}
});
return view;
}
}
Logcat
11-28 13:14:17.177: E/AndroidRuntime(1152): FATAL EXCEPTION: main
11-28 13:14:17.177: E/AndroidRuntime(1152): Process: android.palharini.myhealth, PID: 1152
11-28 13:14:17.177: E/AndroidRuntime(1152): java.lang.NullPointerException
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.palharini.myhealth.fragments.tabs.adapters.IndicatorsListAdapter.getView(IndicatorsListAdapter.java:81)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.widget.ListView.onMeasure(ListView.java:1175)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.View.measure(View.java:16497)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.View.measure(View.java:16497)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.View.measure(View.java:16497)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.View.measure(View.java:16497)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1456)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.View.measure(View.java:16497)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.View.measure(View.java:16497)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-28 13:14:17.177: E/AndroidRuntime(1152): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.View.measure(View.java:16497)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-28 13:14:17.177: E/AndroidRuntime(1152): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.View.measure(View.java:16497)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.Choreographer.doFrame(Choreographer.java:544)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.os.Handler.handleCallback(Handler.java:733)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.os.Handler.dispatchMessage(Handler.java:95)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.os.Looper.loop(Looper.java:136)
11-28 13:14:17.177: E/AndroidRuntime(1152): at android.app.ActivityThread.main(ActivityThread.java:5001)
11-28 13:14:17.177: E/AndroidRuntime(1152): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 13:14:17.177: E/AndroidRuntime(1152): at java.lang.reflect.Method.invoke(Method.java:515)
11-28 13:14:17.177: E/AndroidRuntime(1152): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-28 13:14:17.177: E/AndroidRuntime(1152): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-28 13:14:17.177: E/AndroidRuntime(1152): at dalvik.system.NativeStart.main(Native Method)
Can someone help me on this? (if additional code samples are needed, I'll be glad to provide)
Thanks in advance!
change
private DateFormat df;
to
private DateFormat df = new SimpleDateFormat("MM-dd kk:mm:ss");
I see private DateFormat df; at the top of your IndicatorsListAdapter-class, but I don't see you intialize it anywhere before you get in the getView line 81:
String stDate = df.getDataAndroid(indicator.getData());
Perhaps change it to:
if(df != null)
String stDate = df.getDataAndroid(indicator.getData());
or simply set your df somewhere before this, like directly at the variable:
private final DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
// replace dd-MM-yyyy with your desired Date format

Android App crashes right on start

I have an app that's supposed to get some string from a server depending on the webpage, and it crashes every time I run it. I don't understand how to even debug it, and I've tried numerous changes. It started crashing ever since I messed with the GUI, and added the ability to switch views if that provides any sort of help.
Here is the code:
public class MainActivity extends Activity implements TextToSpeech.OnInitListener, OnClickListener{
private WebView mWebview;
EditText addressBar;
String currentWebpage = "http://www.aljazeera.com/news/americas/2013/07/20137113200544375.html";
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.linearview);
View main = (View) findViewById(R.layout.activity_main);
View readOut = (View) findViewById(R.layout.read_out);
#Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("Showing Activity_Main...");
viewGroup.addView(View.inflate(this, R.layout.activity_main, null));
super.onCreate(savedInstanceState);
//setContentView(R.menu.main);
addressBar = (EditText)findViewById(R.id.addressBar);
addressBar.setText(currentWebpage);
mWebview = (WebView)findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true); // enables javascript
mWebview.setWebViewClient(new WebViewClient());
System.out.println("Loading Webpage...");
mWebview.loadUrl(currentWebpage);
}
public void speakOut(String text) {
TextToSpeech tts = new TextToSpeech(this, this);
System.out.println("Speaking");
if(tts.isLanguageAvailable(Locale.ENGLISH) != -1){
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
public int speakFull(String text){
switchToRead();
String[] sentences = text.split("\n|\\.(?!\\d)|(?<!\\d)\\."); // Regex that splits the body of text into the sentences of that body which are stored in a String array.
for(int i = 0; i < sentences.length; i++){
speakOut(sentences[i]);
if(i == sentences.length - 1){
return 1;
}
}
return 0;
}
public String getText(String webPage) throws ParseException, IOException{
HttpResponse response = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://someserver.net:8080/" + webPage));
response = client.execute(request);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String responseBody = "No text found on webpage.";
int responseCode = response.getStatusLine().getStatusCode();
switch(responseCode) {
case 200:
HttpEntity entity = response.getEntity();
if(entity != null) {
responseBody = EntityUtils.toString(entity);
}
}
System.out.println("Returning Response..");
System.out.println(responseBody);
return responseBody;
}
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
}
private class tts extends AsyncTask<String, Void, String>{//Async http request to get text
#Override
protected String doInBackground(String... arg0) {
try {
System.out.println("Running seperate thread for TTS.");
int complete = 0;
while(complete == 0){
System.out.println("Speaking full..");
complete = speakFull(getText(mWebview.getUrl()));
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result){
}
}
public void clickPlay(View v){
new tts().execute("");
}
public void clickGo(View v){
if(addressBar.getText() != null){
currentWebpage = addressBar.getText().toString();
System.out.println("Current webpage changed to: " + currentWebpage);
mWebview.loadUrl(currentWebpage);
}
}
public void clickPause(View v){
System.out.println("Clicked pause.");
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
public void switchToRead(){// Switches to the reading view which displays the text that the tts engine reads off.
System.out.println("Swtiching view to Read.");
viewGroup.removeView(main);
viewGroup.addView(View.inflate(this, R.layout.read_out, null));
}
public void switchToMain(){
System.out.println("Switching view to Main.");
viewGroup.removeView(readOut);
viewGroup.addView(View.inflate(this, R.layout.activity_main, null));
}
}
Also here are the numerous errors I encounter when launching my app:
08-01 14:53:10.210: E/Trace(812): error opening trace file: No such file or directory (2)
08-01 14:53:10.600: D/AndroidRuntime(812): Shutting down VM
08-01 14:53:10.631: W/dalvikvm(812): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-01 14:53:10.660: E/AndroidRuntime(812): FATAL EXCEPTION: main
08-01 14:53:10.660: E/AndroidRuntime(812): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.webview/com.example.webview.MainActivity}: java.lang.NullPointerException
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.os.Looper.loop(Looper.java:137)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 14:53:10.660: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 14:53:10.660: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 14:53:10.660: E/AndroidRuntime(812): at dalvik.system.NativeStart.main(Native Method)
08-01 14:53:10.660: E/AndroidRuntime(812): Caused by: java.lang.NullPointerException
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.Activity.findViewById(Activity.java:1839)
08-01 14:53:10.660: E/AndroidRuntime(812): at com.example.webview.MainActivity.<init>(MainActivity.java:39)
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.Class.newInstanceImpl(Native Method)
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.Class.newInstance(Class.java:1319)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-01 14:53:10.660: E/AndroidRuntime(812): ... 11 more
08-01 14:53:27.439: D/AndroidRuntime(860): Shutting down VM
08-01 14:53:27.439: W/dalvikvm(860): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-01 14:53:27.449: E/AndroidRuntime(860): FATAL EXCEPTION: main
08-01 14:53:27.449: E/AndroidRuntime(860): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.webview/com.example.webview.MainActivity}: java.lang.NullPointerException
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.os.Looper.loop(Looper.java:137)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 14:53:27.449: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 14:53:27.449: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 14:53:27.449: E/AndroidRuntime(860): at dalvik.system.NativeStart.main(Native Method)
08-01 14:53:27.449: E/AndroidRuntime(860): Caused by: java.lang.NullPointerException
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.Activity.findViewById(Activity.java:1839)
08-01 14:53:27.449: E/AndroidRuntime(860): at com.example.webview.MainActivity.<init>(MainActivity.java:39)
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.Class.newInstanceImpl(Native Method)
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.Class.newInstance(Class.java:1319)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-01 14:53:27.449: E/AndroidRuntime(860): ... 11 more
Move your view initialization inside onCreate after setContentView
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.linearview);
...// rest of the code

using startActivityForResult

i have made 2 classes YehActivity.java and h.java. On running the application i am getting an error ,Application has stopped unexpectedly.Here is the code
public class YehActivity extends Activity {
public static final int r=1;
Button b;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode==r && resultCode==RESULT_OK){
String h=data.getStringExtra("a");
tv.setText(h);
}
}
}
where to check for null.
this is the second file
public class he extends Activity{
Button b;
EditText et;
Intent i=getIntent();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.h);
b=(Button) findViewById(R.id.button12);
et=(EditText) findViewById(R.id.editText1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String value=et.getText().toString().trim();
i.putExtra("a", value);
he.this.setResult(RESULT_OK, i);
finish();
}
});
}
}
and the log file is
02-11 23:31:46.408: I/Process(302): Sending signal. PID: 302 SIG: 9
02-11 23:45:04.778: D/AndroidRuntime(357): Shutting down VM
02-11 23:45:04.778: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:04.798: E/AndroidRuntime(357): FATAL EXCEPTION: main
02-11 23:45:04.798: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:04.798: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:04.798: E/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:04.798: E/AndroidRuntime(357): Caused by: java.lang.NullPointerException
02-11 23:45:04.798: E/AndroidRuntime(357): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:04.798: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:04.798: E/AndroidRuntime(357): ... 11 more
02-11 23:45:11.158: I/Process(357): Sending signal. PID: 357 SIG: 9
02-11 23:45:22.708: D/AndroidRuntime(374): Shutting down VM
02-11 23:45:22.708: W/dalvikvm(374): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-11 23:45:22.728: E/AndroidRuntime(374): FATAL EXCEPTION: main
02-11 23:45:22.728: E/AndroidRuntime(374): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ye/com.ye.YehActivity}: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Handler.dispatchMessage(Handler.java:99)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.os.Looper.loop(Looper.java:123)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invokeNative(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): at java.lang.reflect.Method.invoke(Method.java:507)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-11 23:45:22.728: E/AndroidRuntime(374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-11 23:45:22.728: E/AndroidRuntime(374): at dalvik.system.NativeStart.main(Native Method)
02-11 23:45:22.728: E/AndroidRuntime(374): Caused by: java.lang.NullPointerException
02-11 23:45:22.728: E/AndroidRuntime(374): at com.ye.YehActivity.onCreate(YehActivity.java:23)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-11 23:45:22.728: E/AndroidRuntime(374): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-11 23:45:22.728: E/AndroidRuntime(374): ... 11 more
02-11 23:45:25.497: I/Process(374): Sending signal. PID: 374 SIG: 9
In your
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(YehActivity.this,he.class);
//startActivity(i);
startActivityForResult(i, r);
}
});
}
onCreate() method you attempt to use b, but you never initialize it (I'm assuming its declared as a global variable). This means that you will run into a NullPointerException when you try to call setOnClickListener().
In your code in OnCreate() you have to declare b as button and then apply listener to that.
b=(Button) findViewById(R.id.button12);
Also check your data is null or not. If it is null than handle it properly.
Then your code runs fine.

Categories