android - getActionBar() return null - android

i have a problem with getActionBar(), this problem happens when i'm trying to change view of action bar , i'm getting this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{h.ui01/h.ui01.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
at android.app.ActivityThread.access$1000(ActivityThread.java:198)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
at h.ui01.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6500)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
            at android.app.ActivityThread.access$1000(ActivityThread.java:198)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
My code is here :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.action_bar, null);
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
}
even when i try to hide action bar or change color of the action bar , i saw similar error.

Related

ParseTwitterUtils initialize throws NullPointerException

I am getting a NullPointerException from calling
ParseTwitterUtils.initialize(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
Here is the stack trace
java.lang.RuntimeException: Unable to create application com.myapp.app.android.Application: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5015)
at android.app.ActivityThread.access$1600(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference
at com.parse.Parse.getParseDir(Parse.java:313)
at com.parse.ParseCorePlugins.getCurrentUserController(ParseCorePlugins.java:124)
at com.parse.ParseUser.getCurrentUserController(ParseUser.java:56)
at com.parse.ParseUser.getCurrentUser(ParseUser.java:892)
at com.parse.ParseUser.getCurrentUser(ParseUser.java:879)
at com.parse.ParseUser.registerAuthenticationProvider(ParseUser.java:1125)
at com.parse.ParseTwitterUtils.getAuthenticationProvider(ParseTwitterUtils.java:20)
at com.parse.ParseTwitterUtils.initialize(ParseTwitterUtils.java:50)
at com.myapp.app.android.Application.onCreate(Application.java:42)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1020)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5012)
            at android.app.ActivityThread.access$1600(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5837)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
My app used to work before I added the line ParseTwitterUtils.initialize(...). Here is the onCreate method of my Application class
public void onCreate(){
super.onCreate();
registerDatabaseTables();
ParseTwitterUtils.initialize(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
Parse.enableLocalDatastore(this);
Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
}
Unfortunately the instructions on parse.com do not say this, but you have to initialize Parse before you initialize Twitter. This is not obvious since you are required to do so much before you initialize parse. So anyway change your code to this:
public void onCreate(){
super.onCreate();
registerDatabaseTables();
Parse.enableLocalDatastore(this);
Parse.initialize(this, PARSE_APPLICATION_ID, PARSE_CLIENT_KEY);
ParseTwitterUtils.initialize(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
}

Getting NullPointerException while displaying ListView

I was trying to make a ListView of reminder notes. But when I try to save it and display it in a ListView, app crashes with this error/exception. saveNote method is called when you press save button. Any help is appreciated.
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case R.id.add_reminder : {
et = (EditText)findViewById(R.id.new_note);
et.setVisibility(View.VISIBLE);
note = et.getText().toString();
i++;
Button save_button = (Button) findViewById(R.id.button_save);
save_button.setVisibility(View.VISIBLE);
}
default : return super.onOptionsItemSelected(item);
}
}
public void saveNote(View view) {
String[] notes = new String[20];
notes[i] = note;
listView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,notes);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Clicked item index
int itemPosition = position;
//Clicked item value
String itemNote = (String) listView.getItemAtPosition(position);
//Alert
Toast.makeText(getApplicationContext(), "Position :" + itemPosition + " ListItem : " + itemNote, Toast.LENGTH_LONG).show();
}
});
Logcat
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.cokosofts.pramith.reminder.MainActivity.saveNote(MainActivity.java:69)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4015)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
The main cause which I found from the Logs is ListView is null. Please check one more time if the listView is empty or not? Also what is notes[i] = note ??
I think in each Save button click you are finding ListView and the setting Adapter each time. This is not a good approach from my point of view. You should set the adapter very first time and next time just update your adapter data and call notifyDataSetChanged();
Debug and find out why your ListView was not found on your layout (was null).
Problem is with following line:
listView = (ListView) findViewById(R.id.list);
Please check whether your xml has ListView widget with id as 'list' .
If id is different in XML it will result in a NULL object reference. When you call
listView.setAdapter(adapter)
It will give a NULL pointer exception.

How to get image name from imageview

I have an imageview which has an image loaded in it. This way
// create bitmap from resource
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.profile_blank_xlarge);
// set circle bitmap
ImageView mImage = (ImageView) layout.findViewById(R.id.image);
mImage.setImageBitmap(getCircleBitmap(bm));
I want to be able to find out the name of the image in the ImageView once the application is run.
I tried doing mImage.getTag() to get the name of the image in the ImageView but it crashes my app.
What is the correct way to get the name of the image in a ImageView
Crash Details
--------- beginning of crash
07-02 13:11:37.259 2096-2096/com.example.kmi_dev.fbloginsample E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.kmi_dev.fbloginsample, PID: 2096
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kmi_dev.fbloginsample/com.example.kmi_dev.fbloginsample.MyActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.kmi_dev.fbloginsample.MyAccount.onCreateView(MyAccount.java:115)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:834)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.Activity.performStart(Activity.java:6005)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

AudioManager setStreamMute() Null Pointer Exception

I am getting Null Pointer Exception on line 219: amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);. It was not happening before, today i saw it first time, i am using Android 5.0. Code mutes notification sound when activity starts and unmutes when it is destroyed.
Code:
private AudioManager amanager;
#Override
protected void onCreate(Bundle savedInstanceState) {
amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
}
#Override
protected void onResume() {
super.onResume();
if(amanager!=null) {
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
}
}
#Override
protected void onPause() {
if(amanager!=null) {
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
}
super.onPause();
}
Error:
java.lang.RuntimeException: Unable to resume activity {com.jemshit.itu/com.jemshit.nfcAttendance.ClassActivity}: java.lang.NullPointerException: Null pointer exception during instruction 'invoke-static {v0}, android.os.IBinder android.media.AudioService$VolumeStreamState$VolumeDeathHandler.access$5300(android.media.AudioService$VolumeStreamState$VolumeDeathHandler) // method#11492'
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3346)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Null pointer exception during instruction 'invoke-static {v0}, android.os.IBinder android.media.AudioService$VolumeStreamState$VolumeDeathHandler.access$5300(android.media.AudioService$VolumeStreamState$VolumeDeathHandler) // method#11492'
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1493)
at android.media.IAudioService$Stub$Proxy.setStreamMute(IAudioService.java:992)
at android.media.AudioManager.setStreamMute(AudioManager.java:1466)
at com.jemshit.nfcAttendance.ClassActivity.onResume(ClassActivity.java:219)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1255)
at android.app.Activity.performResume(Activity.java:6338)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3335)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
It's an Android bug it's not your code. Put a try/catch block in your code to avoid the crash.

Attempt to invoke virtual method

I am trying to make a simple timer using the Timer and TimerTask classes. I keep getting the following error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
Here is my code:
package com.austinheitmann.stopwatch;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends ActionBarActivity {
TextView timerf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerf = (TextView) findViewById(R.id.timer);
}
int i=0;
TimerTask time = new TimerTask() {
public void run() {
i++;
int k = i/60;
int j = i%60;
if (j>9) {
timerf.setText("seconds remaining: " + k + ":" + j);
}else {
timerf.setText("seconds remaining: " + k + ":0" + j);
}
Log.d("Uhh", "Sec:" + i);
}
};
Timer timers = new Timer();
public void startB(View view) {
timers.schedule(time, 100000000, 1000);
}
#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);
}
}
}
Here is the xml file(layout):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0:00"
android:id="#+id/timer"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start/Pause"
android:id="#+id/start"
android:layout_below="#+id/timer"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="119dp"
android:onClick="startB"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop/Reset"
android:id="#+id/stop"
android:layout_alignBottom="#+id/start"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Every solution I have seen to this error has to do with the layout but I am not altering that in anyway. I have moved to the TimerTask in the startB class also and get the same error. How can I fix this error?
Here's the entire error I get from one run after clearing the logcat (I put ** where the error occurs at):
01-16 12:27:21.008 2455-2455/com.austinheitmann.stopwatch I/art﹕ Not late-enabling -Xcheck:jni (already on)
01-16 12:27:21.119 2455-2455/com.austinheitmann.stopwatch D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
01-16 12:27:21.123 2455-2455/com.austinheitmann.stopwatch E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.austinheitmann.stopwatch, PID: 2455
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.austinheitmann.stopwatch/com.austinheitmann.stopwatch.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2071)
at com.austinheitmann.stopwatch.MainActivity.<init>(MainActivity.java:25)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-16 12:44:57.778 2582-2582/com.austinheitmann.stopwatch D/AndroidRuntime﹕ Shutting down VM
01-16 12:44:57.778 2582-2582/com.austinheitmann.stopwatch E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.austinheitmann.stopwatch, PID: 2582
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.austinheitmann.stopwatch/com.austinheitmann.stopwatch.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2071)
at com.austinheitmann.stopwatch.MainActivity.<init>(MainActivity.java:25)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-16 12:46:25.059 2641-2641/com.austinheitmann.stopwatch D/AndroidRuntime﹕ Shutting down VM
01-16 12:46:25.059 2641-2641/com.austinheitmann.stopwatch E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.austinheitmann.stopwatch, PID: 2641
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.austinheitmann.stopwatch/com.austinheitmann.stopwatch.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2071)
at com.austinheitmann.stopwatch.MainActivity.<init>(MainActivity.java:25)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-16 12:47:50.630 2702-2702/com.austinheitmann.stopwatch D/AndroidRuntime﹕ Shutting down VM
01-16 12:47:50.630 2702-2702/com.austinheitmann.stopwatch E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.austinheitmann.stopwatch, PID: 2702
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.austinheitmann.stopwatch/com.austinheitmann.stopwatch.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
**Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference**
at android.app.Activity.findViewById(Activity.java:2071)
at com.austinheitmann.stopwatch.MainActivity.<init>(MainActivity.java:25)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
You are trying to findViewById before the activity's context is ready.
If you want to access timerf in other methods too, create a class member but make the initialization in the onCreate after setContentView:
TextView timerf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerf = (TextView) findViewById(R.id.timer);
}
Update:
I didn't notice the other class member variables you're instantiating. They are referencing timerf which null.
You need to initialize them in the onCreate too. Here's the full code that should work:
package com.austinheitmann.stopwatch;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends ActionBarActivity {
Timer timers = new Timer();
TextView timerf;
TimerTask time;
int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerf = (TextView) findViewById(R.id.timer);
time = new TimerTask() {
public void run() {
i++;
int k = i/60;
int j = i%60;
if (j>9) {
timerf.setText("seconds remaining: " + k + ":" + j);
}else {
timerf.setText("seconds remaining: " + k + ":0" + j);
}
Log.d("Uhh", "Sec:" + i);
}
};
}
public void startB(View view) {
timers.schedule(time, 100000000, 1000);
}
#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);
}
}
First of you use the constructor public MainActivity()and
Timer timer; and long startTime; long stopTime Declare Gobble variable
public MainActivity() {
timer = new Timer();
startTime = System.currentTimeMillis();
stopTime = 0;
}

Categories

Resources