I have two fragments but the second fragment can not open when clicked
this is the drug fragment the second fragment cant open because of the spinner i
placed on the first screen and i dont know how to fix it please help
I have two fragments but the second fragment can not open when clicked
this is the drug fragment the second fragment cant open because of the spinner i
placed on the first screen and i dont know how to fix it please help
<?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"
>
<Button
android:id="#+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="51dp"
android:layout_marginStart="63dp"
android:text="Save" />
<Button
android:id="#+id/buttonCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/btnSave"
android:layout_marginEnd="76dp"
android:text="Cancel" />
<EditText
android:id="#+id/textView4"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="221dp"
android:layout_marginStart="36dp"
android:hint="Drug" />
<Spinner
android:id="#+id/SpinnerTime"
android:layout_width="133dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/btnSave"
android:layout_marginBottom="135dp" />
<Spinner
android:id="#+id/spinnerFrequency"
android:layout_width="93dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="175dp"
android:layout_marginStart="86dp" />
<Spinner
android:id="#+id/SpinnerQty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="227dp"
android:layout_marginEnd="63dp" />
</RelativeLayout>
this is the appointment fragment code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Appointment"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
import android.graphics.Color;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
appBarLayout = (AppBarLayout)findViewById(R.id.bar);
viewPager = (ViewPager)findViewById(R.id.viewpager);
Spinner spin1 = (Spinner)findViewById(R.id.spinnerFrequency);
Spinner spin2 = (Spinner)findViewById(R.id.SpinnerTime);
Spinner spin3 = (Spinner)findViewById(R.id.SpinnerQty);
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.AddFragment(new Drugfragment(),"Drug");
adapter.AddFragment(new Appointmentfragment(),"Appointment");
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.Qty,android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> arrayAdapter2 = ArrayAdapter.createFromResource(this, R.array.time,android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> arrayAdapter3 = ArrayAdapter.createFromResource(this, R.array.frequency,android.R.layout.simple_spinner_item);
spin1.setAdapter(arrayAdapter);
spin2.setAdapter(arrayAdapter2);
spin3.setAdapter(arrayAdapter3);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
arrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
arrayAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setOnItemSelectedListener(this);
spin2.setOnItemSelectedListener(this);
spin3.setOnItemSelectedListener(this);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(),text,Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
logcat error when i dont add the include layout
05-18 10:26:34.316 5868-5868/? D/dalvikvm: Not late-enabling CheckJNI (already on)
05-18 10:26:34.326 5868-5874/? E/jdwp: Failed sending reply to debugger: Broken pipe
05-18 10:26:34.326 5868-5874/? D/dalvikvm: Debugger has detached; object registry had 1 entries
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.view.Window$Callback.onPointerCaptureChanged, referenced from method android.support.v7.view.WindowCallbackWrapper.onPointerCaptureChanged
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve interface method 20907: Landroid/view/Window$Callback;.onPointerCaptureChanged (Z)V
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve interface method 20909: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve interface method 20911: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve interface method 20915: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 866: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 888: Landroid/content/res/TypedArray;.getType (I)I
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.createDeviceProtectedStorageContext, referenced from method android.support.v4.content.ContextCompat.createDeviceProtectedStorageContext
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 644: Landroid/content/Context;.createDeviceProtectedStorageContext ()Landroid/content/Context;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getCodeCacheDir, referenced from method android.support.v4.content.ContextCompat.getCodeCacheDir
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 650: Landroid/content/Context;.getCodeCacheDir ()Ljava/io/File;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getColor, referenced from method android.support.v4.content.ContextCompat.getColor
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 651: Landroid/content/Context;.getColor (I)I
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v4.content.ContextCompat.getColorStateList
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 652: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getDataDir, referenced from method android.support.v4.content.ContextCompat.getDataDir
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 654: Landroid/content/Context;.getDataDir ()Ljava/io/File;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getDrawable, referenced from method android.support.v4.content.ContextCompat.getDrawable
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 655: Landroid/content/Context;.getDrawable (I)Landroid/graphics/drawable/Drawable;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getExternalCacheDirs, referenced from method android.support.v4.content.ContextCompat.getExternalCacheDirs
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 657: Landroid/content/Context;.getExternalCacheDirs ()[Ljava/io/File;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getExternalFilesDirs, referenced from method android.support.v4.content.ContextCompat.getExternalFilesDirs
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 659: Landroid/content/Context;.getExternalFilesDirs (Ljava/lang/String;)[Ljava/io/File;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getNoBackupFilesDir, referenced from method android.support.v4.content.ContextCompat.getNoBackupFilesDir
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 663: Landroid/content/Context;.getNoBackupFilesDir ()Ljava/io/File;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.getObbDirs, referenced from method android.support.v4.content.ContextCompat.getObbDirs
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 665: Landroid/content/Context;.getObbDirs ()[Ljava/io/File;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.isDeviceProtectedStorage, referenced from method android.support.v4.content.ContextCompat.isDeviceProtectedStorage
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 676: Landroid/content/Context;.isDeviceProtectedStorage ()Z
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.336 5868-5868/? I/dalvikvm: Could not find method android.content.Context.startForegroundService, referenced from method android.support.v4.content.ContextCompat.startForegroundService
05-18 10:26:34.336 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 691: Landroid/content/Context;.startForegroundService (Landroid/content/Intent;)Landroid/content/ComponentName;
05-18 10:26:34.336 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
05-18 10:26:34.346 5868-5868/? I/dalvikvm: Could not find method android.support.design.widget.AppBarLayout.setKeyboardNavigationCluster, referenced from method android.support.design.widget.AppBarLayout.<init>
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 3160: Landroid/support/design/widget/AppBarLayout;.setKeyboardNavigationCluster (Z)V
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x006e
05-18 10:26:34.346 5868-5868/? I/dalvikvm: Could not find method android.support.design.widget.AppBarLayout.setTouchscreenBlocksFocus, referenced from method android.support.design.widget.AppBarLayout.<init>
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 3163: Landroid/support/design/widget/AppBarLayout;.setTouchscreenBlocksFocus (Z)V
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x007f
05-18 10:26:34.346 5868-5868/? I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 829: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
05-18 10:26:34.346 5868-5868/? I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve virtual method 831: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
05-18 10:26:34.346 5868-5868/? E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve instanceof 239 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper;
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c
05-18 10:26:34.346 5868-5868/? I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init>
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve direct method 21530: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000
05-18 10:26:34.346 5868-5868/? I/dalvikvm: Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.design.widget.AppBarLayout$LayoutParams.<init>
05-18 10:26:34.346 5868-5868/? W/dalvikvm: VFY: unable to resolve direct method 21530: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
05-18 10:26:34.346 5868-5868/? D/dalvikvm: VFY: replacing opcode 0x70 at 0x0000
05-18 10:26:34.346 5868-5871/? D/dalvikvm: GC_CONCURRENT freed 168K, 25% free 2708K/3580K, paused 0ms+0ms, total 2ms
05-18 10:26:34.346 5868-5868/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 1ms
05-18 10:26:34.356 5868-5868/? D/dalvikvm: GC_FOR_ALLOC freed 100K, 26% free 2829K/3804K, paused 1ms, total 1ms
05-18 10:26:34.356 5868-5868/? I/dalvikvm-heap: Grow heap (frag case) to 4.521MB for 1127532-byte allocation
05-18 10:26:34.356 5868-5877/? D/dalvikvm: GC_FOR_ALLOC freed <1K, 20% free 3930K/4908K, paused 2ms, total 2ms
05-18 10:26:34.366 5868-5871/? D/dalvikvm: GC_CONCURRENT freed <1K, 20% free 3930K/4908K, paused 2ms+0ms, total 4ms
05-18 10:26:34.366 5868-5868/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 2ms
05-18 10:26:34.366 5868-5868/? I/dalvikvm-heap: Grow heap (frag case) to 6.940MB for 2536932-byte allocation
05-18 10:26:34.366 5868-5871/? D/dalvikvm: GC_CONCURRENT freed 0K, 14% free 6408K/7388K, paused 1ms+0ms, total 5ms
05-18 10:26:34.376 5868-5868/? D/AndroidRuntime: Shutting down VM
05-18 10:26:34.376 5868-5868/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4fa4678)
05-18 10:26:34.376 5868-5868/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.koichisato.medtime/com.example.koichisato.medtime.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.koichisato.medtime.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Drugfragment
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Spinner;
public class Drugfragment extends Fragment {
View view;
public Drugfragment(){
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.drugfragment,container,false);
return view;
}
}
appointmentfragment
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Appointmentfragment extends Fragment {
View view;
public Appointmentfragment(){
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.appointmentfragment,container,false);
return view;
}
}
Adapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class Adapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentListTitles = new ArrayList<>();
public Adapter(FragmentManager fn){
super(fn);
}
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentListTitles.size();
}
#Override
public CharSequence getPageTitle(int position) {
return fragmentListTitles.get(position);
}
public void AddFragment(Fragment fragment, String Title){
fragmentList.add(fragment);
fragmentListTitles.add(Title);
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/bar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:elevation="0dp"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#be0a16"
app:tabGravity="fill"
app:tabIndicatorColor="#color/colorTabindi"
app:tabMode="fixed"
app:tabTextColor="#color/colorText">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewpager">
</android.support.v4.view.ViewPager>
</LinearLayout>
Strings
<resources>
<string name="app_name">MedTime</string>
<string-array name="frequency">
<item>x1</item>
<item>x2</item>
<item>x3</item>
<item>x4</item>
<item>x5</item>
</string-array>
<string-array name="time">
<item>12:00AM</item>
<item>11:00AM</item>
<item>10:00AM</item>
<item>09:00AM</item>
<item>08:00AM</item>
<item>07:00AM</item>
<item>06:00AM</item>
<item>05:00AM</item>
<item>04:00AM</item>
<item>03:00AM</item>
<item>02:00AM</item>
<item>01:00AM</item>
<item>12:00PM</item>
<item>11:00PM</item>
<item>10:00PM</item>
<item>09:00PM</item>
<item>08:00PM</item>
<item>07:00PM</item>
<item>06:00PM</item>
<item>05:00PM</item>
<item>04:00PM</item>
<item>03:00PM</item>
<item>02:00PM</item>
<item>01:00PM</item>
</string-array>
<string-array name="Qty">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
</string-array>
</resources>
Change your drugfragment to
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class Drugfragment extends Fragment {
View view;
public Drugfragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.drugfragment, container, false);
final Spinner spin1 = view.findViewById(R.id.spinnerFrequency);
final Spinner spin2 = view.findViewById(R.id.SpinnerTime);
final Spinner spin3 = view.findViewById(R.id.SpinnerQty);
Button save = view.findViewById(R.id.btnSave);
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(getContext(), R.array.Qty, android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<CharSequence> arrayAdapter2 = ArrayAdapter.createFromResource(getContext(), R.array.time, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> arrayAdapter3 = ArrayAdapter.createFromResource(getContext(), R.array.frequency, android.R.layout.simple_spinner_item);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
arrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
arrayAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(arrayAdapter);
spin2.setAdapter(arrayAdapter2);
spin3.setAdapter(arrayAdapter3);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = spin1.getSelectedItem().toString();
String text2 = spin2.getSelectedItem().toString();
String text3 = spin3.getSelectedItem().toString();
Toast.makeText(getContext(), "text1: " + text + "\ntext2: " + text2 + "\ntext3: " + text3, Toast.LENGTH_SHORT).show();
}
});
return view;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
and your main activity to
import android.graphics.Color;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private ViewPager viewPager;
Spinner spin1;
Spinner spin2;
Spinner spin3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tablayout);
appBarLayout = findViewById(R.id.bar);
viewPager = findViewById(R.id.viewpager);
spin1 = findViewById(R.id.spinnerFrequency);
spin2 = findViewById(R.id.SpinnerTime);
spin3 = findViewById(R.id.SpinnerQty);
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.AddFragment(new Drugfragment(), "Drug");
adapter.AddFragment(new appointmentfragment(), "Appointment");
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.Qty, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> arrayAdapter2 = ArrayAdapter.createFromResource(this, R.array.time, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> arrayAdapter3 = ArrayAdapter.createFromResource(this, R.array.frequency, android.R.layout.simple_spinner_item);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
arrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
arrayAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
and do the same for appointmentfragment, Its the same
Related
i'm create recycler view to display data, after that I want to pass
position of the data to the another activity, but when I use the loge for check if position is pass or not I can't found the position in logcat.
In MainActivity I add static list for Recipe model and I create method to return this list:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ProgressBar progressBar;
private LinearLayoutManager mLayoutManager;
private ArrayList<Model> list;
private RecyclerViewAdapter adapter;
private static List<Recipe>mListrecipe;
String url = "https://d17h27t6h515a5.cloudfront.net/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
mLayoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(mLayoutManager);
/// create a list--
list = new ArrayList<>();
/// get the list from json file
getRetrofitArray();
adapter = new RecyclerViewAdapter( list, MainActivity.this);
// set adapter to recyclerview
recyclerView.setAdapter(adapter);
}
public void getRetrofitArray(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class);
Call<List<Recipe>> call = service.getRecipeDetails();
call.enqueue(new Callback<List<Recipe>>() {
#Override
public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) {
Log.e("main ", " retrofit response "+ response.body().toString());
mListrecipe=response.body();
for (int i=0; i<response.body().size();i++){
Log.e("main ", " name "+ response.body().get(i).getName() + " serving "+
response.body().get(i).getServings());
list.add( new Model( Model.IMAGE_TYPE,response.body(), response.body().get(i).getName() ,
" Serving " +response.body().get(i).getServings() ) );
}
adapter.notifyDataSetChanged();
}
#Override
public void onFailure(Call<List<Recipe>> call, Throwable t) {
Log.e("main ", " retrofit error "+ t.toString());
}
});
}
public static List<Recipe>getList(){
return mListrecipe;
}
}
I add putExtra with key value to pass position
public class RecyclerViewAdapter extends RecyclerView.Adapter {
private ArrayList<Model> dataset;
private Context mContext;
public RecyclerViewAdapter(ArrayList<Model> mlist, Context context) {
this.dataset = mlist;
this.mContext = context;
}
public static class ImageTypeViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView title, subtitle;
public ImageTypeViewHolder(View itemView) {
super(itemView);
this.title = (TextView) itemView.findViewById(R.id.title);
this.subtitle = (TextView) itemView.findViewById(R.id.subtitle);
this.imageView=(ImageView)itemView.findViewById(R.id.imageview);
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from( parent.getContext()).inflate(R.layout.itemlist, parent, false);
return new ImageTypeViewHolder(view) ;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
Model object = dataset.get(position);
( (ImageTypeViewHolder) holder).title.setText( object.title );
( (ImageTypeViewHolder) holder).subtitle.setText( object.subtitle );
/// dataset.get(position)
( (ImageTypeViewHolder) holder).title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(mContext,SelectReceipe.class);
intent.putExtra("itempostion",position);
mContext.startActivity(intent);
}
});
( (ImageTypeViewHolder) holder).subtitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(mContext,SelectReceipe.class);
intent.putExtra("itempostion",position);
mContext.startActivity(intent);
}
});
( (ImageTypeViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(mContext,SelectReceipe.class);
intent.putExtra("itempostion",position);
mContext.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return dataset.size() ;
}
}
this is 2 activity I pass position but i can't find value in logcat
public class SelectReceipe extends AppCompatActivity {
List<Recipe>sListRecipe;
public int itempostion;
private String TAG;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectreceipe);
sListRecipe=MainActivity.getList();
Intent i =getIntent();
itempostion= i.getExtras().getInt("itempostion");
Log.e(TAG,"itempostion"+String.valueOf(itempostion)+"valou"+sListRecipe.get(itempostion).getName().toString());
}
logcat:
09-26 13:34:41.355 31852-31852/? D/dalvikvm: Late-enabling CheckJNI
09-26 13:34:42.459 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
09-26 13:34:42.471 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve interface method 20343: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V
09-26 13:34:42.471 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve interface method 20345: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve interface method 20349: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
09-26 13:34:42.479 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
09-26 13:34:42.483 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
09-26 13:34:42.495 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 478: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
09-26 13:34:42.495 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
09-26 13:34:42.495 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
09-26 13:34:42.495 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 500: Landroid/content/res/TypedArray;.getType (I)I
09-26 13:34:42.495 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008
09-26 13:34:42.695 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.widget.FrameLayout.startActionModeForChild, referenced from method android.support.v7.widget.ActionBarContainer.startActionModeForChild
09-26 13:34:42.695 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 20822: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
09-26 13:34:42.699 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002
09-26 13:34:42.735 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
09-26 13:34:42.735 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 20233: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
09-26 13:34:42.735 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6f at 0x0007
09-26 13:34:42.739 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList
09-26 13:34:42.743 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 292: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
09-26 13:34:42.743 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
09-26 13:34:42.763 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
09-26 13:34:42.763 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 441: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
09-26 13:34:42.763 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
09-26 13:34:42.763 31852-31852/blueappsoftware.mybakingtips I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
09-26 13:34:42.767 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve virtual method 443: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
09-26 13:34:42.767 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
09-26 13:34:42.771 31852-31852/blueappsoftware.mybakingtips E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
09-26 13:34:42.771 31852-31852/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve instanceof 142 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper;
09-26 13:34:42.771 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c
09-26 13:34:42.815 31852-31853/blueappsoftware.mybakingtips D/dalvikvm: GC_CONCURRENT freed 256K, 3% free 10939K/11271K, paused 18ms+1ms, total 27ms
09-26 13:34:42.815 31852-31852/blueappsoftware.mybakingtips D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 4ms
09-26 13:34:42.911 31852-31852/blueappsoftware.mybakingtips D/libEGL: loaded /system/lib/egl/libEGL_genymotion.so
[ 09-26 13:34:42.931 31852:31852 D/ ]
HostConnection::get() New Host Connection established 0xb7f81e90, tid 31852
09-26 13:34:43.019 31852-31852/blueappsoftware.mybakingtips D/libEGL: loaded /system/lib/egl/libGLESv1_CM_genymotion.so
09-26 13:34:43.035 31852-31852/blueappsoftware.mybakingtips D/libEGL: loaded /system/lib/egl/libGLESv2_genymotion.so
09-26 13:34:43.111 31852-31852/blueappsoftware.mybakingtips W/EGL_genymotion: eglSurfaceAttrib not implemented
09-26 13:34:43.127 31852-31852/blueappsoftware.mybakingtips D/OpenGLRenderer: Enabling debug mode 0
09-26 13:34:43.183 31852-31852/blueappsoftware.mybakingtips D/OpenGLRenderer: TextureCache::get: create texture(0xb7fa1438): name, size, mSize = 1, 4096, 4096
09-26 13:34:43.339 31852-31877/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips I/dalvikvm: Could not find method java.nio.file.Files.newOutputStream, referenced from method okio.Okio.sink
09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve static method 22787: Ljava/nio/file/Files;.newOutputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/OutputStream;
09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x71 at 0x000a
09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips I/dalvikvm: Could not find method java.nio.file.Files.newInputStream, referenced from method okio.Okio.source
09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips W/dalvikvm: VFY: unable to resolve static method 22786: Ljava/nio/file/Files;.newInputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/InputStream;
09-26 13:34:43.343 31852-31877/blueappsoftware.mybakingtips D/dalvikvm: VFY: replacing opcode 0x71 at 0x000a
09-26 13:34:43.567 31852-31853/blueappsoftware.mybakingtips D/dalvikvm: GC_CONCURRENT freed 279K, 4% free 11121K/11527K, paused 24ms+10ms, total 38ms
09-26 13:34:44.079 31852-31852/blueappsoftware.mybakingtips E/main: retrofit response [blueappsoftware.mybakingtips.Recipe#536b4c20, blueappsoftware.mybakingtips.Recipe#536c68ec, blueappsoftware.mybakingtips.Recipe#536c1a24, blueappsoftware.mybakingtips.Recipe#536e27b8]
09-26 13:34:44.079 31852-31852/blueappsoftware.mybakingtips E/main: name Nutella Pie serving 8
09-26 13:34:44.079 31852-31852/blueappsoftware.mybakingtips E/main: name Brownies serving 8
09-26 13:34:44.079 31852-31852/blueappsoftware.mybakingtips E/main: name Yellow Cake serving 8
09-26 13:34:44.079 31852-31852/blueappsoftware.mybakingtips E/main: name Cheesecake serving 8
09-26 13:34:44.119 31852-31853/blueappsoftware.mybakingtips D/dalvikvm: GC_CONCURRENT freed 217K, 4% free 11298K/11655K, paused 15ms+0ms, total 20ms
09-26 13:34:44.179 31852-31852/blueappsoftware.mybakingtips D/OpenGLRenderer: TextureCache::get: create texture(0xb7f36950): name, size, mSize = 9, 33856, 37952
09-26 13:34:48.955 31852-31852/blueappsoftware.mybakingtips W/EGL_genymotion: eglSurfaceAttrib not implemented
09-26 13:34:48.955 31852-31852/blueappsoftware.mybakingtips E/RecyclerView: No adapter attached; skipping layout
09-26 13:34:48.967 31852-31852/blueappsoftware.mybakingtips E/RecyclerView: No adapter attached; skipping layout
09-26 13:52:03.367 31852-31853/blueappsoftware.mybakingtips D/dalvikvm: GC_CONCURRENT freed 332K, 4% free 11375K/11847K, paused 6ms+1ms, total 16ms
09-26 14:16:49.627 31852-31853/blueappsoftware.mybakingtips D/dalvikvm: GC_CONCURRENT freed 426K, 5% free 11345K/11911K, paused 9ms+0ms, total 9ms
You are probably not getting the list because you are using the getter from a different Activity.Instead, you can pass your list via Intent, but for that, you should implement your class as Parcelable or Serializable .Here is a nice post for that How can I make my custom objects Parcelable? And one thing if you are bound to load and temporarily save a list so that you can use anywhere in App then you can use setter and getter in Android Application class.Here is a nice post for that Using the Android Application class to persist data
I want every minute to push a notifaction and a tried this:
https://stackoverflow.com/a/8801990
But the problem is, that the alarm doesn't run.
I tried to set Logs but I can't see them...
Now I've started a new project and here are my files, maybe one import is wrong, but I don't know...:
MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//startService(new Intent(this, YourService.class));
Log.e("MainActivity", "onCreate");
}
}
Alarm.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;
public class Alarm extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
// Put here YOUR code.
Log.e("Alarm", "onReceive");
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
wl.release();
}
public void setAlarm(Context context)
{
Log.e("Alarm", "setAlarm");
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
// Changed to 1 minute
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi); // Millisec * Second * Minute
}
public void cancelAlarm(Context context)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
}
YourService.java
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class YourService extends Service
{
Alarm alarm = new Alarm();
public void onCreate()
{
Log.e("Service", "onCreate");
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e("Service", "onStartCommand");
alarm.setAlarm(this);
return START_STICKY;
}
#Override
public void onStart(Intent intent, int startId)
{
alarm.setAlarm(this);
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
}
It's the same code like in the link, i just only changed the time to one minute...
Here is my Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--AutoStart Receiver -->
<receiver android:name="com.example.alarm.alarmmanagertest.AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<!-- Receiver and Permisson added -->
<receiver android:process=":remote" android:name="com.example.alarm.alarmmanagertest.Alarm"></receiver>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<!-- Added my Service -->
<service
android:name=".YourService"
android:enabled="true"
android:process=":your_service" >
</service>
</application>
There is nothing in the Android Monitor like "E/Alarm: onReceive" and for example the toast also doesn't start.
To be honest I tried everything, also tried this here: http://it-ride.blogspot.de/2010/10/android-implementing-notification.html
Could someone help me please and is it the right way to push a notification?
Using API 16 now...
App Logcat:
11-02 21:48:19.234 3433-3433/? D/dalvikvm: Late-enabling CheckJNI
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.app.Application.registerOnProvideAssistDataListener, referenced from method com.android.tools.fd.runtime.BootstrapApplication.registerOnProvideAssistDataListener
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 234: Landroid/app/Application;.registerOnProvideAssistDataListener (Landroid/app/Application$OnProvideAssistDataListener;)V
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.app.Application.unregisterOnProvideAssistDataListener, referenced from method com.android.tools.fd.runtime.BootstrapApplication.unregisterOnProvideAssistDataListener
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 237: Landroid/app/Application;.unregisterOnProvideAssistDataListener (Landroid/app/Application$OnProvideAssistDataListener;)V
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-02 21:48:19.726 3433-3433/com.example.alarm.alarmmanagertest I/InstantRun: Instant Run Runtime started. Android package is com.example.alarm.alarmmanagertest, real application class is null.
11-02 21:48:19.734 3433-3433/com.example.alarm.alarmmanagertest W/InstantRun: No instant run dex files added to classpath
11-02 21:48:19.742 3433-3433/com.example.alarm.alarmmanagertest E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
11-02 21:48:19.742 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve check-cast 1886 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
11-02 21:48:19.742 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x1f at 0x025e
11-02 21:48:19.742 3433-3433/com.example.alarm.alarmmanagertest E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
11-02 21:48:19.742 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve const-class 1886 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
11-02 21:48:19.742 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x1c at 0x0060
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve interface method 16034: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve interface method 16036: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve interface method 16040: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 721: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 743: Landroid/content/res/TypedArray;.getType (I)I
11-02 21:48:20.101 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008
11-02 21:48:20.437 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.widget.FrameLayout.startActionModeForChild, referenced from method android.support.v7.widget.ActionBarContainer.startActionModeForChild
11-02 21:48:20.437 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 16467: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
11-02 21:48:20.437 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002
11-02 21:48:20.453 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList
11-02 21:48:20.453 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 449: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
11-02 21:48:20.453 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
11-02 21:48:20.460 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
11-02 21:48:20.460 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 684: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
11-02 21:48:20.460 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-02 21:48:20.460 3433-3433/com.example.alarm.alarmmanagertest I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
11-02 21:48:20.460 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve virtual method 686: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
11-02 21:48:20.460 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-02 21:48:20.476 3433-3433/com.example.alarm.alarmmanagertest E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
11-02 21:48:20.476 3433-3433/com.example.alarm.alarmmanagertest W/dalvikvm: VFY: unable to resolve instanceof 149 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper;
11-02 21:48:20.476 3433-3433/com.example.alarm.alarmmanagertest D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c
11-02 21:48:20.648 3433-3438/com.example.alarm.alarmmanagertest D/dalvikvm: GC_CONCURRENT freed 235K, 20% free 5386K/6664K, paused 5ms+6ms, total 32ms
11-02 21:48:20.656 3433-3433/com.example.alarm.alarmmanagertest E/MainActivity: onCreate
11-02 21:48:20.906 3433-3433/com.example.alarm.alarmmanagertest D/libEGL: loaded /system/lib/egl/libEGL_MRVL.so
11-02 21:48:20.960 3433-3433/com.example.alarm.alarmmanagertest D/libEGL: loaded /system/lib/egl/libGLESv1_CM_MRVL.so
11-02 21:48:20.976 3433-3433/com.example.alarm.alarmmanagertest D/libEGL: loaded /system/lib/egl/libGLESv2_MRVL.so
11-02 21:48:20.992 3433-3433/com.example.alarm.alarmmanagertest D/GC: <tid=3433> OES20 ===> GC Version : GC Ver SS_rls_pxa988_JB42_R1_RC2_GC13.15
11-02 21:48:21.046 3433-3433/com.example.alarm.alarmmanagertest D/OpenGLRenderer: Enabling debug mode 0
Non-App Logcat:
1-02 21:59:45.765 515-617/? V/AlarmManager: trigger ELAPSED_REALTIME_WAKEUP or RTC_WAKEUP
11-02 21:59:49.914 515-592/? I/Monitor: SIOP:: Current AP = 380, CP = 0, PST = 380
11-02 21:59:52.914 515-607/? D/BatteryService: update start
11-02 21:59:52.921 515-607/? D/BatteryService: level:100, scale:100, status:3, health:2, present:true, voltage: 4325, temperature: 220, technology: Li-ion, AC powered:false, USB powered:true, Wireless powered:false, icon:17303457, invalid charger:0, online:4, charge type:1, current avg:450
11-02 21:59:52.921 515-592/? D/BatteryService: Sending ACTION_BATTERY_CHANGED.
11-02 21:59:52.929 775-775/? D/STATUSBAR-BatteryController: onReceive() - ACTION_BATTERY_CHANGED
11-02 21:59:52.929 775-775/? D/STATUSBAR-BatteryController: onReceive() - BATTERY_STATUS_DISCHARGING: tw_stat_sys_battery_usb_not_charge
11-02 21:59:52.953 775-775/? D/STATUSBAR-IconMerger: checkOverflow(390), More:false, Req:false Child:13
11-02 21:59:52.968 775-775/? D/STATUSBAR-PhoneStatusBar: mBrightnessEnablebySettings = true mBrightnessEnablebyBattery = true mBrightnessEnablebyDisableFlag = true
11-02 21:59:57.664 515-615/? I/PowerManagerService: [PWL] Off : 105s ago
11-02 21:59:59.921 515-592/? I/Monitor: SIOP:: Current AP = 380, CP = 0, PST = 380
11-02 21:59:59.992 515-617/? V/AlarmManager: waitForAlarm result :8
11-02 22:00:00.007 3817-3817/? D/KeyguardClockWidgetService: onReceive action=android.intent.action.TIME_TICK
11-02 22:00:00.015 515-592/? V/AlarmManager: ClockReceiver onReceive() ACTION_TIME_TICK
11-02 22:00:00.054 775-775/? D/STATUSBAR-IconMerger: checkOverflow(390), More:false, Req:false Child:13
11-02 22:00:03.015 515-607/? D/BatteryService: update start
11-02 22:00:05.039 515-595/? W/ProcessStats: Skipping unknown process pid 14477
11-02 22:00:05.039 515-595/? W/ProcessStats: Skipping unknown process pid 14479
11-02 22:00:05.039 515-595/? W/ProcessStats: Skipping unknown process pid 14480
11-02 22:00:09.929 515-592/? I/Monitor: SIOP:: Current AP = 380, CP = 0, PST = 380
11-02 22:00:12.062 515-837/? E/Watchdog: !#Sync 1050
11-02 22:00:12.078 515-617/? V/AlarmManager: waitForAlarm result :4
You can see, that there is something from the AlarmManager, but I think it's from an other App. Should I unstill my other Apps where I tried to work with the AlarmManager and restart my device?
Your <receiver> block in the manifest for the Alarm broadcast receiver is incorrect. The name attribute needs the FQN of the class or the shorthand ".Alarm", assuming that the class is part of the package declared at the top of the manifest file.
So guys I tried this one and it finally worked!
http://stacktips.com/tutorials/android/repeat-alarm-example-in-android
So if you want to run an AlarmManager and send a Notificiation, that opens your Application by clicking on it, here is the code:
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- Permission to start Alarm on device reboot -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<activity
android:name="alarmservice.javatechig.com.alarmservice.MyActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="alarmservice.javatechig.com.alarmservice.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<!-- Will not be called unless the application explicitly enables it -->
<receiver android:name="alarmservice.javatechig.com.alarmservice.DeviceBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
MyActivity.java
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.util.Calendar;
public class MyActivity extends Activity {
private PendingIntent pendingIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
/* Retrieve a PendingIntent that will perform a broadcast */
Intent alarmIntent = new Intent(MyActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MyActivity.this, 0, alarmIntent, 0);
findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
start();
}
});
findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancel();
}
});
findViewById(R.id.stopAlarmAt10).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAt10();
}
});
}
public void start() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Log.e("Alarm","started");
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
public void cancel() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
public void startAt10() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 20;
/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 13);
calendar.set(Calendar.MINUTE, 27);
/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, pendingIntent);
}
}
DeviceBootReceiver.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* #author Neel
* <p/>
* Broadcast reciever, starts when the device gets starts.
* Start your repeating alarm here.
*/
public class DeviceBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
/* Setting the alarm here */
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int interval = 8000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(context, "Alarm Set", Toast.LENGTH_SHORT).show();
}
}
}
AlarmReceiver.java
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
showNotification(context);
System.out.println("running");
Log.e("Alarm","running");
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}
private void showNotification(Context context) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MyActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("Finally");
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
The Source again: http://stacktips.com/tutorials/android/repeat-alarm-example-in-android
Some tips:
in <receiver android:name=""> write the whole name
don't forget the uses-permission
start a new project, insert this code and try to unterstand how it works
after that you can put in your project
Hi everyone I am new in Java programming and in the android studio environment and I need your help.
I am building an application that demands storing records of appointments into a database. I have created navigation between the activity's and I have also create a class named DBhandler which is about create update and delete records so far so good but the moment I add the following part of code in the activity that is about to create a new row in the database:
DBHandler db = new DBHandler(this);
db.NewApointment(new Apointments(23123,text2,text3,text4,text5));
Intent sendIntent = new Intent(Create_Apointment.this, Records.class);
startActivity(sendIntent);
The application for some reason stops working. I did research for my problem but I did not found anything similar to my problem, could it be a bug of android studio or am I missing something? I will show you my whole code just make to easier to you
This is my main activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void CreateApo(View view)
{
Intent i = new Intent(MainActivity.this, Create_Apointment.class);
startActivity(i);
}
public void Edit_Apointment(View v)
{
Intent intent = new Intent(MainActivity.this, Records.class);
startActivity(intent);
}
}
This is the activity which is used to store a new appointment
public class Create_Apointment extends AppCompatActivity {
EditText editText=(EditText) findViewById(R.id.editText);
EditText editText2=(EditText) findViewById(R.id.editText2);
EditText editText3=(EditText) findViewById(R.id.editText3);
EditText editText4=(EditText) findViewById(R.id.editText4);
String text2= editText.getText().toString();
String text3= editText2.getText().toString();
String text4= editText3.getText().toString();
String text5= editText4.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create__apointment);
}
public void back(View view)
{
Intent i = new Intent(Create_Apointment.this, MainActivity.class);
startActivity(i);
}
public void Create(View view)
{
DBHandler db = new DBHandler(this);
db.NewApointment(new Apointments(23123,text2,text3,text4,text5));
Intent sendIntent = new Intent(Create_Apointment.this, Katagrafes.class);
startActivity(sendIntent);
}
}
The following code is for the management of the database
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION=1;
//DATABASE NAME
private static final String DATABASE_NAME="APOINTMENTINFO";
private static final String APOINTMENT_TABLE="APOINTMENTS";
//Column NAMES
private static final String KEY_ID="id";
private static final String KEY_NAME="Name";
private static final String KEY_ADDRESS="Address";
private static final String KEY_PLACE="Place";
private static final String KEY_WRA="WRA";
public DBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
String Create_APOINTMENTS_TABLE= "CREATE TABLE"+ APOINTMENT_TABLE + "(" + KEY_ID +"INTERGER PRIMARY KEY,"+ KEY_NAME + "TEXT," +KEY_ADDRESS + "TEXT," + KEY_PLACE+"TEXT,"+ KEY_WRA+"TEXT" + ")";
db.execSQL(Create_APOINTMENTS_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion)
{
db.execSQL("DROP TABLE IF EXISTS " + APOINTMENT_TABLE );
onCreate(db);
}
//add-insert new apointment
public void NewApointment(Apointments apointments)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values= new ContentValues();
values.put(KEY_NAME, apointments.getName()); //ONOMA RADEVOY
values.put(KEY_ADDRESS, apointments.getAddress());// adress
values.put(KEY_PLACE, apointments.getPlace());//meros
values.put(KEY_WRA, apointments.getWraRad());//wra
db.insert(APOINTMENT_TABLE, null ,values);
db.close();
}
// read 1 record
public Apointments getApointment(int id )
{
SQLiteDatabase db= this.getReadableDatabase();
Cursor cursor = db.query(APOINTMENT_TABLE, new String[]{KEY_ID, KEY_NAME,KEY_ADDRESS,KEY_PLACE,KEY_WRA},KEY_ID+"=?",new String[]{String.valueOf(id)},null,null,null,null );
if (cursor !=null)
cursor.moveToFirst();
Apointments contact = new Apointments(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
//return apointment
return contact;
}
public List<Apointments> getAllApointments()
{
List<Apointments> apointmentsList= new ArrayList<Apointments>();
//selection of all querys
String selectQuery = "SELECT * FROM "+ APOINTMENT_TABLE;
SQLiteDatabase db= this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery,null);
if (cursor.moveToFirst())
{
do {
Apointments apointments = new Apointments(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
}while (cursor.moveToNext());
}
return apointmentsList;
}
public int updateApointment(Apointments apointments)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME,apointments.getName());
values.put(KEY_ADDRESS,apointments.getName());
values.put(KEY_PLACE,apointments.getPlace());
values.put(KEY_WRA,apointments.getWraRad());
//update row
return db.update(APOINTMENT_TABLE,values, KEY_ID + " =?", new String[] {String.valueOf(apointments.getId())} );
}
//delete records
public void deleteapoitnments (Apointments apointments)
{
SQLiteDatabase db= this.getWritableDatabase();
db.delete(APOINTMENT_TABLE,KEY_ID+"=?", new String[]{String.valueOf(apointments.getId())} );
db.close();
}
}
Also I have create a class named "appointments" to refer an appointment as an object in my application.
public class Apointments {
private int id;
private String Name;
private String Address;
private String Place;
private String WraRad;
public Apointments(int id, String Name, String Address, String Place, String WraRad )
{
this.id=id;
this.Name=Name;
this.Address=Address;
this.Place=Place;
this.WraRad=WraRad;
}
public void setId(int id) {
this.id = id;
}
public void setName(String Name) {
this.Name=Name;
}
public void setAddress(String Address) {
this.Address=Address;
}
public void setPlace(String Place) {
this.Place=Place;
}
public void setWraRad(String WraRad) {
this.WraRad=WraRad;
}
public int getId()
{
return id;
}
public String getName()
{
return Name;
}
public String getAddress()
{
return Address;
}
public String getPlace()
{
return Place;
}
public String getWraRad()
{
return WraRad;
}
}
My logCat says the following:
08-30 16:34:26.479 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x1f at 0x0ac7
08-30 16:34:26.479 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.getReferrer, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.479 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 74: Landroid/app/Activity;.getReferrer ()Landroid/net/Uri;
08-30 16:34:26.479 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0b3b
08-30 16:34:26.479 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.content.ContextWrapper.getSystemServiceName, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.479 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 517: Landroid/content/ContextWrapper;.getSystemServiceName (Ljava/lang/Class;)Ljava/lang/String;
08-30 16:34:26.479 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0bb8
08-30 16:34:26.479 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.isVoiceInteractionRoot, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 98: Landroid/app/Activity;.isVoiceInteractionRoot ()Z
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0be0
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.isDestroyed, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 93: Landroid/app/Activity;.isDestroyed ()Z
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0beb
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve check-cast 236 (Landroid/os/UserHandle;) in Lcom/example/dim/myapplication/Create_Apointment;
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x1f at 0x0c3b
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.content.ContextWrapper.getExternalMediaDirs, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 504: Landroid/content/ContextWrapper;.getExternalMediaDirs ()[Ljava/io/File;
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0c68
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve check-cast 236 (Landroid/os/UserHandle;) in Lcom/example/dim/myapplication/Create_Apointment;
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x1f at 0x0ca4
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.getMediaController, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 68: Landroid/app/Activity;.getMediaController ()Landroid/media/session/MediaController;
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0cac
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.finishAffinity, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 48: Landroid/app/Activity;.finishAffinity ()V
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0cb2
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.getSearchEvent, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 77: Landroid/app/Activity;.getSearchEvent ()Landroid/view/SearchEvent;
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0d2b
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication I/dalvikvm: DexOpt: illegal method access (call Landroid/support/v4/app/FragmentActivity;.onReallyStop ()V from Lcom/example/dim/myapplication/Create_Apointment;)
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.support.v4.app.FragmentActivity.onReallyStop, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 2396: Landroid/support/v4/app/FragmentActivity;.onReallyStop ()V
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0d6b
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.isVoiceInteraction, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 97: Landroid/app/Activity;.isVoiceInteraction ()Z
08-30 16:34:26.489 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0ddb
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.requestVisibleBehind, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 169: Landroid/app/Activity;.requestVisibleBehind (Z)Z
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0e40
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.shouldShowRequestPermissionRationale, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 199: Landroid/app/Activity;.shouldShowRequestPermissionRationale (Ljava/lang/String;)Z
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0e55
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.onProvideAssistData, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 141: Landroid/app/Activity;.onProvideAssistData (Landroid/os/Bundle;)V
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0e6a
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.finishAndRemoveTask, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 50: Landroid/app/Activity;.finishAndRemoveTask ()V
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0e92
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.content.Context.getColor, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 425: Landroid/content/Context;.getColor (I)I
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0ee9
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.onNavigateUp, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 131: Landroid/app/Activity;.onNavigateUp ()Z
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0efa
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.reportFullyDrawn, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 167: Landroid/app/Activity;.reportFullyDrawn ()V
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0f17
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.getParentActivityIntent, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 72: Landroid/app/Activity;.getParentActivityIntent ()Landroid/content/Intent;
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0f1d
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve check-cast 53 (Landroid/app/assist/AssistContent;) in Lcom/example/dim/myapplication/Create_Apointment;
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x1f at 0x0f5b
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.finishAfterTransition, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication W/dalvikvm: VFY: unable to resolve virtual method 49: Landroid/app/Activity;.finishAfterTransition ()V
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication D/dalvikvm: VFY: replacing opcode 0x6f at 0x0f7f
08-30 16:34:26.499 12295-12295/com.example.dim.myapplication I/dalvikvm: Could not find method android.app.Activity.getContentScene, referenced from method com.example.dim.myapplication.Create_Apointment.access$super
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.dim.myapplication/com.example.dim.myapplication.Create_Apointment}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
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:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:68)
at android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDelegateImplV7.java:145)
at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:188)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:170)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:502)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:174)
at com.example.dim.myapplication.Create_Apointment.(Create_Apointment.java:13)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1027)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
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:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
Any suggestions would be very helpful feel free to comment if something of my question is wrong
Thanks in advance
I am creating a quiz app for androids. I have 3 fragments and 3 checkboxes on first two of them. On the last fragment there is a button which when pressed opens an activity called "End". I need that on the "End" activity a result would appear of how many correct answers one have chosen.
Firstly, I've tried to check if it's working with only the first fragment, but I got stuck. The app closes when I press the button.
End.java:
package bandymas.viewpagerexample;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class End extends ActionBarActivity {
private CheckBox checkBox1, checkBox2, checkBox3;
private Button button;
TextView newresult = (TextView)findViewById(R.id.textView1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_end);
getSupportActionBar().hide();
addListenerOnButton();
}
private void addListenerOnButton() {
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
//Run when button is clicked
#Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("CheckBox1 : ").append(checkBox1.isChecked());
result.append("\nCheckbox2 : ").append(checkBox2.isChecked());
result.append("\nCheckBox3 :").append(checkBox3.isChecked());
newresult.setText("My Awesome Text");
}
});
}
#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_naujas_baigimas, 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's my logcat:
04-01 20:04:37.564 6020-6020/bandymas.viewpagerexample W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-01 20:04:37.564 6020-6020/bandymas.viewpagerexample I/PersonaManager﹕ getPersonaService() name persona_policy
04-01 20:04:37.684 6020-6020/bandymas.viewpagerexample I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: (CL3869936)
OpenGL ES Shader Compiler Version: 17.01.11.SPL
Build Date: 01/17/14 Fri
Local Branch:
Remote Branch:
Local Patches:
Reconstruct Branch:
04-01 20:04:37.734 6020-6020/bandymas.viewpagerexample D/OpenGLRenderer﹕ Enabling debug mode 0
04-01 20:07:31.404 7094-7094/bandymas.viewpagerexample I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
04-01 20:07:31.404 7094-7094/bandymas.viewpagerexample I/SELinux﹕ Function: selinux_android_load_priority , priority version is VE=SEPF_GT-I9505_4.4.2_0033
04-01 20:07:31.404 7094-7094/bandymas.viewpagerexample I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
04-01 20:07:31.404 7094-7094/bandymas.viewpagerexample E/dalvikvm﹕ >>>>> Normal User
04-01 20:07:31.404 7094-7094/bandymas.viewpagerexample E/dalvikvm﹕ >>>>> bandymas.viewpagerexample [ userId:0 | appId:10229 ]
04-01 20:07:31.404 7094-7094/bandymas.viewpagerexample D/dalvikvm﹕ Late-enabling CheckJNI
04-01 20:07:31.584 7094-7094/bandymas.viewpagerexample W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-01 20:07:31.594 7094-7094/bandymas.viewpagerexample I/PersonaManager﹕ getPersonaService() name persona_policy
04-01 20:07:31.794 7094-7094/bandymas.viewpagerexample I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: (CL3869936)
OpenGL ES Shader Compiler Version: 17.01.11.SPL
Build Date: 01/17/14 Fri
Local Branch:
Remote Branch:
Local Patches:
Reconstruct Branch:
04-01 20:07:31.844 7094-7094/bandymas.viewpagerexample D/OpenGLRenderer﹕ Enabling debug mode 0
04-01 20:07:42.454 7094-7094/bandymas.viewpagerexample W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample W/dalvikvm﹕ VFY: unable to resolve virtual method 11347: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample W/dalvikvm﹕ VFY: unable to resolve virtual method 11353: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample W/dalvikvm﹕ VFY: unable to resolve virtual method 9041: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
04-01 20:07:42.485 7094-7094/bandymas.viewpagerexample D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
04-01 20:07:42.495 7094-7094/bandymas.viewpagerexample I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
04-01 20:07:42.495 7094-7094/bandymas.viewpagerexample W/dalvikvm﹕ VFY: unable to resolve virtual method 365: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
04-01 20:07:42.495 7094-7094/bandymas.viewpagerexample D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-01 20:07:42.495 7094-7094/bandymas.viewpagerexample I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
04-01 20:07:42.495 7094-7094/bandymas.viewpagerexample W/dalvikvm﹕ VFY: unable to resolve virtual method 387: Landroid/content/res/TypedArray;.getType (I)I
04-01 20:07:42.495 7094-7094/bandymas.viewpagerexample D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
04-01 20:07:42.525 7094-7094/bandymas.viewpagerexample D/AndroidRuntime﹕ Shutting down VM
04-01 20:07:42.525 7094-7094/bandymas.viewpagerexample W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4187fda0)
04-01 20:07:42.525 7094-7094/bandymas.viewpagerexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: bandymas.viewpagerexample, PID: 7094
java.lang.RuntimeException: Unable to start activity ComponentInfo{bandymas.viewpagerexample/bandymas.viewpagerexample.End}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at bandymas.viewpagerexample.End.addListenerOnButton(End.java:33)
at bandymas.viewpagerexample.End.onCreate(End.java:23)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
activity_end.xml:
<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="bandymas.viewpagerexample.End">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Result"
android:id="#+id/textView1"
android:layout_marginTop="100dp"
android:textSize="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Please Check that there is the button(R.id.button) in activity_end.xml.
I guess that the button's id doesn't exist in activity_end.xml.
I'm developing an Android app but as I am a newbie so can't figure out the solution to the problem. My application crashes when I install it on my Phone (Android 4.3) while it runs fine on the emulator. Though, I know that it crashes when I initialize my Bluetooth Adapter mBluetoothAdapter, but dont know the solution.
public class MainActivity extends ActionBarActivity {
protected static final int REQUEST_ENABLE_BT = 1;
Button btnConnect;
int seekVal;
SeekBar seek ;
TextView editText1;
BluetoothAdapter mBluetoothAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnConnect = (Button)findViewById(R.id.btnConnect);
editText1 = (TextView)findViewById(R.id.editText1);
seekVal = 0;
seek = (SeekBar)findViewById(R.id.seekBar1);
editText1.setText(String.valueOf(0));
// Crashes when Bluetooth Adapter is initialized
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// App runs when the above line is commented out
try{
if (mBluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "Your device does not support bluetooth device. Sorry but the application will exit now!", Toast.LENGTH_SHORT).show();
btnConnect.setEnabled(false);
}
else
{
if (mBluetoothAdapter.isEnabled()) {
btnConnect.setEnabled(false);
}
}
}
catch(Exception ex)
{
}
btnConnect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}
Here is a part of my MANIFEST
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="21" />
<permission android:name="android.permission.BLUETOOTH"></permission>
Here is my LogCat
01-10 14:55:56.142: E/ResourceType(1027): Style contains key with bad entry: 0x01010479
01-10 14:55:56.392: I/dalvikvm(1027): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
01-10 14:55:56.392: W/dalvikvm(1027): VFY: unable to resolve virtual method 11341: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
01-10 14:55:56.404: D/dalvikvm(1027): VFY: replacing opcode 0x6f at 0x0000
01-10 14:55:56.412: I/dalvikvm(1027): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
01-10 14:55:56.412: W/dalvikvm(1027): VFY: unable to resolve virtual method 11347: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
01-10 14:55:56.423: D/dalvikvm(1027): VFY: replacing opcode 0x6f at 0x0000
01-10 14:55:56.423: I/dalvikvm(1027): Could not find method android.view.ViewGroup.onWindowSystemUiVisibilityChanged, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onWindowSystemUiVisibilityChanged
01-10 14:55:56.423: W/dalvikvm(1027): VFY: unable to resolve virtual method 11349: Landroid/view/ViewGroup;.onWindowSystemUiVisibilityChanged (I)V
01-10 14:55:56.423: D/dalvikvm(1027): VFY: replacing opcode 0x6f at 0x0008
01-10 14:55:56.442: I/dalvikvm(1027): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
01-10 14:55:56.442: W/dalvikvm(1027): VFY: unable to resolve virtual method 9035: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
01-10 14:55:56.453: D/dalvikvm(1027): VFY: replacing opcode 0x6e at 0x000e
01-10 14:55:56.662: I/dalvikvm(1027): Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
01-10 14:55:56.671: W/dalvikvm(1027): VFY: unable to resolve virtual method 11344: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
01-10 14:55:56.671: D/dalvikvm(1027): VFY: replacing opcode 0x6f at 0x0007
01-10 14:55:56.711: I/dalvikvm(1027): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
01-10 14:55:56.711: W/dalvikvm(1027): VFY: unable to resolve virtual method 366: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
01-10 14:55:56.711: D/dalvikvm(1027): VFY: replacing opcode 0x6e at 0x0002
01-10 14:55:56.722: I/dalvikvm(1027): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
01-10 14:55:56.722: W/dalvikvm(1027): VFY: unable to resolve virtual method 388: Landroid/content/res/TypedArray;.getType (I)I
01-10 14:55:56.722: D/dalvikvm(1027): VFY: replacing opcode 0x6e at 0x0002
01-10 14:55:58.262: D/dalvikvm(1027): GC_CONCURRENT freed 196K, 5% free 7098K/7431K, paused 8ms+5ms
01-10 14:55:58.492: D/gralloc_goldfish(1027): Emulator without GPU emulation detected.
Basically the problem was of insufficient user permission in the Manifest
https://stackoverflow.com/a/11469502/1584140 helped me to use try catch for getting the actual error.
Finally I solved my issue by setting up the permissions like this.
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />