I tried to implement tabs into my app and followed this tutorial:
http://android-er.blogspot.de/2012/06/create-actionbar-in-tab-navigation-mode.html
But now i have the problem that my TabListener does not take the parameters from my tabs.
tab1 class:
package com.example.MainActivity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.MainActivity.R;
public class Tab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
return myFragmentView;
}
}
and this is my TabListener:
public class TabListener <T extends Fragment> implements ActionBar.TabListener{
private final Activity myActivity;
private final String myTag;
private final Class myClass;
public TabListener(Activity activity, String tag, Class<T> cls) {
myActivity = activity;
myTag = tag;
myClass = cls;
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
// Check if the fragment is already initialized
if (myFragment == null) {
// If not, instantiate and add it to the activity
myFragment = Fragment.instantiate(myActivity, myClass.getName());
ft.add(android.R.id.content, myFragment, myTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(myFragment);
}
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
if (myFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(myFragment);
}
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
MainActivity:
public class MyActivity extends Activity {
private String string1 = "gahwareawbad password gagaw";
public String datum = new String("");
//STUNDENPLAN-VARIABLEN
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tabA = actionBar.newTab();
tabA.setText("Tab A");
tabA.setTabListener(new TabListener<Tab1>(this, "Tag A", Tab1.class));
actionBar.addTab(tabA);
Tab tabB = actionBar.newTab();
tabB.setText("Tab B");
tabB.setTabListener(new TabListener<Tab2>(this, "Tag B", Tab2.class));
actionBar.addTab(tabB);
Tab tabC = actionBar.newTab();
tabC.setText("Tab C");
tabC.setTabListener(new TabListener<Tab3>(this, "Tag C", Tab3.class));
actionBar.addTab(tabC);
if (savedInstanceState != null) {
int savedIndex = savedInstanceState.getInt("SAVED_INDEX");
getActionBar().setSelectedNavigationItem(savedIndex);
}
// quellcode = (TextView) findViewById(com.example.MainActivity.R.id.TextView01);
geschichte = (TextView) findViewById(com.example.MainActivity.R.id.geschichte);
datum1 = (TextView) findViewById(com.example.MainActivity.R.id.datum1);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putInt("SAVED_INDEX", getActionBar().getSelectedNavigationIndex());
}
}
MainXML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/tabHost">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/einstellungen"
android:layout_width="fill_parent android:layout_height="fill_parent" android:orientation="vertical" android:background="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/datum1" android:layout_gravity="center_horizontal"
android:textColor="#000000"
android:background="#f6fff4"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_column="0">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" android:text="Montag"
android:id="#+id/montagx" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" android:text="Dienstag"
android:id="#+id/dienstagx" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" android:text="Mittwoch"
android:id="#+id/mittwochx" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" android:text="Donnerstag"
android:id="#+id/donnerstagx" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" android:text="Freitag"
android:id="#+id/freitagx" android:textColor="#000000"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/montag1" android:layout_column="0" android:textColor="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/dienstag1" android:textColor="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/mittwoch1" android:textColor="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/donnerstag1" android:textColor="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/freitag1" android:textColor="#000000"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/montag2" android:textColor="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/dienstag2" android:textColor="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/mittwoch2" android:textColor="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/donnerstag2" android:textColor="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/freitag2" android:textColor="#000000"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/tableRow">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/montag3"
android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/dienstag3"
android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/mittwoch3"
android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/donnerstag3"
android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/freitag3"
android:textColor="#000000"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/tableRow2">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/montag4" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/dienstag4" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/mittwoch4" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/donnerstag4" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/freitag4" android:textColor="#000000"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/tableRow3">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/montag5"
android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/dienstag5"
android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/mittwoch5"
android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/donnerstag5"
android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" android:id="#+id/freitag5"
android:textColor="#000000"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/tableRow4">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/montag6" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/dienstag6" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/mittwoch6" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/donnerstag6" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/freitag6" android:textColor="#000000"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/tableRow5">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/montag7" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/dienstag7" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/mittwoch7" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/donnerstag7" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/freitag7" android:textColor="#000000"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="#+id/tableRow6">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/montag8" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/dienstag8" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/mittwoch8" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/donnerstag8" android:textColor="#000000"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/freitag8" android:textColor="#000000"/>
</TableRow>
</TableLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
03-30 22:09:51.720 441-876/com.android.launcher D/dalvikvm﹕ WAIT_FOR_CONCURRENT_GC blocked 692ms
03-30 22:09:51.997 441-877/com.android.launcher D/dalvikvm﹕ GC_FOR_ALLOC freed 1576K, 25% free 17107K/22564K, paused 176ms, total 189ms
03-30 22:09:52.027 2952-2952/? D/AndroidRuntime﹕ >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
03-30 22:09:52.057 2952-2952/? D/AndroidRuntime﹕ CheckJNI is ON
03-30 22:09:52.147 2952-2952/? D/dalvikvm﹕ Trying to load lib libjavacore.so 0x0
03-30 22:09:52.187 2952-2952/? D/dalvikvm﹕ Added shared lib libjavacore.so 0x0
03-30 22:09:52.227 2952-2952/? D/dalvikvm﹕ Trying to load lib libnativehelper.so 0x0
03-30 22:09:52.237 2952-2952/? D/dalvikvm﹕ Added shared lib libnativehelper.so 0x0
03-30 22:09:53.419 441-441/com.android.launcher I/Choreographer﹕ Skipped 55 frames! The application may be doing too much work on its main thread.
03-30 22:09:53.542 302-338/system_process W/RecognitionManagerService﹕ no available voice recognition services found for user 0
03-30 22:09:54.067 2952-2952/? D/AndroidRuntime﹕ Calling main entry com.android.commands.am.Am
03-30 22:09:54.077 2952-2952/? D/dalvikvm﹕ Note: class Landroid/app/ActivityManagerNative; has 157 unimplemented (abstract) methods
03-30 22:09:54.107 302-479/system_process I/ActivityManager﹕ START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.MainActivity/.MyActivity} from pid 2952
03-30 22:09:54.147 302-479/system_process W/WindowManager﹕ Failure taking screenshot for (328x546) to layer 21005
03-30 22:09:54.187 2962-2962/com.example.MainActivity D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
03-30 22:09:54.217 302-739/system_process I/ActivityManager﹕ Start proc com.example.MainActivity for activity com.example.MainActivity/.MyActivity: pid=2962 uid=10048 gids={50048, 3003, 1028}
03-30 22:09:54.227 2952-2952/? D/AndroidRuntime﹕ Shutting down VM
03-30 22:09:54.257 2952-2956/? D/dalvikvm﹕ GC_CONCURRENT freed 92K, 17% free 491K/588K, paused 1ms+1ms, total 27ms
03-30 22:09:54.257 2952-2958/? D/jdwp﹕ Got wake-up signal, bailing out of select
03-30 22:09:54.257 2952-2958/? D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
03-30 22:09:54.427 36-201/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property
03-30 22:09:55.067 2962-2962/com.example.MainActivity E/Trace﹕ error opening trace file: No such file or directory (2)
03-30 22:09:57.057 2962-2962/com.example.MainActivity D/AndroidRuntime﹕ Shutting down VM
03-30 22:09:57.057 2962-2962/com.example.MainActivity W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-30 22:09:57.077 2962-2962/com.example.MainActivity E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.MainActivity/com.example.MainActivity.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.MainActivity.MyActivity.onCreate(MyActivity.java:104)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
03-30 22:09:57.087 302-739/system_process W/ActivityManager﹕ Force finishing activity com.example.MainActivity/.MyActivity
03-30 22:09:57.117 302-739/system_process W/WindowManager﹕ Failure taking screenshot for (328x546) to layer 21010
03-30 22:09:57.397 36-490/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property
03-30 22:09:57.637 302-319/system_process W/ActivityManager﹕ Activity pause timeout for ActivityRecord{40e9e8c0 u0 com.example.MainActivity/.MyActivity}
03-30 22:09:59.227 302-329/system_process I/Choreographer﹕ Skipped 30 frames! The application may be doing too much work on its main thread.
Issue with the Import in MainActivity
change this
import android.app.ActionBar.TabListener;
to
import com.example.MainActivity.TabListener
or change your custom TabListener class name.
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
i'm getting java.lang.NullPointerException. And i don't know how to solve it. looking for some help here
What i'v tried:
to look here java.lang.RuntimeException: Unable to start activity ComponentInfo But, it didn't help me
to clean and Rebuild project.
to Invalidate Cache and Restart in Android studio
and i'v discovered some problems in LayoutInflater.java
It cant resolve some imports"
sorry cant import images yet this is and image of errors in LayoutInflater.java
My XML file activity_main.XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:text="Team A" />
<TextView
android:id="#+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="24dp"
android:gravity="center"
android:textSize="50sp"
android:text="0" />
<Button
android:id="#+id/three_points_button_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusThreePointsA"
android:text="+3 Points" />
<Button
android:id="#+id/two_points_button_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusTwoPointsA"
android:text="+2 Points" />
<Button
android:id="#+id/free_throw_button_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusOnePointA"
android:text="Free Throw" />
</LinearLayout>
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:text="Team B" />
<TextView
android:id="#+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="24dp"
android:gravity="center"
android:textSize="50sp"
android:text="0" />
<Button
android:id="#+id/three_points_button_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusThreePointsB"
android:text="+3 Points" />
<Button
android:id="#+id/two_points_button_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusTwoPointsB"
android:text="+2 Points" />
<Button
android:id="#+id/free_throw_button_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusOnePointB"
android:text="Free Throw" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|center"
android:orientation="vertical">
<Button
android:id="#+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="24dp"
android:onClick="reset"
android:text="Reset" />
</LinearLayout>
</LinearLayout>
My MainActivity.java
package com.example.problems;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
int scoreA = 0;
int scoreB = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void plusThreePointsA(View view){
scoreA = scoreA+3;
displayScoreA(scoreA);
}
public void plusTwoPointsA(View view){
scoreA = scoreA+2;
displayScoreA(scoreA);
}
public void plusOnePointA(View view){
scoreA = scoreA+1;
displayScoreA(scoreA);
}
public void plusThreePointsB(View view){
scoreB = scoreB+3;
displayScoreB(scoreB);
}
public void plusTwoPointsB(View view){
scoreB = scoreB+2;
displayScoreB(scoreB);
}
public void plusOnePointB(View view){
scoreB = scoreB+1;
displayScoreB(scoreB);
}
public void displayScoreA (int score){
TextView viewScore = findViewById(R.id.team_a_score);
viewScore.setText(String.valueOf(score));
}
public void displayScoreB (int score){
TextView viewScore = findViewById(R.id.team_b_score);
viewScore.setText(String.valueOf(score));
}
public void reset (View view){
scoreA = 0;
scoreB = 0;
displayScoreA(scoreA);
displayScoreB(scoreB);
}
}
Error that i get
--------- beginning of crash
05-11 08:14:25.720 4084-4084/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.problems, PID: 4084
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.problems/com.example.problems.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:715)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.problems.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
05-11 08:19:26.000 4084-4084/com.example.problems I/Process: Sending signal. PID: 4084 SIG: 9
Use :
<View android:layout_width="1dp" android:layout_height="match_parent" android:background="#android:color/darker_gray" />
instead of :
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
/>
i just want to fill some infos in the activitymain and then pass an intent to a second activity, at the first it works perfect but when i add some xml layout, when running the app on device and pass the intent it crashes
the main activity
public void move(View view) {
Intent i = new Intent(MainActivity.this, FormActivity.class);
EditText name = findViewById(R.id.name);
EditText animal = findViewById(R.id.animal);
String n = name.getText().toString();
String a = animal.getText().toString();
if (n.isEmpty() || a.isEmpty()) {
Toast.makeText(this, "nom ou animal est vide !", Toast.LENGTH_SHORT).show();
} else {
i.putExtra("KEY_Name", name.getText().toString());
i.putExtra("KEY_Animal", animal.getText().toString());
startActivity(i);
}
}
// and the formactivity or the second
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
Intent i = getIntent();
TextView Text1 = findViewById(R.id.t1);
TextView Text2 = findViewById(R.id.t2);
String name = i.getStringExtra("KEY_Name");
String animal = i.getStringExtra("KEY_Animal");
Text1.setText(name);
Text2.setText(animal);
}
// XML file for activity_main it works perfect
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/dog" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="40dp"
android:src="#drawable/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:shadowColor="#000000"
android:shadowDx="-2"
android:shadowDy="-2"
android:shadowRadius="1"
android:text="#string/hello"
android:textColor="#ffffff"
android:textSize="26sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView"
app:layout_constraintVertical_bias="0.075" />
<LinearLayout
android:id="#+id/linear1"
style="#style/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_weight="1"
android:shadowColor="#000000"
android:shadowDx="-2"
android:shadowDy="-2"
android:shadowRadius="1"
android:text="#string/name"
android:textColor="#ffffff" />
<View
android:layout_width="3dip"
android:layout_height="match_parent"
android:background="#ffffff" />
<EditText
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="#string/ex_name"
android:inputType="textCapWords"
android:maxLines="1" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#ffffff"
app:layout_constraintBottom_toTopOf="#+id/linear2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/linear2" />
<LinearLayout
android:id="#+id/linear2"
style="#style/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linear1">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_weight="1"
android:shadowColor="#000000"
android:shadowDx="-2"
android:shadowDy="-2"
android:shadowRadius="1"
android:text="#string/pet"
android:textColor="#ffffff" />
<View
android:layout_width="3dip"
android:layout_height="match_parent"
android:background="#ffffff" />
<EditText
android:id="#+id/animal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="#string/ex_animal"
android:inputType="textCapWords"
android:maxLines="1" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:onClick="move"
android:text="Suivant"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.096"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linear2"
app:layout_constraintVertical_bias="0.344" />
// the activity_form or second that crashes
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/sheep" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:foregroundGravity="center_horizontal"
android:src="#drawable/title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="16dp"
android:background="#drawable/list_fluid"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="#+id/t1"
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center_horizontal"
android:text="Hello freind!"
android:textColor="#color/green"
android:textSize="24sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginBottom="12dp"
android:layout_marginTop="12dp"
android:background="#ffffff" />
<TextView
android:id="#+id/t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="oh, puppy!"
android:textColor="#color/green"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="12dp"
android:text="comment Aidez-Vous ?" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="mon Chat souffre de fièvre"
android:inputType="text"
android:lines="6"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:textColor="#FFFFFF"
android:textColorHint="#cccccc"
android:textSize="12sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="12dp"
android:background="#color/logo"
android:text="envoyer"
android:textColor="#ffffff"
android:textSize="16sp" />
</LinearLayout>
the logcat
10-09 18:03:08.327 29724-29724/com.example.android.workingproject W/ResourceType: Failure getting entry for 0x01080946 (t=7 e=2374) (error -75)
10-09 18:04:20.737 29724-29724/com.example.android.workingproject W/ResourceType: Failure getting entry for 0x01080946 (t=7 e=2374) (error -75)
10-09 18:04:21.538 29724-29724/com.example.android.workingproject E/art: Throwing OutOfMemoryError "Failed to allocate a 343944636 byte allocation with 16777120 free bytes and 52MB until OOM"
10-09 18:04:21.608 29724-29724/com.example.android.workingproject E/art: Throwing OutOfMemoryError "Failed to allocate a 343944636 byte allocation with 16777120 free bytes and 52MB until OOM"
10-09 18:04:21.628 29724-29724/com.example.android.workingproject E/art: li.han, Prepare to dump hprof for OOM Error !
li.han, Directory [/data/log] could not be write !
10-09 18:04:21.638 29724-29724/com.example.android.workingproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.workingproject, PID: 29724
java.lang.OutOfMemoryError: Failed to allocate a 343944636 byte allocation with 16777120 free bytes and 52MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:837)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:656)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1037)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:4056)
at android.content.res.Resources.loadDrawable(Resources.java:3929)
at android.content.res.Resources.loadDrawable(Resources.java:3779)
at android.content.res.TypedArray.getDrawable(TypedArray.java:776)
at android.widget.ImageView.<init>(ImageView.java:151)
at android.widget.ImageView.<init>(ImageView.java:140)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:732)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.android.workingproject.FormActivity.onCreate(FormActivity.java:13)
at android.app.Activity.performCreate(Activity.java:6609)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3103)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6934)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
At least one of the drawables referenced in your second XML file is too big.
Check the following drawables and resize them:
sheep
title
list_fluid
PNGs generally don't need to be very large. Keep them below 1600x1600 if you can, preferably 800x800.
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.
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.
Here is the Java Code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_create_report, container, false);
activity = getActivity();
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinnerComplaintType);
ArrayAdapter<String> adapterComplaint = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, R.array.complaintType);
adapterComplaint.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapterComplaint);
spinnerComplaintType.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
complaintType = parent.getItemAtPosition(position).toString();
// Toast.makeText(getApplicationContext(), complaintType, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return rootView;
}
Here is the XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="833dp" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="24dp"
android:layout_marginTop="54dp"
android:text="Complaint Type: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/spinnerComplaintType"
android:layout_marginTop="15dp"
android:text="Vehicle Type"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinnerVehicleType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/textView2"
android:textColor="#000000" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/spinnerVehicleType"
android:layout_marginTop="21dp"
android:text="Body No:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editTextBodyNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView4"
android:layout_centerHorizontal="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTextPlate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextBodyNo"
android:layout_alignTop="#+id/textView5"
android:ems="10" />
<EditText
android:id="#+id/editTextDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextLocation"
android:layout_alignTop="#+id/textView7"
android:ems="10" />
<EditText
android:id="#+id/editTextComplaintDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextDate"
android:layout_alignTop="#+id/textView8"
android:ems="10"
android:inputType="textMultiLine" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView5"
android:layout_below="#+id/textView5"
android:layout_marginTop="58dp"
android:text="Location"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/textView4"
android:layout_marginTop="55dp"
android:text="Plate No:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/editTextLocation"
android:layout_marginTop="30dp"
android:text="Date of Incident"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/editTextDate"
android:layout_marginTop="28dp"
android:text="Complaint Details"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editTextLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextPlate"
android:layout_alignTop="#+id/textView6"
android:ems="10" />
<ImageView
android:id="#+id/imageViewPhotoReport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonCamera"
android:layout_below="#+id/buttonCamera"
android:src="#drawable/about"
android:visibility="invisible"/>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:text="File Complaint"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/buttonCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonSend"
android:layout_toLeftOf="#+id/spinnerVehicleType"
android:text="Take Photo" />
<Button
android:id="#+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editTextComplaintDetails"
android:layout_marginTop="48dp"
android:layout_toRightOf="#+id/textView1"
android:text="SEND" />
<Button
android:id="#+id/buttonDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextComplaintDetails"
android:layout_below="#+id/editTextComplaintDetails"
android:layout_marginTop="20dp"
android:text="pick date" />
<Spinner
android:id="#+id/spinnerComplaintType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_toRightOf="#+id/textView3"
android:textColor="#000000" />
</RelativeLayout>
</ScrollView>
Here is the Error
Log Trace
01-04 21:18:36.513: E/AndroidRuntime(3772): FATAL EXCEPTION: main
01-04 21:18:36.513: E/AndroidRuntime(3772): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Spinner
01-04 21:18:36.513: E/AndroidRuntime(3772): at info.androidhive.slidingmenu.CreateReportFragment.onCreateView(CreateReportFragment.java:31)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:830)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1034)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.BackStackRecord.run(BackStackRecord.java:622)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1384)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:428)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.os.Handler.handleCallback(Handler.java:605)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.os.Handler.dispatchMessage(Handler.java:92)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.os.Looper.loop(Looper.java:154)
01-04 21:18:36.513: E/AndroidRuntime(3772): at android.app.ActivityThread.main(ActivityThread.java:4945)
01-04 21:18:36.513: E/AndroidRuntime(3772): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 21:18:36.513: E/AndroidRuntime(3772): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 21:18:36.513: E/AndroidRuntime(3772): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-04 21:18:36.513: E/AndroidRuntime(3772): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-04 21:18:36.513: E/AndroidRuntime(3772): at dalvik.system.NativeStart.main(Native Method)
So I was able to make a Spinner work in a single Activity, but when I transferred the code to a Fragment it obviously didn't work.
I think the Error was from the Instatiation of Spinner.
Use this one for your TextView and Spinner.
<Spinner
android:id="#+id/spinnerComplaintType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="54dp"
android:layout_toRightOf="#+id/buttonSend"
android:textColor="#000000" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignTop="#+id/spinnerComplaintType"
android:layout_marginTop="17dp"
android:text="File Complaint"
android:textAppearance="?android:attr/textAppearanceLarge" />
Also Change
android.R.layout.simple_spinner_item
instead of
android.R.layout.simple_list_item_1
in your Array Adapter.
UPDATE:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.complaintType, android.R.layout.simple_spinner_item);