SharedPreferences in Android AndroidRunTime Exception and NullPointerException - java

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) 

Related

Unable to start activity ComponentInfo (LOGCAT)

I'm a new android programmer and recently, I'm getting this error in the logcat:
03-22 15:33:49.395 13639-13639/com.abaco.awser E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.abaco.awser, PID: 13639
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abaco.awser/com.abaco.awser.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
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:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.abaco.awser.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5447)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
at android.app.ActivityThread.access$800(ActivityThread.java:166) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5584) 
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:1268) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
at dalvik.system.NativeStart.main(Native Method) 
Here's my MainActivity.java:
package com.abaco.awser;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
public void onBackPressed() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView myWebView = (WebView) findViewById(R.id.activity_main_webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://primopizza.com.br/eloja");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
setContentView(R.layout.activity_main);
}
}
And here's my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abaco.abawser.MainActivity">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Can you please help me solving this error? I already tried everything.
You are getting NPE because your myWebView is null
place this
WebView myWebView = (WebView)findViewById(R.id.activity_main_webview);
after
setContentView(R.layout.activity_main);
Because you can actually retrive your views only after setting content View

error java app Android

I have a problem and I do not understand the reason why. I'm working on Android Studio on Linux. The bitmap size is 10x10 pixels. I searched in google, but it gave me nothing. I would be grateful if someone could help me.
This problem is something with this:
"Bitmap suweren = BitmapFactory.decodeResource(getResources(), R.drawable.suweren);"
and this is error log:
"12-30 07:16:36.125 9336-9336/com.example.szczesciarz.sawa E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.szczesciarz.sawa, PID: 9336
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.szczesciarz.sawa/com.example.szczesciarz.sawa.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
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:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:98)
at android.support.v7.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:91)
at android.support.v7.app.ToolbarActionBar.<init>(ToolbarActionBar.java:73)
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:205)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
at com.example.szczesciarz.sawa.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
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:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
12-30 07:21:36.565 9336-9336/com.example.szczesciarz.sawa I/Process: Sending signal. PID: 9336 SIG: 9"
Main Activity_code:
package com.example.szczesciarz.sawa;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(this));
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
GameView code:
package com.example.szczesciarz.sawa;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
class GameView extends View{
public GameView(Context context){
super(context);
}
#Override
public void onDraw(Canvas canvas ){
Bitmap suweren = BitmapFactory.decodeResource(Context.getResources(), R.drawable.suweren);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(suweren, 0, 0, null);
}
}
Thanks):-
Toolbar cannot be found :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
So when you call
setSupportActionBar(toolbar);
You have a NullPointerException at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
try to remove setSupportActionBar(toolbar)
Make sure that your layout file contains toolbar or you have included the toolbar layout.
This exception occurs when your layout file does not contain the id, so android is unable to get the view from that id and returns null when you call findViewById(R.id.toolbar);

Issue with adding DatePickerDialog to project

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);

Using/understanding Alarm manager force close [Android]

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;
}
}

ViewPager nullPointerException error

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.

Categories