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;
}
Related
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.
New to Android. Not sure how many method I am supposed to upload, so I will upload the just method with the error.
I am following a book Android Application Development for Dummies 3rd edition, but getting an error even though I checked for code discrepancies.
Here is the method.
package com.dan190.tasks.activity;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toolbar;
import com.dan190.tasks.R;
import com.dan190.tasks.fragment.TaskEditFragment;
public class TaskEditActivity extends Activity {
public static final String EXTRA_TASKID = "taskId";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_edit);
setActionBar((Toolbar) findViewById(R.id.toolbar));
long id = getIntent().getLongExtra(TaskEditActivity.EXTRA_TASKID, 0L);
Fragment fragment = TaskEditFragment.newInstance(id);
String fragment_tag = TaskEditFragment.DEFAULT_FRAGMENT_TAG;
/** This part below is where the bug happens
** If I comment this part out, app still runs
**/
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(
R.id.container,
fragment,
fragment_tag).commit();
}
}
}
Here is the error
07-17 11:52:47.110 29969-29969/com.dan190.tasks E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.dan190.tasks, PID: 29969
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dan190.tasks/com.dan190.tasks.activity.TaskEditActivity}: java.lang.IllegalArgumentException: Target must not be null.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
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.IllegalArgumentException: Target must not be null.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:553)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:536)
at com.dan190.tasks.fragment.TaskEditFragment.onCreate(TaskEditFragment.java:56)
at android.app.Fragment.performCreate(Fragment.java:2031)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:833)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.Activity.performStart(Activity.java:5952)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
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)
Thanks
I am having a CustomRecyclerViewAdapter.class file in which I have implemented the below method.
public void onBindViewHolder(RecyclerViewHolder viewHolder, int position)
{
viewHolder.title.setText(mData.get(position).text);
//viewHolder.icon.setBackgroundColor(Color.parseColor(mData.get(position).color));
viewHolder.setClickListener(new RecyclerViewHolder.ClickListener(){
#Override
public void onClick(View v, int position, boolean isLongClick) {
if (isLongClick) {
// View v at position pos is long-clicked.
Toast.makeText(v.getContext(), "Hey you just hit item" + position, Toast.LENGTH_SHORT).show();
}else {
// View v at position pos is clicked.
//how to start a new activity here
Toast.makeText(v.getContext(),"Hey you just hit item" + position,Toast.LENGTH_SHORT).show();
}
}
});
}
So how i can start the new activity in the else block above.
Group.class
public class Group extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//line no. 16 which is indicated in logcat
setContentView(R.layout.group);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
This is my error logcat.....
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.trueblueoperator.samplerecyclerview/com.trueblueoperator.samplerecyclerview.Group}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
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.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.trueblueoperator.samplerecyclerview.Group.onCreate(Group.java:16)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
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)
Thanks for your time.
You can use any View to it.
getContext() method provides almost activity methods because Activity extends Context.
v.getContext().startActivity(intent);
I have a image button to delete that particular row inside a listview in a fragment.When I run the activity from main activity it gives NPE.
Here is the logcat output
03-10 12:29:36.647 2525-2525/com.restaurantmgmt.dcode1.restaurantms E/libprocessgroup﹕ failed to make and chown /acct/uid_10007: Read-only file system
03-10 12:29:50.351 2525-2525/com.restaurantmgmt.dcode1.restaurantms E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.restaurantmgmt.dcode1.restaurantms, PID: 2525
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.restaurantmgmt.dcode1.restaurantms/com.restaurantmgmt.dcode1.restaurantms.menu}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
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: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.restaurantmgmt.dcode1.restaurantms.menu.onCreate(menu.java:18)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
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 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.restaurantmgmt.dcode1.restaurantms.SidePanelFragment.onCreateView(SidePanelFragment.java:45)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1045)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1147)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2116)
at android.app.Activity.onCreateView(Activity.java:5282)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.restaurantmgmt.dcode1.restaurantms.menu.onCreate(menu.java:18)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
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)
Here is the relevant code from the SidePanelActivity
public class SidePanelFragment extends android.app.Fragment implements View.OnClickListener {
ListView list1;
Items addedItems=null;
ImageButton btnDelete;
Items[] addedItms=new Items[100];
//Items[] addedItms=null;
ArrayList<Items> itemsArray=null;
ItemsAdapter1 adapter=null;
int countSideList=0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_side_panel,container,false);
list1=(ListView)view.findViewById(R.id.listViewSide);
btnDelete=(ImageButton)view.findViewById(R.id.btndelete);
itemsArray= new ArrayList<Items>();
btnDelete.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
Toast.makeText(getActivity(),"btndelete clicked",Toast.LENGTH_LONG);
}
}
And here is the activity which has the fragment
public class menu extends ActionBarActivity implements OnAddItemPressed{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
int screenOrien=getResources().getConfiguration().orientation;
if(screenOrien== Configuration.ORIENTATION_PORTRAIT) {
hideSidePanel();
}
else
{
}
}
private void hideSidePanel() {
View side=findViewById(R.id.side_panel);
if(side.getVisibility()==View.VISIBLE)
{
side.setVisibility(View.GONE);
}
}
#Override
public void onAddItemPressed(Items item){
FragmentManager manager= getFragmentManager();
SidePanelFragment sideObj= (SidePanelFragment) manager.findFragmentById(R.id.side_panel);
sideObj.getAddedItems(item);
}
}
I have set a breakpoint at btnDelete.setOnClickListener(this);
and btnDelete is null in the debug window at that breakpoint.
Try this way :
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btndelete:
Toast.makeText(getActivity(),"btndelete clicked",Toast.LENGTH_LONG);
break;
}
Also try to replace your implements View.OnClickListener to implements OnClickListener
Make sure this btndelete id is on your fragment_side_panel.xml
I've been playing around with Android Studio and I'm trying to implement a simple chronometer with the Chronometer widget. First I tried this code:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Chronometer;
import android.view.View;
public class MainActivity extends AppCompatActivity {
Chronometer chrono;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
chrono = (Chronometer) findViewById(R.id.chronometer);
setContentView(R.layout.activity_main);
}
public void startChrono(View view) {
chrono.start();
}
public void stopChrono(View view) {
chrono.stop();
}
}
But when I used it, whenever I pressed the Start button, it would crash the app. Then I found this implementation online:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Chronometer;
import android.view.View;
public class MainActivity extends AppCompatActivity {
Chronometer chrono;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startChrono(View view) {
((Chronometer) findViewById(R.id.chronometer)).start();
}
public void stopChrono(View view) {
((Chronometer) findViewById(R.id.chronometer)).start();
}
}
This one works though, can anyone point out the differences between creating the Chronometer object in onCreate as opposed to what the second block of code does?
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.Chronometer.start()' on a null object reference
at com.example.mykongee.testchronometer.MainActivity.startChrono(MainActivity.java:22)
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)
Your issue is here:
chrono = (Chronometer) findViewById(R.id.chronometer);
setContentView(R.layout.activity_main);
You can not call findViewById before you call setContentLayout, because there is no view to find.
You app would have crashed with a NullPointerException because chrono was null.
In your first example, try swapping these two lines, and it will work the same as the second
setContentView(R.layout.activity_main);
chrono = (Chronometer) findViewById(R.id.chronometer);
In the future, do not simply say it didnt work, Android provides an amazong LogCat that tells you pretty much EXACTLY what the problem is.
If you can not decode the error yourself, then post the stacktrace provided by logcat.
In the first one you are first trying to get a reference to chrono, then you are setting your layout. The thing is, you can't get a reference without setting the layout first (can't find what isn't there...)
false:
chrono = (Chronometer) findViewById(R.id.chronometer);
setContentView(R.layout.activity_main);
correct:
setContentView(R.layout.activity_main);
chrono = (Chronometer) findViewById(R.id.chronometer);
In the second example you are setting the layout, then on button press you find the reference, so everything is ok.