Issue
Could anyone please help me as my android studio application pauses and freezes, when I try to run my application it run well however when I launch the app on my device it says *unfortunately app has stopped**
Main Java Class File
public class MainActivity extends Activity {
ImageButton btnsignin;
ImageButton btnsignup;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnsignin = (ImageButton) findViewById(R.id.btnsignin);
btnsignup = (ImageButton) findViewById(R.id.btnsignup);
btnsignin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Login.class);
startActivityForResult(myIntent, 0);
finish();
}
});
btnsignup.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Register.class);
startActivityForResult(myIntent, 0);
finish();
}
});
}}
Manifesta file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.brad.visor" >
<application
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login">
</activity>
<activity
android:name=".Register">
</activity>
<activity
android:name=".Registered">
</activity>
<activity
android:name=".Main">
</activity>
<activity
android:name=".PasswordReset">
</activity>
<activity
android:name=".ChangePassword">
</activity>
</application>
<!-- Allow to connect with internet and to know the current network state-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
LogCat File
Process: com.brad.visor, PID: 13838
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brad.visor/com.brad.visor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.brad.visor.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:6221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
06-24 21:46:04.285 941-1126/? E/WifiStateMachine﹕ WifiStateMachine CMD_START_SCAN source 10014 txSuccessRate=37.05 rxSuccessRate=33.84 targetRoamBSSID=c4:04:15:c1:13:70 RSSI=-39
06-24 21:46:04.295 941-1126/? E/WifiStateMachine﹕ [1,435,178,764,302 ms] noteScanStartWorkSource{10014} uid 10014
06-24 21:46:07.065 288-288/? E/SMD﹕ DCD ON
06-24 21:46:08.195 941-1126/? E/WifiStateMachine﹕ [1,435,178,768,204 ms] noteScanEnd WorkSource{10014}
06-24 21:46:08.215 941-1126/? E/WifiStateMachine﹕ wifi setScanResults statecom.android.server.wifi.WifiStateMachine$ConnectedState@667b28a sup_state=COMPLETED debouncing=false
06-24 21:46:08.345 941-941/? E/LocSvc_flp﹕ I/===> int flp_inject_location(FlpLocation*) line 222
06-24 21:46:08.345 941-1395/? E/LocSvc_ApiV02﹕ I/<--- void globalRespCb(locClientHandleType, uint32_t, locClientRespIndUnionType, void*) line 125 QMI_LOC_INJECT_POSITION_REQ_V02
06-24 21:46:10.065 288-288/? E/SMD﹕ DCD ON
06-24 21:46:13.066 288-288/? E/SMD﹕ DCD ON
06-24 21:46:16.075 288-288/? E/SMD﹕ DCD ON
06-24 21:46:19.076 288-288/? E/SMD﹕ DCD ON
06-24 21:46:22.075 288-288/? E/SMD﹕ DCD ON
06-24 21:46:25.075 288-288/? E/SMD﹕ DCD ON
06-24 21:46:27.745 941-1334/? E/Watchdog﹕ !@Sync 75
06-24 21:46:28.075 288-288/? E/SMD﹕ DCD ON
06-24 21:46:31.075 288-288/? E/SMD﹕ DCD ON
06-24 21:46:34.075 288-288/? E/SMD﹕ DCD ON
06-24 21:46:37.075 288-288/? E/SMD﹕ DCD ON
06-24 21:46:40.075 288-288/? E/SMD﹕ DCD ON
06-24 21:46:43.075 288-288/? E/SMD﹕ DCD ON
06-24 21:46:46.075 288-288/? E/SMD﹕ DCD ON
06-24 21:46:49.075 288-288/? E/SMD﹕ DCD ON
06-24 21:46:52.075 288-288/? E/SMD﹕ DCD ON
06-24 21:46:55.085 288-288/? E/SMD﹕ DCD ON
06-24 21:46:57.756 941-1334/? E/Watchdog﹕ !@Sync 76
06-24 21:46:58.086 288-288/? E/SMD﹕ DCD ON
06-24 21:47:01.085 288-288/? E/SMD﹕ DCD ON
06-24 21:47:04.085 288-288/? E/SMD﹕ DCD ON
06-24 21:47:05.485 941-1121/? E/Sensors﹕ Acc old sensor_state 0, new sensor_state : 1 en : 1
06-24 21:47:05.505 941-941/? E/LightSensor﹕ Light old sensor_state 1, new sensor_state : 513 en : 1
06-24 21:47:05.515 6462-6462/? E/SPPClientService﹕ [[PushClientService]] F:false, D:false, E:false, T:false, S:true, R:false
06-24 21:47:05.525 941-1126/? E/WifiStateMachine﹕ WifiStateMachine CMD_START_SCAN source 10014 txSuccessRate=37.05 rxSuccessRate=33.84 targetRoamBSSID=c4:04:15:c1:13:70 RSSI=-39
06-24 21:47:05.535 941-1126/? E/WifiStateMachine﹕ [1,435,178,825,536 ms] noteScanStartWorkSource{10014} uid 10014
06-24 21:47:05.765 941-1030/? E/LightSensor﹕ RED : 0, GREEN : 0, BLUE : 0, CLEAR : 0, CALCULATED LUX : 0.000000, CCT : 1596.000000, REAL LUX : 0.000000 a_time = 238, gain = 64, ir=0, rp1=1, gp1=0, bp1=0, cp1=0, cpl=3202560
06-24 21:47:05.765 941-1070/? E/LightSensor﹕ Light old sensor_state 513, new sensor_state : 1 en : 0
06-24 21:47:07.085 288-288/? E/SMD﹕ DCD ON
XML main file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:background="#0d47a1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/logo"
android:layout_weight="0.32"
android:contentDescription="@string/logonew" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/logo1"
android:textColor="#ffffff"
android:id="@+id/textView"
android:layout_gravity="center"
android:textSize="30sp"
android:layout_marginLeft="25dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/img_logo"
android:src="@drawable/logo"
android:layout_weight="1"
android:contentDescription="@string/logo2" />
</LinearLayout>
<TableRow
android:layout_width="match_parent"
android:layout_height="122dp"
android:background="@drawable/bgnew">
<ImageView
android:layout_width="match_parent"
android:layout_height="181dp"
android:id="@+id/img_mainbanner"
android:src="@drawable/banner"
android:background="#64b5f6"
android:contentDescription="@string/banner" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bgnew">
<ImageView
android:layout_width="match_parent"
android:layout_height="181dp"
android:id="@+id/img_banner2"
android:src="@drawable/bannernew"
android:background="#64b5f6"
android:contentDescription="@string/banner1" />
</TableRow>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="494dp"
android:background="#64b5f6"
android:id="@+id/relativeLayout">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnsignin"
android:background="@android:color/transparent"
android:layout_alignParentTop="false"
android:clickable="false"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true"
android:src="@drawable/signinbutton" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnsignup"
android:background="@android:color/transparent"
android:clickable="false"
android:layout_centerHorizontal="true"
android:src="@drawable/signupbutton"
android:layout_below="@+id/btnsignin" />
<DigitalClock
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/digitalClock"
android:textSize="45dp"
android:textColor="#ffffffff"
android:layout_below="@+id/btnsignup"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
Solution
Error happens in this line :
btnsignin = (ImageButton) findViewById(R.id.btnsignin);
btnsignin.setOnClickListener(...)
looks like you set incorrect layout or this view(R.id.btnsignin)
is not present in him.
Change main.xml
layout to :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:background="#0d47a1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/logo"
android:layout_weight="0.32"
android:contentDescription="@string/logonew" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/logo1"
android:textColor="#ffffff"
android:id="@+id/textView"
android:layout_gravity="center"
android:textSize="30sp"
android:layout_marginLeft="25dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/img_logo"
android:src="@drawable/logo"
android:layout_weight="1"
android:contentDescription="@string/logo2" />
</LinearLayout>
<TableRow
android:layout_width="match_parent"
android:layout_height="122dp"
android:background="@drawable/bgnew">
<ImageView
android:layout_width="match_parent"
android:layout_height="181dp"
android:id="@+id/img_mainbanner"
android:src="@drawable/banner"
android:background="#64b5f6"
android:contentDescription="@string/banner" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bgnew">
<ImageView
android:layout_width="match_parent"
android:layout_height="181dp"
android:id="@+id/img_banner2"
android:src="@drawable/bannernew"
android:background="#64b5f6"
android:contentDescription="@string/banner1" />
</TableRow>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="494dp"
android:background="#64b5f6"
android:id="@+id/relativeLayout">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnsignin"
android:background="@android:color/transparent"
android:layout_alignParentTop="false"
android:clickable="false"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true"
android:src="@drawable/signinbutton" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnsignup"
android:background="@android:color/transparent"
android:clickable="false"
android:layout_centerHorizontal="true"
android:src="@drawable/signupbutton"
android:layout_below="@id/btnsignin" />
<DigitalClock
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/digitalClock"
android:textSize="45dp"
android:textColor="#ffffffff"
android:layout_below="@id/btnsignup"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
See this question about the difference between
@id
and@+id
.
Answered By - Sergey Shustikov