Crashing Activity when button clicked in Android studio


Crashing Activity when button clicked in Android studio



need some help with android. I'm new to android development so please bear with me.



I have two activity and I am able to successfully go to the second activity by a button click event(intent), however once I click a button in the second activity, the activity crashes and goes back to the main activity.



here is my codes:


public boolean insertEmployee(String name, int pin, int roleid)
{
mydb= getWritableDatabase();
ContentValues contentvalue= new ContentValues();`enter code here`
contentvalue.put(emp_name, name);
contentvalue.put(emp_Pin,pin);
contentvalue.put(emp_roleID,roleid);
long result=mydb.insert(emp,null,contentvalue);
if(result==-1)
return false;
else
return true;


}



and on my secondactivity.java


public void insertEmployee()
{
btnemployee.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isSuccess = dbhelp.insertEmployee(name.getText().toString(), Integer.parseInt(pin.getText().toString()), Integer.parseInt(roleid.getText().toString()));


}
}
);
}



and manifest.xml


<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=".MainActivity2Activity"
android:label="@string/app_name" >

</activity>



Just can't seems to understand but if I have only one activity the insert method works.
Please help. Thanks guys



LOgcat


06-29 16:07:29.125 15133-15133/
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: "I REMOVED APP NAME", PID: 15133
java.lang.NullPointerException
at
.MainActivity2Activity$1.onClick(MainActivity2Activity.java:45)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18439)
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:5095)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
at dalvik.system.NativeStart.main(Native Method)



XML


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert Employee"
android:id="@+id/binsertEmployee"
android:layout_below="@+id/bRoleinsert"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/bRoleinsert"
android:layout_alignEnd="@+id/bRoleinsert" />



mainactivity2.java


public class MainActivity2Activity extends AppCompatActivity {
DatabaseHelper dbhelp;
Button btnemployee, btnrole;
EditText role,name,pin,roleid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
btnrole=(Button)findViewById(R.id.bRoleinsert);
btnemployee=(Button)findViewById(R.id.binsertEmployee);
role=(EditText)findViewById(R.id.editText2);
name=(EditText)findViewById(R.id.editText3);
pin=(EditText)findViewById(R.id.editText4);
roleid=(EditText)findViewById(R.id.editText5);

InsertRole();
insertEmployee();
}





please post error log
– dev
2 days ago





Crashing Activity when button clicked Where is you crash log? your need to share that crash report
– Nilesh Rathod
2 days ago


Crashing Activity when button clicked





please send all code and also error log
– Hossein Seifi
2 days ago





Logcat and Debugging are 2 best friends of Android developer.
– Paresh Mayani
2 days ago


Logcat


Debugging





I think ur running in main thread!!!
– Gobu CSG
2 days ago




2 Answers
2



Try this


btnemployee.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
dbhelp.insertEmployee(name.getText().toString(), Integer.parseInt(pin.getText().toString()), Integer.parseInt(roleid.getText().toString()));


}
}
);





Explain your changes
– ZUNJAE
2 days ago



You have a NullPointerException error at your btnemployee, which means that the button was never initalized. You may have made it a global variable with Button btnemployee, but you may have not actually assigned it to the button in your XML file. Assuming for example that your button in the XML has an id of btnEmployee, you would need to assign it like this in your onCreate(Bundle savedInstanceState) method:


Button btnemployee


onCreate(Bundle savedInstanceState)



btnemployee = findViewById(R.id.btnEmployee)


btnemployee = findViewById(R.id.btnEmployee)



Make sure that your button in your layout has an id assigned, like:



android:id="@+id/btnEmployee"


android:id="@+id/btnEmployee"



With that, the resource will be available to your code, in the form of R.id.btnEmployee.


R.id.btnEmployee



While you must make sure to assign the value in the onCreate method, you must not declare the variable there, or it will be in the scope of the onCreate method only. Instead you have to be sure to declare the type in the class scope.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV