My android app keeps force closing, due to the recent addition of Alarm manager. I am new to alarm manager and did what i did with parts of tutorials. In the code i have said where i have put my 'doing code' (not sure if this is the right place?). Pls also mention where i should put other parts of my code, that are not affected be alarm manager.
Here is my logcat:
04-16 14:36:52.011: E/AndroidRuntime(1127): FATAL EXCEPTION: main
04-16 14:36:52.011: E/AndroidRuntime(1127): Process: com.knight, PID: 1127
04-16 14:36:52.011: E/AndroidRuntime(1127): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.knight./com.knight.MainActivity}: java.lang.NullPointerException
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.os.Handler.dispatchMessage(Handler.java:102)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.os.Looper.loop(Looper.java:136)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-16 14:36:52.011: E/AndroidRuntime(1127): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 14:36:52.011: E/AndroidRuntime(1127): at java.lang.reflect.Method.invoke(Method.java:515)
04-16 14:36:52.011: E/AndroidRuntime(1127): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-16 14:36:52.011: E/AndroidRuntime(1127): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-16 14:36:52.011: E/AndroidRuntime(1127): at dalvik.system.NativeStart.main(Native Method)
04-16 14:36:52.011: E/AndroidRuntime(1127): Caused by: java.lang.NullPointerException
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:173)
04-16 14:36:52.011: E/AndroidRuntime(1127): at com.knight.MainActivity.<init>(MainActivity.java:44)
04-16 14:36:52.011: E/AndroidRuntime(1127): at java.lang.Class.newInstanceImpl(Native Method)
04-16 14:36:52.011: E/AndroidRuntime(1127): at java.lang.Class.newInstance(Class.java:1208)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
04-16 14:36:52.011: E/AndroidRuntime(1127): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
04-16 14:36:52.011: E/AndroidRuntime(1127): ... 11 more
04-16 14:36:56.491: I/Process(1127): Sending signal. PID: 1127 SIG: 9
Here is my Alarm.java:
package com.knight.;
import java.util.Random;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.widget.TextView;
import android.app.Service;
public class Alarm extends Service{
public void onStart(Intent intent, int startId) {
{
//MY CODE????
}
}
private TextView findViewById(int textview3) {
// TODO Auto-generated method stub
return null;
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
Here is the alarm setup java:
package com.knight.;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import java.util.Calendar;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import com.knight..R;
public class MainActivity extends Activity {
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Intent myIntent = new Intent(MainActivity.this, Alarm.class);
pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.set(Calendar.HOUR_OF_DAY, 0, pendingIntent);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
// time set up is unsuccessful in earlier versions
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Related
package com.example.cyber.sayac_say;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
int count;
Button btn;
SharedPreferences ayarlar = null;
RelativeLayout arkaplan;
Boolean ses_durumu, titresim_durumu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ayarlar = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
ayarlariYukle();
btn = (Button) findViewById(R.id.button);
arkaplan = (RelativeLayout) findViewById(R.id.rl);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
btn.setText(""+count);
}
});
}
hi , this is my codes. And when I used Shared Preferences so I have this error.But when I erase shared preferences lines so error is gone.
The problem is about Shared Prefenrences.I definitely sure.
What can I do before crazy:)?
03-22 14:16:23.972 6613-6613/com.example.cyber.sayac_say
E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cyber.sayac_say/com.example.cyber.sayac_say.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.cyber.sayac_say.MainActivity.ayarlariYukle(MainActivity.java:76)
at com.example.cyber.sayac_say.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
I follow this example about adding DatePicker to my app when using Android Studio.
import android.support.v4.app.DialogFragment;
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
The problem is that I can not even compile the project. Here is the error:
Error:(6, 42) java: cannot find symbol
symbol: class DialogFragment
Error:(7, 36) java: package DatePickerDialog does not exist
I tried import android.support.v4.app.DialogFragment;, but it does not exist. Also I set my project to API 11 as that is minimum api level for this control. Any ideas?
I got datePicker somehow working but now I have error when opening activity that contains datepicker:
07-16 18:17:36.274 1822-1822/mycalories.com.jalle.mycalories D/AndroidRuntime﹕ Shutting down VM
07-16 18:17:36.274 1822-1822/mycalories.com.jalle.mycalories W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4db2b20)
07-16 18:17:36.274 1822-1822/mycalories.com.jalle.mycalories E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: mycalories.com.jalle.mycalories, PID: 1822
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{mycalories.com.jalle.mycalories/mycalories.com.jalle.mycalories.MealsEditActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1884)
at mycalories.com.jalle.mycalories.MealsEditActivity.<init>(MealsEditActivity.java:23)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
and here is the activity
package mycalories.com.jalle.mycalories;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.parse.Parse;
import com.parse.ParseUser;
//import android.app.DialogFragment;
public class MealsEditActivity extends FragmentActivity implements mycalories.com.jalle.mycalories.DatePickerFragment.TheListener{
public final EditText txtDate=(EditText) findViewById(R.id.txtDate);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.meals_edit);
Button btnDate = (Button) findViewById(R.id.btnDate);
btnDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Utils.MyToast("btnDate", getApplicationContext());
DialogFragment newFragment = new com.jalle.mycalories.DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
}
);
}
public void returnDate(String date) {
// TODO Auto-generated method stub
txtDate.setText(date);
}
}
}
What is wrong with this activity ?
have you add android support library to build.gradle of your application ?
for example :
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:+'
}
then try to rebuild it.
For exception :
put
public final EditText txtDate=(EditText) findViewById(R.id.txtDate);
this line below
setContentView(R.layout.meals_edit);
My main Activity
package com.std.stunduizmainas;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import com.std.adapter.SSS;
import com.std.adapter.TabsPagerAdapter;
import com.std.stunduizmainas.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
public static String izm;
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
SharedPreferences settings;
Spinner skoluSp;
Spinner klasuSp;
Spinner izvSp;
static String info="";
static String rinfo="";
Dialog fb;
static String sd="";
static String rd="";
List<String> ssk = new ArrayList<String>();
List<String> person = new ArrayList<String>();
List<String> skl = new ArrayList<String>();
List<String> tabi = new ArrayList<String>();
String[] techerorstudent = new String[]{"Skolēns","Skolotājs"};
static String[] tabs = new String[4];
static String regid="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults();
getActionBar().setTitle("Stundu");
getActionBar().setSubtitle("izmainas");
settings = this.getSharedPreferences("settings", Context.MODE_PRIVATE);
getTab();
startService(new Intent(this,SSS.class));
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//tabs
for (String tab_name : tabi) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
and my TabsPagerAdapter
package com.std.adapter;
import com.std.stunduizmainas.KlasesIzm;
import com.std.stunduizmainas.RitdienasIzm;
import com.std.stunduizmainas.SkolasIzm;
import com.std.stunduizmainas.Paz;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new SkolasIzm();
case 1:
// Games fragment activity
return new RitdienasIzm();
case 2:
// Movies fragment activity
return new Paz();
case 3:
return new KlasesIzm();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 4;
}
}
});
}
I can't seem to be able to figure out what's the problem. I know that it is with this line of code viewPager.setAdapter(mAdapter);.
The log cat shows this as an error:
03-04 17:45:48.092: E/AndroidRuntime(1880): FATAL EXCEPTION: main
03-04 17:45:48.092: E/AndroidRuntime(1880): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.std.stunduizmainas/com.std.stunduizmainas.MainActivity}: java.lang.NullPointerException
03-04 17:45:48.092: E/AndroidRuntime(1880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-04 17:45:48.092: E/AndroidRuntime(1880): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-04 17:45:48.092: E/AndroidRuntime(1880): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-04 17:45:48.092: E/AndroidRuntime(1880): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-04 17:45:48.092: E/AndroidRuntime(1880): at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 17:45:48.092: E/AndroidRuntime(1880): at android.os.Looper.loop(Looper.java:137)
03-04 17:45:48.092: E/AndroidRuntime(1880): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-04 17:45:48.092: E/AndroidRuntime(1880): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 17:45:48.092: E/AndroidRuntime(1880): at java.lang.reflect.Method.invoke(Method.java:525)
03-04 17:45:48.092: E/AndroidRuntime(1880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-04 17:45:48.092: E/AndroidRuntime(1880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-04 17:45:48.092: E/AndroidRuntime(1880): at dalvik.system.NativeStart.main(Native Method)
03-04 17:45:48.092: E/AndroidRuntime(1880): Caused by: java.lang.NullPointerException
03-04 17:45:48.092: E/AndroidRuntime(1880): at com.std.stunduizmainas.MainActivity.onCreate(MainActivity.java:132)
03-04 17:45:48.092: E/AndroidRuntime(1880): at android.app.Activity.performCreate(Activity.java:5133)
03-04 17:45:48.092: E/AndroidRuntime(1880): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-04 17:45:48.092: E/AndroidRuntime(1880): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-04 17:45:48.092: E/AndroidRuntime(1880): ... 11 more
Could anybody please help me?
Looks like your viewPager is null.
Make sure the ID used in this line:
viewPager = (ViewPager) findViewById(R.id.pager);
is valid and references a viewpager in your activity_main.xml layout.
I'm new android programmer, and I don't know how to set new text of TextView after call by OnTabChangeListener. I've tried a lot of ways but each time was displayed weird error.
Edit: (Lastest code)
MainActivity.java:
package com.RobsoN;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TabHost tabHost = getTabHost();
Log.i("App","App initialization");
TabSpec start = tabHost.newTabSpec("Start");
start.setIndicator("Start", getResources().getDrawable(android.R.drawable.ic_menu_rotate));
Intent photosIntent = new Intent(this, Start.class);
start.setContent(photosIntent);
TabSpec settings = tabHost.newTabSpec("Ustawienia");
settings.setIndicator("Ustawienia", getResources().getDrawable(android.R.drawable.ic_menu_manage));
Intent songsIntent = new Intent(this, Settings.class);
settings.setContent(songsIntent);
TabSpec info = tabHost.newTabSpec("Informacje");
info.setIndicator("Informacje", getResources().getDrawable(android.R.drawable.ic_menu_help));
Intent videosIntent = new Intent(this, Other.class);
info.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(start);
tabHost.addTab(settings);
tabHost.addTab(info);
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
if(tabId.equals("Informacje"))
{
Other child = (Other) getTabHost().getChildAt(0).getContext(); //Error here: 1 java.lang.ClassCastException: com.RobsoN.MainActivity 2 com.RobsoN.MainActivity$1.onTabChanged(MainActivity.java:50)
child.refreshInfo();
}
}});
}
}
Other.java:
package com.RobsoN;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Other extends Activity {
Button button_update;
TextView stat_lastestappversion;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_other);
stat_lastestappversion = (TextView) findViewById(R.id.stat_lastestappversion);
button_update = (Button) findViewById(R.id.button_update);
button_update.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
}
});
}
public void refreshInfo()
{
stat_lastestappversion = (TextView) findViewById(R.id.stat_lastestappversion);
stat_lastestappversion.setText("TEST TEST 321");
}
}
Error:
05-05 13:00:59.845: E/AndroidRuntime(314): FATAL EXCEPTION: main
05-05 13:00:59.845: E/AndroidRuntime(314): java.lang.ClassCastException: com.RobsoN.MainActivity
05-05 13:00:59.845: E/AndroidRuntime(314): at com.RobsoN.MainActivity$1.onTabChanged(MainActivity.java:50)
05-05 13:00:59.845: E/AndroidRuntime(314): at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:356)
05-05 13:00:59.845: E/AndroidRuntime(314): at android.widget.TabHost.setCurrentTab(TabHost.java:341)
05-05 13:00:59.845: E/AndroidRuntime(314): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
05-05 13:00:59.845: E/AndroidRuntime(314): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
05-05 13:00:59.845: E/AndroidRuntime(314): at android.view.View.performClick(View.java:2408)
05-05 13:00:59.845: E/AndroidRuntime(314): at android.view.View$PerformClick.run(View.java:8816)
05-05 13:00:59.845: E/AndroidRuntime(314): at android.os.Handler.handleCallback(Handler.java:587)
05-05 13:00:59.845: E/AndroidRuntime(314): at android.os.Handler.dispatchMessage(Handler.java:92)
05-05 13:00:59.845: E/AndroidRuntime(314): at android.os.Looper.loop(Looper.java:123)
05-05 13:00:59.845: E/AndroidRuntime(314): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-05 13:00:59.845: E/AndroidRuntime(314): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 13:00:59.845: E/AndroidRuntime(314): at java.lang.reflect.Method.invoke(Method.java:521)
05-05 13:00:59.845: E/AndroidRuntime(314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-05 13:00:59.845: E/AndroidRuntime(314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-05 13:00:59.845: E/AndroidRuntime(314): at dalvik.system.NativeStart.main(Native Method)
as Luksprog says you can not use direct intialization of activity like you did in java. As far I can see you want to show a textview in a child activity of your tab host. To do so you can use the following
Other child = (Other) getTabHost().getChildAt(0).getContext();
child.refreshInfo();
N.B.
As you are new android programmer it is highly recommended to start with Fragments. Tab concept is depcrecated long time ago. Old programmers may still work on it to support their existing programs but as a new programmer you better avoid it.
UPDATE:
You can do another thing
getTabHost().setCurrentTab(tabindex);
Other child= (Other) this.getCurrentActivity();
child.refreshInfo();
05-05 14:32:25.210: ERROR/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.org.pc/com.org.pc.Login}: java.lang.NullPointerException
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:123)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:521)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): Caused by: java.lang.NullPointerException
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at com.org.pc.Login.loginDetails(Login.java:116)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at com.org.pc.Login.onCreate(Login.java:54)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
the code is :
package com.org.pc;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
public class Login extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Login","Reached");
Log.i("Login","View set");
Bitmap bitmap = (Bitmap) this.getIntent().getParcelableExtra("BitmapImage");
if(bitmap==null)
Log.v("Sdrawable","null");
else
Log.v("Sdrawable","not null");
loginDetails();
setContentView(R.layout.view);
ImageView viewBitmap = (ImageView)findViewById(R.id.bitmapview);
viewBitmap.setImageBitmap(bitmap);
Button login=(Button) findViewById(R.id.login);
SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
username = pref.getString(PREF_USERNAME, null);
password = pref.getString(PREF_PASSWORD, null);
if(username != null && password != null)
{
Log.v("username",username);
//user.setText(username);
//pass.setText(password);
}
login.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{
setContentView(R.layout.home);
}
});
}
public static final String PREFS_NAME = "PrefsFile";
private static final String PREF_USERNAME = "username";
private static final String PREF_PASSWORD = "password";
public String username = null;
public String password = null;
//EditText user = (EditText)findViewById(R.id.username);
//EditText pass = (EditText)findViewById(R.id.password);
public void loginDetails()
{
if (username == null || password == null)
{
// username = user.getText().toString();
// password = pass.getText().toString();
}
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://69.10.60.88:88/BZLogin.aspx");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "abdul"));
nameValuePairs.add(new BasicNameValuePair("password", "abdul"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
CheckBox cb = (CheckBox)findViewById(R.id.remember);
if ( cb.isChecked() )
{
getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
.edit()
.putString(PREF_USERNAME, username)
.putString(PREF_PASSWORD, password)
.commit();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
You're calling loginDetails() before you have inflated your layout to the activity. So, no ui elements exist. call setContentView() before you call findViewById().
You called CheckBox cb = (CheckBox)findViewById(R.id.remember) before setContentView(R.layout.view). That's may be why.
Call loginDetails() after setContentView(R.layout.view).