Unable to start new activity involving Firebase - android

I am building an app that has a charity and restaurant. They register themselves and then proceed using Firebase for database handling. When I register charity register charity should open but it shows error. Logcat and register charity file attached.
activity register charity
package com.example.aayu.myapplication;
import android.content.Intent;
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.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
/**
* Created by AAYU on 30-May-17.
*/
public class regc extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reg_c);
final DatabaseReference rootref = FirebaseDatabase.getInstance()
.getReferenceFromUrl("https://myapplication-6e0b9.firebaseio.com/charity");
final EditText mname = (EditText) findViewById(R.id.cname);
final EditText madd = (EditText) findViewById(R.id.cadd);
final EditText mcity = (EditText) findViewById(R.id.ccity);
final EditText mstate = (EditText) findViewById(R.id.cstate);
final EditText mpin = (EditText) findViewById(R.id.cpin);
final Button mnext= (Button) findViewById(R.id.cnext);
mnext.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
String name = mname.getText().toString();
String add = madd.getText().toString();
String city = mcity.getText().toString();
String state = mstate.getText().toString();
String pin = mpin.getText().toString();
DatabaseReference childref = rootref.child("Charity Name");
childref.setValue(name);
childref = rootref.child("Address");
childref.setValue(add);
childref = rootref.child("City");
childref.setValue(city);
childref = rootref.child("State");
childref.setValue(state);
childref = rootref.child("Pincode");
childref.setValue(pin);
}
});
//setContentView(R.layout.reg2_c);
final EditText memail = (EditText) findViewById(R.id.cemail);
final EditText mph = (EditText) findViewById(R.id.cph);
final EditText muname = (EditText) findViewById(R.id.cuname);
final EditText mpwd = (EditText) findViewById(R.id.cpwd);
final Button mreg=(Button) findViewById(R.id.creg);
mreg.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
String email = memail.getText().toString();
String ph = mph.getText().toString();
String uname = muname.getText().toString();
String pwd = mpwd.getText().toString();
DatabaseReference childref = rootref.child("Email Id");
childref.setValue(email);
childref = rootref.child("Phone No");
childref.setValue(ph);
childref = rootref.child("Username");
childref.setValue(uname);
childref = rootref.child("Password");
childref.setValue(pwd);
Toast.makeText(view.getContext(), "Registration done successfully", Toast.LENGTH_SHORT).show();
}
});
// Intent loginc = new Intent(this, loginc.class);
// startActivity(loginc);
}
}
logcat
W/GooglePlayServicesUtil: Google Play services out of date. Requires 11011000 but found 9877470
V/FA: Activity paused, time: 32230643
V/FA: Processing queued up service tasks: 2
E/FA: Failed to send current screen to service
E/FA: Discarding data. Failed to send event to service
D/AndroidRuntime: Shutting down VM
E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aayu.myapplication/com.example.aayu.myapplication.regc}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.aayu.myapplication.regc.onCreate(regc.java:76)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aayu.myapplication, PID: 9023
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aayu.myapplication/com.example.aayu.myapplication.regc}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.aayu.myapplication.regc.onCreate(regc.java:76)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
Application terminated.
reg_c.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginStart="51dp"
android:layout_marginTop="49dp"
android:id="#+id/cname"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:hint="Charity Home Name"
android:selectAllOnFocus="true" />
<Button
android:text="NEXT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cnext"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#color/colorPrimary"
android:textStyle="normal|bold"
android:textSize="20sp"
android:elevation="21dp"
android:onClick="next" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="24dp"
android:id="#+id/cadd"
android:layout_below="#+id/cname"
android:layout_alignStart="#+id/cname"
android:hint="Address "
android:selectAllOnFocus="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="36dp"
android:id="#+id/ccity"
android:hint="City"
android:selectAllOnFocus="true"
android:layout_below="#+id/cadd"
android:layout_alignStart="#+id/cadd" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="48dp"
android:id="#+id/cstate"
android:hint="State"
android:selectAllOnFocus="true"
android:layout_below="#+id/ccity"
android:layout_alignStart="#+id/ccity" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_marginTop="34dp"
android:id="#+id/cpin"
android:hint="Pincode"
android:selectAllOnFocus="true"
android:layout_below="#+id/cstate"
android:layout_alignStart="#+id/cstate" />
</RelativeLayout>
layout for reg_c.xml

The issue is here:
final Button mreg=(Button) findViewById(R.id.creg);
mreg.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
String email = memail.getText().toString();
String ph = mph.getText().toString();
String uname = muname.getText().toString();
String pwd = mpwd.getText().toString();
DatabaseReference childref = rootref.child("Email Id");
childref.setValue(email);
childref = rootref.child("Phone No");
childref.setValue(ph);
childref = rootref.child("Username");
childref.setValue(uname);
childref = rootref.child("Password");
childref.setValue(pwd);
Toast.makeText(view.getContext(), "Registration done successfully", Toast.LENGTH_SHORT).show();
}
});
You don't have any button with this id in your XML file but you are trying to access it in your Activity. That is causing Null Pointer Exception.
You have two solution:
If you need this button then add a button with the id "creg" in your XML file.
OR
If you don't need that button then remove the code given above from your Activity.

Related

I keep getting a error when I execute the send Credentials void

When I run my code i get a error when the SendCredentials void is executed.
The error is:-
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.messager, PID: 16131
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6614)
at android.view.View.performClickInternal(View.java:6591)
at android.view.View.access$3100(View.java:786)
at android.view.View$PerformClick.run(View.java:25948)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6614) 
at android.view.View.performClickInternal(View.java:6591) 
at android.view.View.access$3100(View.java:786) 
at android.view.View$PerformClick.run(View.java:25948) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:201) 
at android.app.ActivityThread.main(ActivityThread.java:6806) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.DataInputStream.readLine()'
on a null object reference
at com.example.messager.MainActivity.SendCredentials(MainActivity.java:69)
at com.example.messager.MainActivity.SendMessage(MainActivity.java:26)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6614) 
at android.view.View.performClickInternal(View.java:6591) 
at android.view.View.access$3100(View.java:786) 
at android.view.View$PerformClick.run(View.java:25948) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:201) 
at android.app.ActivityThread.main(ActivityThread.java:6806) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
My Code is
package com.example.messager;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import java.net.*;
import java.io.*;
public class MainActivity extends AppCompatActivity {
EditText stopPort;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void SendMessage(View view)throws IOException{
EditText emailText = (EditText) findViewById(R.id.Email);
String Username = emailText.getText().toString();
EditText passwordText = (EditText) findViewById(R.id.Password);
String Password = passwordText.getText().toString();
System.out.println("Your username is " + Username + "Your Password is " + Password);
SendCredentials();
}
public void SendCredentials()throws IOException {
System.out.println("Work");
Socket socket = new Socket("192.168.0.114", 4321);
System.out.println("Work");
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
dOut.writeByte(1);
dOut.writeUTF("This is a test");
dOut.flush();
System.out.println("Work");
}
public void Check(){
}
}
`
 
Looking to above code and exception trace 2 things can be wrong.
check your code for
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.DataInputStream.readLine()' on a null object reference at
In xml layout file, have you declared "check" method on android:onClick
i.e. android:onClick="check", if yes then your check method should look like this
public void Check(View view){
}
Can you share you xml layout file as well.
yea my xml code is '
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/Welcome"
android:layout_width="198dp"
android:layout_height="52dp"
android:layout_marginStart="104dp"
android:layout_marginTop="68dp"
android:text="Welcome"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#color/colorPrimaryDark"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.TextInputLayout
android:id="#+id/passwordWrapper"
android:layout_width="387dp"
android:layout_height="56dp"
android:layout_marginStart="8dp"
android:layout_marginTop="444dp"
android:hint="Password"
app:hintEnabled="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/Password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Password"
android:inputType="textPassword"
android:textSize="18sp"
tools:text="" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/emailWrapper"
android:layout_width="397dp"
android:layout_height="56dp"
android:layout_marginStart="8dp"
android:layout_marginTop="368dp"
android:hint="Email"
app:hintEnabled="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/Email"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Email"
android:inputType="textWebEmailAddress"
android:textAlignment="textStart"
android:textSize="18sp"
tools:text=""/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/LoginButton"
android:layout_width="348dp"
android:layout_height="45dp"
android:layout_marginStart="28dp"
android:layout_marginTop="520dp"
android:text="LOGIN"
android:onClick="SendMessage"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
'
public void SendMessage(View view) throws IOException {
EditText emailText = (EditText) findViewById(R.id.Email);
String Username = emailText.getText().toString();
EditText passwordText = (EditText) findViewById(R.id.Password);
String Password = passwordText.getText().toString();
System.out.println("Your username is " + Username + "Your Password is " + Password);
SendCredentials();
}
public void SendCredentials() throws IOException {
new MyAsyncTask().execute();
}
public void Check() {
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
Activity activity;
IOException ioException;
MyAsyncTask() {
super();
}
#Override
protected Void doInBackground(Void... voids) {
StringBuilder sb = new StringBuilder();
try {
System.out.println("Work");
Socket socket = new Socket("192.168.0.114", 4321);
System.out.println("Work");
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
dOut.writeByte(1);
dOut.writeUTF("This is a test");
dOut.flush();
System.out.println("Work");
socket.close();
} catch (IOException e) {
this.ioException = e;
}
}
}

Getting IllegalStateException on Button Click - caused by: java.lang.NullPointerException [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I tried to add data to sqlite, but when I clicked Add Button error appear and my app closed. I don't understand, so many wasted hours for looking the solution by myself..
here is my java code, AddSampleActivity.java
package com.example.mangan;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddSampleActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editNama, editJarak, editRating , editHarga;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_sample);
editNama = (EditText) findViewById(R.id.nama);
editJarak = (EditText) findViewById(R.id.jarak);
editRating = (EditText) findViewById(R.id.rating);
editHarga = (EditText) findViewById(R.id.harga);
myDb = new DatabaseHelper(this);
}
public void addData(View view) {
boolean isInserted = myDb.insertData(editNama.getText().toString(),
editJarak.getText().toString(),
editRating.getText().toString(),
editHarga.getText().toString() );
if(isInserted)
Toast.makeText(AddSampleActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(AddSampleActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
}
my layout, activity_add_sample.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context="com.example.mangan.AddSampleActivity">
<EditText
android:id="#+id/jarak_editText"
android:hint="Jarak"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/rating_editText"
android:hint="Rating"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/harga_editText"
android:hint="Harga"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addData"
android:text="Add"/>
</LinearLayout>
This is fullstack trace
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5675)
at android.view.View$PerformClick.run(View.java:22651)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:208)
at android.app.ActivityThread.main(ActivityThread.java:6267)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5675) 
at android.view.View$PerformClick.run(View.java:22651) 
at android.os.Handler.handleCallback(Handler.java:836) 
at android.os.Handler.dispatchMessage(Handler.java:103) 
at android.os.Looper.loop(Looper.java:208) 
at android.app.ActivityThread.main(ActivityThread.java:6267) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.mangan.AddSampleActivity.addData(AddSampleActivity.java:30)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5675) 
at android.view.View$PerformClick.run(View.java:22651) 
at android.os.Handler.handleCallback(Handler.java:836) 
at android.os.Handler.dispatchMessage(Handler.java:103) 
at android.os.Looper.loop(Looper.java:208) 
at android.app.ActivityThread.main(ActivityThread.java:6267) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 
It seem that some content on addData method is the problem. I read another question, Getting IllegalStateException on button click, but the problem is little bit different..
Your edittext ids aren't correct. change them like below
editJarak = (EditText) findViewById(R.id.jarak_editText);
editRating = (EditText) findViewById(R.id.rating_editText);
editHarga = (EditText) findViewById(R.id.harga_editText);
Your edit text ids are wrong. Try this,
editNama = (EditText) findViewById(R.id.nama_editText);
editJarak = (EditText) findViewById(R.id.jarak_editText);
editRating = (EditText) findViewById(R.id.rating_editText);
editHarga = (EditText) findViewById(R.id.harga_editText);

why does my app crash when I use the setText Mehod? [duplicate]

This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
(8 answers)
Closed 5 years ago.
QUESTION:
Why is my app crashing whenever I try to use the setText() method to assign random Numbers as my Buttons' text?
My Goal is to set random numbers to the buttons and if the user clicks the button with the larger number, he/she is awarded a point.
I debugged the program and found the logic to be working correctly. However the app keeps crashing at startup and I don't know what exactly is the underlying problem.
MAIN ACTIVITY:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private static int points, rand1, rand2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
points =0;
randomize();
}
public void onLeftClick(View view) {
if(rand1 > rand2)
points++;
else
points--;
TextView text = (TextView) findViewById(R.id.pointText);
text.setText("Points : "+points);
randomize();
}
public void onRightClick(View view) {
if(rand2 > rand1)
points++;
else
points--;
TextView text = (TextView) findViewById(R.id.pointText);
text.setText("Points : "+points);
randomize();
}
private void randomize() {
Random random = new Random();
rand1= random.nextInt(10);
rand2 = 0;
while (true) {
rand2 = random.nextInt(10);
if (rand1 != rand2)
break;
}
Button leftButton = (Button)findViewById(R.id.leftButton);
leftButton.setText((char)rand1); **ERROR HERE**
Button rightButton = (Button)findViewById(R.id.rightButton);
rightButton.setText((char)rand2);
}
}
XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.clickgame.MainActivity">
<TextView
android:layout_width="119dp"
android:layout_height="24dp"
android:text="CLICK GAME"
android:textAlignment="center"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.032"
tools:layout_constraintRight_creator="1"
tools:layout_constraintLeft_creator="1" />
<Button
android:id="#+id/leftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="215dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="216dp"
android:onClick="onLeftClick"
android:text="0"
app:layout_constraintBottom_toTopOf="#+id/pointText"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1" />
<Button
android:id="#+id/rightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="215dp"
android:layout_marginEnd="18dp"
android:layout_marginRight="18dp"
android:layout_marginTop="216dp"
android:onClick="onRightClick"
android:text="0"
app:layout_constraintBottom_toTopOf="#+id/pointText"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />
<TextView
android:id="#+id/pointText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="POINTS : 0"
android:textStyle="bold"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="16dp"
app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>
ERROR LOG:
09-10 14:51:53.742 478-478/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.clickgame, PID: 478
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.clickgame/com.example.clickgame.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x6
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x6
at android.content.res.Resources.getText(Resources.java:335)
at android.widget.TextView.setText(TextView.java:4555)
at com.example.clickgame.MainActivity.randomize(MainActivity.java:66) //THIS LINE
at com.example.clickgame.MainActivity.onCreate(MainActivity.java:22) //THIS LINE
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
To add to the answer of Amirhossein Naghshzan what is happening here is that the setText method of TextView is capable of using a resourceId as a parameter. When you pass in and integer to this method it tries to find a string resource with that id. If it can't find one an exception is thrown hence the need to wrap a single number value with String.valueOf()
Use String.valueof() to cast int to string:
leftButton.setText(String.valueof(rand1));
rightButton.setText(String.valueof(rand2));
Use String.valueof(), in the brackets of .settext()
Sample:
TextView text = (TextView) findViewById(R.id.pointText);
text.setText("Points : "+ String.valueof(points));
If you glance on this java documentation, then you could see that String.valueOf() returns:
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
So there shouldn't really be a difference except for an additional method invocation.
Also, in case of Object#toString, if the instance is null, a NullPointerException will be thrown, so, arguably, it's less safe.
i have cleaned your activity, use this one instead :
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private int
points = 0,
rand1,
rand2;
private TextView text;
private Button leftButton, rightButton;
private Random random = new Random();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.pointText);
leftButton = (Button)findViewById(R.id.leftButton);
leftButton.setOnClickListener(this);
rightButton = (Button)findViewById(R.id.rightButton);
rightButton.setOnClickListener(this);
randomize();
}
private void randomize() {
rand1 = random.nextInt(10);
rand2 = 0;
while (true) {
rand2 = random.nextInt(10);
if (rand1 != rand2)
break;
}
leftButton.setText(String.valueOf(rand1));
rightButton.setText(String.valueOf(rand2));
}
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.leftButton{
if(rand1 > rand2)
points++;
else
points--;
text.setText("Points : " + String.valueOf(points));
randomize();
}break;
case R.id.rightButton:{
if(rand2 > rand1)
points++;
else
points--;
text.setText("Points : " + String.valueOf(points));
randomize();
}break;
}
}
}
Your Error is:
Resources$NotFoundException: String resource ID
you have to set the ID on your View item:
<TextView
android:id="#+id/pointText"

Android studio app crashing when going to second activity [duplicate]

This question already has answers here:
Resource not found TextView
(2 answers)
Closed 6 years ago.
I"m working on my application, where a user enters a ip address and port number and will connect to the server. As soon as I fill in the ip address and port number and press on connect, the application crashes. These are the errors i'm getting.
EDIT: added xml for both activities
EDIT2: Changed the textview ID and it's still crashing, new error message all the way down.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jobush50.irobot, PID: 2285
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jobush50.irobot/com.example.jobush50.irobot.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x22b8
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x22b8
at android.content.res.Resources.getText(Resources.java:331)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:4550)
at com.example.jobush50.irobot.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
My MainActivity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
static int port;
static String ip;
private Socket socket;
private static final int SERVERPORT = port;
private static final String SERVER_IP = ip;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new ClientThread()).start();
Intent intent = getIntent();
String name = intent.getStringExtra("ip");
int number = intent.getIntExtra("port", 0);
port = number;
ip = name;
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(name);
TextView textView2 = (TextView) findViewById(R.id.textView2);
textView2.setText(number);
}
public void onClick(View view) {
try {
EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(str);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
class ClientThread implements Runnable {
#Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
SecondActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.widget.EditText;
import android.content.Intent;
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
final EditText editText = (EditText)findViewById(R.id.editText_port);
final EditText editText2 = (EditText)findViewById(R.id.editText_port);
Button sendButton = (Button)findViewById(R.id.button_send);
sendButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
String name = editText.getText().toString();
int number = Integer.parseInt(editText2.getText().toString() );
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.putExtra("ip", name);
intent.putExtra("port", number);
startActivity(intent);
}
});
}
}
Activity_main.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:id="#+id/activity_main"
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"
tools:context="com.example.jobush50.irobot.MainActivity"
android:background="#android:color/darker_gray">
<EditText
android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Input" >
</EditText>
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Send"
android:layout_below="#+id/EditText01"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="19dp">
</Button>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/myButton"
android:layout_marginLeft="34dp"
android:layout_marginStart="34dp"
android:layout_marginBottom="223dp"
android:id="#+id/textView1" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/textView1"
android:layout_alignStart="#+id/textView1"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="98dp"
android:id="#+id/textView2" />
activity_second.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:id="#+id/activity_second"
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"
tools:context="com.example.jobush50.irobot.SecondActivity"
android:layout_width="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Enter ip address"
android:ems="10"
android:layout_marginBottom="85dp"
android:id="#+id/editText_ip"
android:layout_above="#+id/button_send"
android:layout_centerHorizontal="true" />
<Button
android:text="Connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="144dp"
android:id="#+id/button_send"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Enter port number"
android:ems="10"
android:layout_above="#+id/button_send"
android:layout_marginBottom="17dp"
android:id="#+id/editText_port" />
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jobush50.irobot, PID: 2278
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jobush50.irobot/com.example.jobush50.irobot.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x22b8
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x22b8
at android.content.res.Resources.getText(Resources.java:331)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:4550)
at com.example.jobush50.irobot.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Application terminated.
textView2.setText(number);
The OS is trying to find match id since number is int try
textView2.setText(String.valueOf(number));

Unable to start activity, virtual method? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
Here's MediaPlayer1.class
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MediaPlayer1 extends AppCompatActivity {
MediaPlayer mpAudio;
Boolean play;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_player);
play = false;
mpAudio = MediaPlayer.create(this, R.raw.youngagain);
mpAudio.setLooping(true);
Button bPlay = (Button) findViewById(R.id.playBtn);
bPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (play == false)
mpAudio.start();
else
mpAudio.pause();
play = !play;
}
});
}
}
and the layout file activity_media_player.xml
<FrameLayout 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:id="#+id/base_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background_material_dark"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/zmountain"
android:scaleType="centerCrop"
android:foreground="#e1000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hardwell on Air"
android:textColor="#fafafa"
android:textSize="24sp"
android:textStyle="bold"
android:layout_marginTop="60dp"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hardwell on Air"
android:textColor="#fafafa"
android:layout_marginBottom="20dp"
android:id="#+id/textView" />
<sas.unplug.SquareImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/zghost" />
<com.wnafee.vector.MorphButton
android:id="#+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="#null"
app:vc_endDrawable="#drawable/ic_pause_to_play"
app:vc_foregroundTint="#color/colorAccent"
app:vc_startDrawable="#drawable/ic_play_to_pause" />
</LinearLayout>
</FrameLayout>
I am getting the following error on my activity above which I've called through my adapter, like this:
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final PodcastData item = items.get(position);
holder.ptitle.setText(item.getPTitle());
holder.plikes.setText(""+item.getPLikes());
//Converting image into a bitmap
Bitmap bitmap = item.getAlbum();
//usig the generate method
Palette palette = Palette.generate(bitmap, 16);
Palette.generateAsync(bitmap, 16, new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
Palette.Swatch vibrant = palette.getVibrantSwatch();
if (vibrant != null) {
// If we have a vibrant color
// update the title TextView
holder.ptitle.setTextColor(vibrant.getTitleTextColor());
holder.ptitle.setBackgroundColor(vibrant.getRgb());
}
}
});
holder.album.setImageBitmap(item.getAlbum());
holder.bLike.setOnClickListener(new View.OnClickListener() {
int likes = item.getPLikes();
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Likes selected " + items.get(position).getPLikes(), Toast.LENGTH_SHORT).show();
if(item.liked) {
holder.bLike.setColorFilter(Color.argb(255, 255, 255, 255));
likes-=1;
}
else {
holder.bLike.setColorFilter(Color.argb(255, 255, 38, 38));
likes+=1;
}
item.liked = !item.liked;
items.get(position).setPlikes(likes);
item.setPlikes(likes);
holder.plikes.setText("" + item.getPLikes());
ParseQuery<ParseObject> query = ParseQuery.getQuery("Podcast");
query.whereEqualTo("name", item.getPTitle());
query.getInBackground(item.objectId, new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if (e == null) {
object.put("likes", likes);
object.saveInBackground();
}
}
});
}
});
holder.album.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, MediaPlayer1.class); // Start MediaPlayer1 activity here
context.startActivity(intent);
}
});
}
activity is started in the last function - holder.album.setOnClickListener()
FATAL EXCEPTION: main
Process: sas.unplug, PID: 1715
java.lang.RuntimeException: Unable to start activity ComponentInfo{sas.unplug/sas.unplug.MediaPlayer1}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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 'void android.media.MediaPlayer.setLooping(boolean)' on a null object reference
at sas.unplug.MediaPlayer1.onCreate(MediaPlayer1.java:21)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
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 am running the same exact code individually as a seperate project and the player works fine there but as soon as I start this activity in my main project my app crashes. Please help me I am in need of help desperately as I have my minor project submission tomorrow.
mpAudio = MediaPlayer.create(this, R.raw.youngagain)
MediaPlayer.create() return a MediaPlayer object, or null if creation failed.
MediaPlayer.create() failed and mpAudio = null .
So when you mpAudio.setLoop, you get nullpointer error.

Categories

Resources