Dialg - ImageView - NullPointerException - setOnClickListener - java

I have a NullpointerException but i can't figure out why! please your help as i'm struggling with it :
Here's my XML resource :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="20dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:gravity="right"
android:orientation="vertical" >
<TextView
android:id="#+id/txt_speciality_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: " />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add:" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="phone :" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abc abc abc" />
<TextView
android:id="#+id/textView44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abc" />
<TextView
android:id="#+id/textView55"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="230020202" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/imageViewClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/close_selector" />
</RelativeLayout>
Below my Java instructions :
The setContentView is before the use of the actual TextView
final Dialog dialog = new Dialog(searchActivity.this);
dialog.setContentView(R.layout.custom_dialog);``
ImageView closeBtn =(ImageView)findViewById(R.id.imageViewClose);
closeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
finish();
}
});
Logcat errros :
java.lang.NullPointerException
at com.itraan.Test.searchActivity$2.onItemClick(searchActivity.java:902)
at android.widget.AdapterView.performItemClick(AdapterView.java:308)
at android.widget.AbsListView.performItemClick(AbsListView.java:1483)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3485)
at android.widget.AbsListView$3.run(AbsListView.java:4843)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
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)
Thank you in advance.

Related

Unable to start activity crash decorcontent parent

Unable to start activity Component Info ERROR
When im starting my app, its skippp all the ativity and goes at the somewhere activity and after 3 seconds it finally crashes.
its show no visible error on my java and xml...
im having error on my logcat
idont know where is the error
2019-10-16 05:55:20.542 23865-23865/com.ilsproj.ilovebatangas E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ilsproj.ilovebatangas, PID: 23865
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ilsproj.ilovebatangas/com.ilsproj.ilovebatangas.ui.CategorySelection}: java.lang.NullPointerException: Attempt to invoke interface method 'void androidx.appcompat.widget.DecorContentParent.setWindowCallback(android.view.Window$Callback)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:169)
at android.app.ActivityThread.main(ActivityThread.java:6521)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void androidx.appcompat.widget.DecorContentParent.setWindowCallback(android.view.Window$Callback)' on a null object reference
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:753)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:552)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.ilsproj.ilovebatangas.ui.CategorySelection.onCreate(CategorySelection.java:20)
at android.app.Activity.performCreate(Activity.java:7051)
at android.app.Activity.performCreate(Activity.java:7042)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:169) 
at android.app.ActivityThread.main(ActivityThread.java:6521) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Heres my Splashcreen.java
public class Splashsrcreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashsrcreen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splashsrcreen.this,CategorySelection.class);
startActivity(i);
finish();
}
}, 3000);
}
}
My catergoryselection.java
public class CategorySelection extends AppCompatActivity {
CardView imagebutton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category_selection);
CardView mountain = findViewById(R.id.cv_mountains);
mountain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( CategorySelection.this, Cat_Mountains.class);
startActivity(intent);
}
});
}
}
activity_category_selection
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<LinearLayout
android:orientation="vertical"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#color/colorPrimary"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<GridLayout
android:id="#+id/mainGrid"
android:layout_width="385dp"
android:layout_height="589dp"
android:layout_gravity="center"
android:layout_weight="8"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:rowCount="5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.53999996">
<androidx.cardview.widget.CardView
android:id="#+id/cv_beaches"
android:layout_width="185dp"
android:layout_height="198dp"
android:layout_margin="5dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="155dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:background="#ffff"
android:scaleType="fitCenter"
android:src="#drawable/beaches" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Beaches"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cv_mountains"
android:layout_width="185dp"
android:layout_height="198dp"
android:layout_margin="5dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="155dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:background="#ffff"
android:scaleType="fitCenter"
android:src="#drawable/mountain" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Mountains"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cv_island"
android:layout_width="185dp"
android:layout_height="198dp"
android:layout_margin="5dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="155dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:background="#ffff"
android:scaleType="fitCenter"
android:src="#drawable/island" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Island"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cv_churces"
android:layout_width="185dp"
android:layout_height="198dp"
android:layout_margin="5dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="155dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:background="#ffff"
android:scaleType="fitCenter"
android:src="#drawable/churches" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Churches"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cv_heritage"
android:layout_width="185dp"
android:layout_height="198dp"
android:layout_margin="5dp"
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="155dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:background="#ffff"
android:scaleType="fitCenter"
android:src="#drawable/heritage" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Heritage"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>
This could be a caching problem.
try with:
"Android Studio: menu> Build -> Clean Project"
or
"Android Studio: menu> File -> Invalidate Caches / Restart"

Error inflating class fragment Android kotlin

Error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.pockethcm.greytrix.test, PID: 6566
android.view.InflateException: Binary XML file line #29: Binary XML file line #29: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #29: Duplicate id 0x7f09012d, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3680)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:420)
at android.app.Dialog.setContentView(Dialog.java:557)
at com.pockethcm.greytrix.test.Fragments.DashboardFragment.openPunchDetailDialog(DashboardFragment.kt:1183)
at com.pockethcm.greytrix.test.Fragments.DashboardFragment$sendLocationData$1.onSuccess(DashboardFragment.kt:767)
at com.pockethcm.greytrix.test.Fragments.DashboardFragment$sendLocationData$1.onSuccess(DashboardFragment.kt:102)
at com.google.android.gms.tasks.zzj.run(Unknown Source:27)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Fragment Code
var mapFragment = activity.supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
if(mapFragment != null){
mapFragment!!.getMapAsync(OnMapReadyCallback {
googleMap = it
val location1 = LatLng(13.03,77.60)
val location2 = LatLng(13.53,77.67)
googleMap.addMarker(MarkerOptions().position(location1).title("First Location"))
googleMap.addMarker(MarkerOptions().position(location2).title("Second Location"))
val center = CameraUpdateFactory.newLatLng(location1)
val zoom = CameraUpdateFactory.zoomTo(15f)
val markerPoints = LatLngBounds(
location1, location2)
googleMap.setOnMapLoadedCallback(GoogleMap.OnMapLoadedCallback {
googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(markerPoints,30))
})
})
}
punchdetailLayout
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/view_layout"
android:orientation="vertical"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Address"
android:layout_weight="0.5"
android:textColor="#000000"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/address"
android:textColor="#000000"
android:layout_weight="0.5"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Locality"
android:layout_weight="0.5"
android:textColor="#000000"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/locality"
android:textColor="#000000"
android:layout_weight="0.5"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Time"
android:layout_weight="0.5"
android:textColor="#000000"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/time"
android:textColor="#000000"
android:layout_weight="0.5"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/ok"
android:text="OK">
</Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Records Found!"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:visibility="gone"
android:id="#+id/no_rec"
/>
</android.support.v7.widget.CardView>

Strange error java.lang.reflect.Method.invokeNative(Native Method) Android Studio please answer

I'm developing an app for my school with a timetable and a little calculator
Here's the manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.markwitt.schul_app">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:launchMode="singleTop"
android:minSdkVersion="20"
android:supportsRtl="true"
android:targetSdkVersion="25"
android:theme="#style/AppTheme">
<activity android:name=".Stundenplan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Vertretungsplan" />
<activity android:name=".Rechner" />
<activity android:name=".Overlay"></activity>
</application>
Here's the Launcher activity activity_stundenplan.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<Button
android:text="Rechner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonrechner"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="44dp"
android:layout_marginStart="44dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonvertr"
android:text="Vertretungsplan"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/Tabelle"
android:layout_alignEnd="#+id/Tabelle" />
<TableLayout
android:id="#+id/Tabelle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.markwitt.schul_app.Stundenplan"
android:background="#color/white"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/r10"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r20"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r30"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r40"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r50"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r60"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/r11"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView android:text="#string/r21" android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r31"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r41"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r51"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r61"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/r12"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:textStyle="normal|bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r22"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r32"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r42"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r52"
android:padding="10dp"
android:background="#drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/r62"
android:padding="10dp"
android:background="#drawable/cell_shape"
//more TableRows
</TableLayout>
</Relativelayout>
I've made an Intent in Stundenplan.java:
rechner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(Stundenplan.this, Rechner.class));
}
});
The Problem is that everytime I debug-launch the app on my Xperia Z3 and press the button to open the "Rechner"-Activity, the app closes and it shows me the following error:(sry for the bad format)
I/OpenGLRenderer: Initialized EGL, version 1.4
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy#f97be6e time:6061206
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy#f97be6e time:6065998
I/Timeline: Timeline: Activity_launch_request id:com.example.markwitt.schul_app time:6074313
D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.markwitt.schul_app, PID: 22562
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.markwitt.schul_app/com.example.markwitt.schul_app.Rechner}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2375)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:203)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:193)
at com.example.markwitt.schul_app.Rechner.<init>(Rechner.java:16)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
at android.app.ActivityThread.access$900(ActivityThread.java:154) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:224) 
at android.app.ActivityThread.main(ActivityThread.java:5526) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
I/Process: Sending signal. PID: 22562 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'
My Rechner.java and my activity_rechner look like this:
package com.example.markwitt.schul_app;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class Rechner extends AppCompatActivity {
private RadioGroup radioGroup;
private RadioButton checked;
private Button los;
public String mode;
EditText number1 = (EditText) findViewById(R.id.nummer1);
EditText number2 = (EditText) findViewById(R.id.nummer2);
EditText ergebnis = (EditText) findViewById(R.id.ergebnis);
public int text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechner);
addListenerOnButton();
}
public void addListenerOnButton() {
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
los = (Button) findViewById(R.id.button5);
los.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = radioGroup.getCheckedRadioButtonId();
checked = (RadioButton) findViewById(selectedId);
switch (checked.getId()){
case R.id.plus: mode = "+";
case R.id.minus: mode = "-";
case R.id.mal: mode = "*";
case R.id.geteilt: mode = ":";
case R.id.fakultaet: mode = "fak";
}
text1 = Integer.parseInt(number1.getText().toString());
int text2 = Integer.parseInt(number2.getText().toString());
switch (mode){
case "+": ergebnis.setText(text1 + text2);
case "-": ergebnis.setText(text1 - text2);
case "*": ergebnis.setText(text1 * text2);
case ":": ergebnis.setText(text1 / text2);
case "fak": ergebnis.setText(Long.toString(fakultaet(text1)));
}
}
});
}
static long fakultaet(int n) {
int ergebnis = 1;
for (int i = 1; i <= n; i++) {
ergebnis = ergebnis * i;
}
return ergebnis;
}
}
...
<?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:id="#+id/activity_durchschnitt"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.markwitt.schul_app.Rechner">
<EditText
android:id="#+id/nummer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:layout_weight="1"
android:ems="10"
android:hint="Nummer 1"
android:inputType="number"
android:maxLines="1"
android:textAlignment="center" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/ergebnis"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:text="=" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/nummer1"
android:layout_marginEnd="42dp"
android:layout_marginRight="42dp"
android:layout_marginTop="69dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/mal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/geteilt"
android:layout_alignBottom="#+id/geteilt"
android:layout_toLeftOf="#+id/geteilt"
android:layout_toStartOf="#+id/geteilt"
android:layout_weight="1"
android:text="* "
android:textAllCaps="true"
android:textStyle="bold" />
<RadioButton
android:id="#+id/fakultaet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/mal"
android:layout_alignBottom="#+id/mal"
android:layout_toLeftOf="#+id/mal"
android:layout_toStartOf="#+id/mal"
android:layout_weight="1"
android:text="Fakultät "
android:textAllCaps="true"
android:textStyle="bold" />
<RadioButton
android:id="#+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/minus"
android:layout_alignBottom="#+id/minus"
android:layout_toEndOf="#+id/nummer1"
android:layout_toRightOf="#+id/nummer1"
android:layout_weight="1"
android:text="+ "
android:textAllCaps="true"
android:textStyle="bold" />
<RadioButton
android:id="#+id/geteilt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/radioGroup"
android:layout_toEndOf="#+id/radioGroup"
android:layout_toRightOf="#+id/radioGroup"
android:layout_weight="1"
android:text=": "
android:textAllCaps="true"
android:textStyle="bold" />
<RadioButton
android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/geteilt"
android:layout_alignBottom="#+id/geteilt"
android:layout_toLeftOf="#+id/plus"
android:layout_toStartOf="#+id/plus"
android:layout_weight="1"
android:text="- "
android:textAllCaps="true"
android:textStyle="bold" />
</RadioGroup>
<EditText
android:id="#+id/ergebnis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nummer2"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/nummer2"
android:layout_marginBottom="61dp"
android:ems="10"
android:hint="Ergebnis"
android:inputType="number"
android:textAlignment="center" />
<EditText
android:id="#+id/nummer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nummer1"
android:layout_alignStart="#+id/nummer1"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Nummer 2"
android:inputType="number"
android:textAlignment="center" />
</RelativeLayout>
Can you please give me an answer what to do? It does the same on my phone and on my emulator
If you have questions, contact me please!
PS: I have to finish it till the 12th December
You cannot use findviewbyid before you set the content view. It returns null and that's your exception.
Change your code to something like that:
EditText number1;
EditText number2;
EditText ergebnis;
public int text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rechner);
number1 = (EditText) findViewById(R.id.nummer1);
number2 = (EditText) findViewById(R.id.nummer2);
ergebnis = (EditText) findViewById(R.id.ergebnis);
addListenerOnButton();
}
Also, Ill recommend you to read the android code style recommendations.
https://source.android.com/source/code-style.html

Can't run App on Note 2 because of too many images (only 20 images)

I am getting the following error when I go from my Main Activity page to my New Activity page. The New Activity page crashes unless it has <10 images or so, which is pathetic (the app runs fine emulated on my computer, however):
06-22 16:53:04.841 15058-15058/? D/AndroidRuntime: Shutting down VM
06-22 16:53:04.841 15058-15058/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41e35c08)
06-22 16:53:04.851 15058-15058/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.matthewscompany.matthew.test1, PID: 15058
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:683)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:513)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:889)
at android.content.res.Resources.loadDrawable(Resources.java:3436)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.widget.ImageView.<init>(ImageView.java:133)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.example.matthew.test1.NewActivity.onCreate(NewActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
06-22 16:53:04.861 2430-2769/? W/ActivityManager: Force finishing activity com.matthewscompany.matthew.test1/com.example.matthew.test1.NewActivity
06-22 16:53:04.866 2430-2769/? W/ActivityManager: Force finishing activity com.matthewscompany.matthew.test1/com.example.matthew.test1.MainActivity
I understand that the out of memory error occurs when the ram for a phone runs out when processing too many bitmaps.
Yes, I have read the Android Developer webpages on tips for processing bitmaps. However, I still don't understand which groups of code from their guides I'm supposed to insert, and where I'm supposed to insert it. I've tried inserting some of their stuff into my .java files but I just end up getting errors. I owe this to me being a beginner at Java and not understanding how to apply the example codes.
Again, I am a beginner to Java and Android Studio so please go easy on me and explain things as simply as you can, if possible.
Here's my code:
MainActivity.java:
package com.example.matthew.test1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity {
//Main Menu
//Main Activity Mario Icon:
ImageButton Mario;
ImageButton Luigi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Mario= (ImageButton) findViewById(R.id.Mario);
Mario.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentLoadNewActivity = new Intent(MainActivity.this, NewActivity.class);
startActivity(intentLoadNewActivity);
}
});
Luigi= (ImageButton) findViewById(R.id.Luigi);
Luigi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentLoadNewActivity = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intentLoadNewActivity);
}
});
}
}
NewActivity.java:
public class NewActivity extends AppCompatActivity implements View.OnClickListener {
ViewFlipper viewFlipper;
Button next;
Button previous;
TextView pageCounter;
int count = 1;
private static final int TOTAL_IMG_COUNT = 23;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
pageCounter = (TextView)findViewById(R.id.textView2);
pageCounter.setText(String.valueOf(count));
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == next) {
viewFlipper.showNext();
if (next.getId() == (R.id.ImageView23)) {
count = 1;
} else {
count++;
}
pageCounter.setText(String.valueOf(count) + "/" + TOTAL_IMG_COUNT);
}
else if (v == previous) {
viewFlipper.showPrevious();
if(previous.getId() == (R.id.ImageView1)) {
count = TOTAL_IMG_COUNT;
} else {
count--;
}
pageCounter.setText(String.valueOf(count) + "/" + TOTAL_IMG_COUNT);
}
}
}
content_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.matthew.test1.MainActivity"
tools:showIn="#layout/activity_main">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Mario"
android:src="#drawable/mario_select"
android:layout_above="#+id/samus_select"
android:layout_alignLeft="#+id/samus_select"
android:layout_alignStart="#+id/samus_select" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Luigi"
android:src="#drawable/luigi_select"
android:layout_above="#+id/samus_select"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dk_select"
android:src="#drawable/dk_select"
android:layout_above="#+id/falcon_select"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/link_select"
android:src="#drawable/link_select"
android:layout_below="#+id/fox_select"
android:layout_alignParentLeft="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/samus_select"
android:src="#drawable/samus_select"
android:layout_above="#+id/kirby_select"
android:layout_centerHorizontal="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/falcon_select"
android:src="#drawable/falcon_select"
android:layout_alignTop="#+id/samus_select"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ness_select"
android:src="#drawable/ness_select"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/yoshi_select"
android:src="#drawable/yoshi_select"
android:layout_below="#+id/falcon_select"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/kirby_select"
android:src="#drawable/kirby_select"
android:layout_above="#+id/pika_select"
android:layout_alignLeft="#+id/samus_select"
android:layout_alignStart="#+id/samus_select" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fox_select"
android:src="#drawable/fox_select"
android:layout_alignParentStart="true"
android:layout_below="#+id/Luigi"
android:layout_alignParentLeft="true" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pika_select"
android:src="#drawable/pika_select"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/kirby_select"
android:layout_alignStart="#+id/kirby_select" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/jiggly_select"
android:src="#drawable/jiggly_select"
android:layout_below="#+id/yoshi_select"
android:layout_alignLeft="#+id/yoshi_select"
android:layout_alignStart="#+id/yoshi_select" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hitbox64"
android:id="#+id/textView"
android:textSize="48dp"
android:layout_above="#+id/Mario"
android:layout_centerHorizontal="true" />
</RelativeLayout>
activity_new.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.matthew.test1.NewActivity">
<ViewFlipper
android:layout_width="480dp"
android:layout_height="480dp"
android:id="#+id/viewFlipper">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView1"
android:scaleType="centerCrop"
android:src="#drawable/b1"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView2"
android:scaleType="centerCrop"
android:src="#drawable/b2"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView3"
android:scaleType="centerCrop"
android:src="#drawable/b3"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView4"
android:scaleType="centerCrop"
android:src="#drawable/b4"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView5"
android:scaleType="centerCrop"
android:src="#drawable/b5"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView6"
android:scaleType="centerCrop"
android:src="#drawable/b6"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView7"
android:scaleType="centerCrop"
android:src="#drawable/b7"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView8"
android:scaleType="centerCrop"
android:src="#drawable/b8"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView9"
android:scaleType="centerCrop"
android:src="#drawable/b9"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView10"
android:scaleType="centerCrop"
android:src="#drawable/b10"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView11"
android:scaleType="centerCrop"
android:src="#drawable/b11"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView12"
android:scaleType="centerCrop"
android:src="#drawable/b12"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView13"
android:scaleType="centerCrop"
android:src="#drawable/b13"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView14"
android:scaleType="centerCrop"
android:src="#drawable/b14"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView16"
android:scaleType="centerCrop"
android:src="#drawable/b17"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView18"
android:scaleType="centerCrop"
android:src="#drawable/b18"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView19"
android:scaleType="centerCrop"
android:src="#drawable/b19"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView20"
android:scaleType="centerCrop"
android:src="#drawable/b20"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView21"
android:scaleType="centerCrop"
android:src="#drawable/b21"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView22"
android:scaleType="centerCrop"
android:src="#drawable/b22"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ImageView23"
android:scaleType="centerCrop"
android:src="#drawable/b23"/>
</ViewFlipper>
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignTop="#+id/next"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Frame Counter"
android:id="#+id/textView2"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Bair"
android:id="#+id/textView3"
android:layout_alignTop="#+id/previous"
android:layout_centerHorizontal="true" />
</RelativeLayout>
If your images are too high-resolution, they can eat up a LOT of memory, especially if you have 20 of them as you say. Consider using Picasso or some other library to load your images.

NullPointerException on a android Fragment

I’m trying to put some data into a bundle and transfer it to a fragment. But the log show me an error of NullPointerException in the fragment and I don't know what is the relation between the two.
The error code :
09-22 14:19:03.216 1918-1918/com.example.user.unchained E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user.unchained, PID: 1918
java.lang.NullPointerException
at com.example.user.unchained.HeaderFragement.onCreate(HeaderFragement.java:85)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1481)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
The Fragment code :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
name = (TextView) getView().findViewById(R.id.nameH);
img = (ImageView) getView().findViewById(R.id.imageViewHeader);
Bundle infos = getArguments();
name.setText(infos.getString("Name"));
img.setImageBitmap(getBitmapFromURL(infos.getString("ImgUrl")));
//Name.setText(Name.getText()+" "+infos.getString("Name"));
}
The Activity in which i put data in the bundle.
protected void onPostExecute(final String res) {
//LoginTask = null;
//showProgress(false);
try {
jObj = new JSONObject(res);
} catch (JSONException e) {
e.printStackTrace();
}
try {
if (jObj.getString("code").equals("1")) {
infos = new Bundle();
infos.putString("ID",jObj.getString("ID"));
infos.putString("Name",jObj.getString("display_name"));
infos.putString("ImgUrl","http://unchained-network.com/uploads/profilpics/53f5c570b6ac2.png");
android.support.v4.app.FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
HeaderFragement hf = new HeaderFragement();
//Charger les infos dans l'activité
hf.setArguments(infos);
tr.replace(R.id.frgmnt, hf);
tr.commit();
Intent myIntent = new Intent(getActivity(), HomesActivity.class);
//Lançer l'activité
startActivityForResult(myIntent, 0);
} else {
//password.setError(getString(R.string.error_incorrect_password));
//password.requestFocus();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Error !");
builder.setMessage("The information entered is incorrect.\nPlease try again!")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
The layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical" >
<!-- Login progress -->
<ProgressBar
android:id="#+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:visibility="gone"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/logo"
android:focusableInTouchMode="true"
android:src="#drawable/unchained_beta_logo"
android:layout_marginTop="20dp"
android:gravity="center"
android:contentDescription="Unchained-Network Logo"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
style="#style/SignInEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:completionThreshold="1"
android:inputType="textEmailAddress"
android:layout_marginTop="64dp"
android:layout_below="#+id/logo"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/email"
android:hint="Email" />
<EditText
style="#style/SignInEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:completionThreshold="1"
android:inputType="textPassword"
android:layout_marginTop="20dp"
android:layout_below="#+id/email"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/password"
android:hint="Password" />
<Button
android:id="#+id/connexion"
style="#style/ShadowText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:layout_below="#+id/password"
android:background="#color/green_android"
android:gravity="center"
android:padding="10dp"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"
android:text="Go Ride !"
/>
<TextView
android:layout_width="200dp"
android:id="#+id/passf"
style="#style/ShadowText"
android:layout_height="30dp"
android:layout_marginLeft="70dp"
android:text="Forgotten your password?"
android:layout_marginTop="20dp"
android:gravity="center"
android:textSize="10sp"
android:textStyle="bold"
android:textColor="#feed01" />
<TextView
android:layout_width="200dp"
style="#style/ShadowText"
android:layout_height="30dp"
android:layout_marginLeft="70dp"
android:text="No account yet ? Get one "
android:layout_marginTop="5dp"
android:gravity="center"
android:id="#+id/newaccount"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#fff" />
</LinearLayout>
</ScrollView>
</LinearLayout>
onCreate() is too early to call getView() and it will return null. Postpone the code that needs to touch the fragment's view hierarchy to onCreateView() or later in the fragment lifecycle.
you should add your fragment container inside your main layout. then you will do that replacement using this layout
<LinearLayout
android:id="#+id/frgmnt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
change your layout with this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical" >
<!-- Login progress -->
<ProgressBar
android:id="#+id/login_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:visibility="gone"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="5dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/logo"
android:focusableInTouchMode="true"
android:src="#drawable/unchained_beta_logo"
android:layout_marginTop="20dp"
android:gravity="center"
android:contentDescription="Unchained-Network Logo"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
style="#style/SignInEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:completionThreshold="1"
android:inputType="textEmailAddress"
android:layout_marginTop="64dp"
android:layout_below="#+id/logo"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/email"
android:hint="Email" />
<EditText
style="#style/SignInEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:completionThreshold="1"
android:inputType="textPassword"
android:layout_marginTop="20dp"
android:layout_below="#+id/email"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/password"
android:hint="Password" />
<Button
android:id="#+id/connexion"
style="#style/ShadowText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:layout_below="#+id/password"
android:background="#color/green_android"
android:gravity="center"
android:padding="10dp"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"
android:text="Go Ride !"
/>
<TextView
android:layout_width="200dp"
android:id="#+id/passf"
style="#style/ShadowText"
android:layout_height="30dp"
android:layout_marginLeft="70dp"
android:text="Forgotten your password?"
android:layout_marginTop="20dp"
android:gravity="center"
android:textSize="10sp"
android:textStyle="bold"
android:textColor="#feed01" />
<TextView
android:layout_width="200dp"
style="#style/ShadowText"
android:layout_height="30dp"
android:layout_marginLeft="70dp"
android:text="No account yet ? Get one "
android:layout_marginTop="5dp"
android:gravity="center"
android:id="#+id/newaccount"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#fff" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/frgmnt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>

Categories