Confusing title, I know, but not quite shure how to explain.
Will do my best here.
What I am trying to accomplish is that when user clicks a button, the text from one field is pushed to firebase, then the text from the next field is pushed below that.
Something like this:
-"UID"--"Scripttitle"--"ScriptURL"
Here is the code that I am trying to do this with:
final EditText title = (EditText) rlLayout.findViewById(R.id.inputScriptTitle);
final EditText scripturl = (EditText) rlLayout.findViewById(R.id.inputScriptURL);
final Button button = (Button) rlLayout.findViewById(R.id.submitScriptButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new Firebase("https://theatre-assistant.firebaseio.com/scripts")
.push()
.child("scripttitle")
.setValue(title.getText().toString());
new Firebase("https://theatre-assistant.firebaseio.com/scripts")
.push()
.child(title.getText().toString())
.setValue(scripturl);
I am shure I am just doing some really basic error, but as I am new to programming, I cant really see what that might be.
The error from logcat is:
--------- beginning of crash
04-28 12:26:15.860 4979-4979/com.langaard.theatreassistant E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.langaard.theatreassistant, PID: 4979
Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
com.firebase.client.FirebaseException: Failed to parse to snapshot
at com.firebase.client.Firebase.setValueInternal(Firebase.java:357)
at com.firebase.client.Firebase.setValue(Firebase.java:248)
at com.langaard.theatreassistant.ManageScriptsFragment$1.onClick(ManageScriptsFragment.java:57)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
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.IllegalArgumentException: Conflicting getter definitions for property "importantForAccessibility": android.view.View#isImportantForAccessibility(0 params) vs android.view.View#getImportantForAccessibility(0 params)
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2615)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542)
at com.firebase.client.Firebase.setValueInternal(Firebase.java:346)
at com.firebase.client.Firebase.setValue(Firebase.java:248)
at com.langaard.theatreassistant.ManageScriptsFragment$1.onClick(ManageScriptsFragment.java:57)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
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: com.fasterxml.jackson.databind.JsonMappingException: Conflicting getter definitions for property "importantForAccessibility": android.view.View#isImportantForAccessibility(0 params) vs android.view.View#getImportantForAccessibility(0 params)
at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:838)
at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:387)
at com.fasterxml.jackson.databind.SerializerProvider.findTypedValueSerializer(SerializerProvider.java:478)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:97)
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2593)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542)
at com.firebase.client.Firebase.setValueInternal(Firebase.java:346)
at com.firebase.client.Firebase.setValue(Firebase.java:248)
at com.langaard.theatreassistant.ManageScriptsFragment$1.onClick(ManageScriptsFragment.java:57)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
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.IllegalArgumentException: Conflicting getter definitions for property "importantForAccessibility": android.view.View#isImportantForAccessibility(0 params) vs android.view.View#getImportantForAccessibility(0 params)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getGetter(POJOPropertyBuilder.java:190)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getAccessor(POJOPropertyBuilder.java:283)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.removeIgnorableTypes(BeanSerializerFactory.java:678)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.findBeanProperties(BeanSerializerFactory.java:557)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.constructBeanSerializer(BeanSerializerFactory.java:373)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.findBeanSerializer(BeanSerializerFactory.java:268)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory._createSerializer2(BeanSerializerFactory.java:213)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:152)
at com.fasterxml.jackson.databind.SerializerProvider._createUntypedSerializer(SerializerProvider.java:873)
at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:833)
at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:387)
at com.fasterxml.jackson.databind.SerializerProvider.findTypedValueSerializer(SerializerProvider.java:478)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:97)
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2593)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542)
at com.firebase.client.Firebase.setValueInternal(Firebase.java:346)
at com.firebase.client.Firebase.setValue(Firebase.java:248)
at com.langaard.theatreassistant.ManageScriptsFragment$1.onClick(ManageScriptsFragment.java:57)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21158)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
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)
You can't set view to setValue. scripturl is a EditText.
new Firebase("https://theatre-assistant.firebaseio.com/scripts")
.push()
.child(title.getText().toString())
.setValue(scripturl);
if you use like that you don't see any error.
new Firebase("https://theatre-assistant.firebaseio.com/scripts")
.child(title.getText().toString())
.push()
.setValue(scripturl.getText().toString);
Replace your following code
new Firebase("https://theatre-assistant.firebaseio.com/scripts")
.push()
.child(title.getText().toString())
.setValue(scripturl);
with
new Firebase("https://theatre-assistant.firebaseio.com/scripts")
.push()
.child(title.getText().toString())
.setValue(scripturl.getText().toString());
//because you pass directly object in value method replace and pass data from edit text not object
Related
public void Clicking(View view) {
double one = Double.parseDouble(testtt.getText().toString());
double two = Double.parseDouble(tessss.getText().toString());
}
already;
testtt [TextView] = "0.0001974794"
tessss [TextView] = "0"
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.Testing_Appi, PID: 2988
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
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:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
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:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NumberFormatException: For input string: "0.0001974794"
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)
at java.lang.Double.parseDouble(Double.java:547)
at com.example.Testing_Appi.MainActivity.testor(MainActivity.java:153)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
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:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
I couldn't spot where the error was!
Example : Find your view by id with view
EditText testttET = view.findViewById(R.id.testtt);
String text = testttET.getText().toString();
check if your text is not empty
if(!text.equals("")){
double value = Double.parseDouble(text);
}
For a few days I've been trying to figure out this multithreading thing on Android Studio. I've only used SwingWorker in the past and after trying several ways to get my https connection to initiate off of the main thread I'm pulling my hair out. I've tried everything from using ASyncTask to just starting a thread everytime the connection gets made, but nothing seems to work. My app crashes every time. The most recent attempt is pictured below.
Edit: It's not a duplicate. I've read that thread and it doesn't do anything to help solve this problem. I'm attempting to execute this task outside of the main thread, but I don't know why my code isn't doing that already since I change to the background thread in run(); and even start a new thread just in case.
public class YelpActivity extends AppCompatActivity implements Runnable{
private yelpLoader load;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yelp);
}
public void run(){
Thread t = new Thread();
t.start();
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
YelpAPI test = new YelpAPI();
test.run();
}
public void yelpClick(View v){
run();
changeYelpResults(v,"test");
}
private void changeYelpResults(View v, String text){
TextView t = (TextView) findViewById(R.id.ResturauntID);
t.setText(text);
}
}
Error Log:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: tastr.tastr, PID: 3560
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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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.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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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: org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.
at org.scribe.model.Request.send(Request.java:70)
at org.scribe.model.Request.send(Request.java:76)
at tastr.tastr.YelpAPI.sendRequestAndGetResponse(YelpAPI.java:132)
at tastr.tastr.YelpAPI.searchForBusinessesByLocation(YelpAPI.java:89)
at tastr.tastr.YelpAPI.queryAPI(YelpAPI.java:146)
at tastr.tastr.YelpAPI.run(YelpAPI.java:190)
at tastr.tastr.YelpActivity.run(YelpActivity.java:26)
at tastr.tastr.YelpActivity.yelpClick(YelpActivity.java:31)
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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
at org.scribe.model.Response.<init>(Response.java:29)
at org.scribe.model.Request.doSend(Request.java:117)
at org.scribe.model.Request.send(Request.java:66)
at org.scribe.model.Request.send(Request.java:76)
at tastr.tastr.YelpAPI.sendRequestAndGetResponse(YelpAPI.java:132)
at tastr.tastr.YelpAPI.searchForBusinessesByLocation(YelpAPI.java:89)
at tastr.tastr.YelpAPI.queryAPI(YelpAPI.java:146)
at tastr.tastr.YelpAPI.run(YelpAPI.java:190)
at tastr.tastr.YelpActivity.run(YelpActivity.java:26)
at tastr.tastr.YelpActivity.yelpClick(YelpActivity.java:31)
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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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)
public class YelpActivity extends AppCompatActivity{
private yelpLoader load;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yelp);
}
public void run(){
new Thread(new Runnable() {
#Override
public void run() {
YelpAPI test = new YelpAPI();
test.run();
}
}).start();
}
public void yelpClick(View v){
run();
changeYelpResults(v,"test");
}
private void changeYelpResults(View v, String text){
TextView t = (TextView) findViewById(R.id.ResturauntID);
t.setText(text);
}
}
Note: It's not a good practice to implement Runnable in Activity
class. Use Core Java threads like I did in the run method or Use other
Android Background mechanism like AsyncTask/IntentService.
I created the code below to get the contents of multiple EditText:
Integer data[] = new Integer[4];
ListView listView = (ListView) findViewById(R.id.listView);
for (i=0;i < listView.getCount();i++){
LinearLayout layout = (LinearLayout) listView.getChildAt(i);
EditText et = (EditText) layout.findViewById(R.id.record_list_edit);
data[i] = Integer.parseInt(et.getText().toString());
}
I can successfully use the following code to to get just the first number (or whichever one I want):
LinearLayout layout = (LinearLayout) listView.getChildAt(0);
EditText et = (EditText) layout.findViewById(R.id.record_list_edit);
data[0] = Integer.parseInt(et.getText().toString());
But when I use the for loop to get all, it breaks.
This is the error log:
08-28 22:12:33.931 7814-7814/edu.nankai.soar.demof E/AndroidRuntime: FATAL EXCEPTION: main
Process: edu.nankai.soar.demof, PID: 7814
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:4851)
at android.view.View$PerformClick.run(View.java:20016)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5506)
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:952)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4851)
at android.view.View$PerformClick.run(View.java:20016)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5506)
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:952)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at edu.nankai.soar.demof.SensorsActivity.log(SensorsActivity.java:81)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4851)
at android.view.View$PerformClick.run(View.java:20016)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5506)
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:952)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
Really thank you, Shaishav
Integer data[] = new Integer[listView.getCount()];
I know why I was wrong. I I counted the wrong number... I was so stupid...
The answer comes form Shaishav.
I want to connect my application with an SQLite database. Whenever someone clicks on the create button, the database should be generated immediately. I'm using the onClick function, but I'm getting an error when the function is called. This is the code that is executed when the onClick function is called:
public void createDb(View view)
{
DataHandler db = new DataHandler(this);
db.addValue();
Toast.makeText(getApplicationContext(),"DataBase Created Successfully.", Toast.LENGTH_SHORT).show();
}
The function addValue is defined in the DataHandler class with the following code:
void addValue()
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values=new ContentValues();
Random rand=new Random();
//for (int i=0;i<15;i++)
{
values.put(KEY_VALUE,rand.nextInt(2));
db.insert(TABLE_DATA,null,values);
}
db.close();
return ;
}
I'm getting this error:
06-18 09:19:33.429 3200-3200/com.example.mobileworkstation.database E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mobileworkstation.database, PID: 3200
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:289)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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: android.database.sqlite.SQLiteException: near "TABLEDATA": syntax error (code 1): , while compiling: CREATE TABLEDATA(VALUEINTEGER )
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.example.mobileworkstation.database.DataHandler.onCreate(DataHandler.java:28)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.mobileworkstation.database.DataHandler.addValue(DataHandler.java:38)
at com.example.mobileworkstation.database.MainActivity.createDb(MainActivity.java:71)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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 have read through many tutorials but I still cannot seem to get it to work. Any suggestions on how to solve this?
I have Firebase fully installed into Android Studio, and I've tested it with a couple 1 activity applications, but on the only application with more than one activity, it's not working.
When I try to do the following in my 'Sign up' code:
private void makeStudent(String email, String password) {
Student s = new Student(email, password);
Firebase ref = new Firebase("MYAPP.firebaseio.com/");
//took out my app name
Firebase studentlocation = ref.child("Student");
studentlocation.setValue(s); //this is the line on which I get the error
//also, when I go on my firebase site, it hasn't created the Student
//directory
}
The Firebase page said:
If your Android app has multiple activities, add this code to your Application Class. Otherwise add it in your main activity.
So, I looked around, and this is what I created:
I made a MyApplication.java file, which contained:
import com.firebase.client.Firebase;
public class MyApplication extends android.app.Application {
#Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
}
}
Then I placed the following into AndroidManifest.xml:
android:name=".MyApplication"
Yet, I still receive an error on the exact same line.
How do I fix this Firebase issue? I appreciate any help I can get, please!
ERROR RECEIVED (scroll to the right in the code box):
FATAL EXCEPTION: main
Process: professionalgrader.professionalgrader, PID: 3827
com.firebase.client.FirebaseException: Failed to parse to snapshot
at com.firebase.client.Firebase.setValueInternal(Firebase.java:347)
at com.firebase.client.Firebase.setValue(Firebase.java:238)
at professionalgrader.professionalgrader.SignUp.makeStudent(SignUp.java:94)
at professionalgrader.professionalgrader.SignUp.access$500(SignUp.java:16)
at professionalgrader.professionalgrader.SignUp$1.onClick(SignUp.java:45)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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.IllegalArgumentException: length=0; index=0 (through reference chain: professionalgrader.professionalgrader.Student["listofTermClasses"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2615)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542)
at com.firebase.client.Firebase.setValueInternal(Firebase.java:336)
at com.firebase.client.Firebase.setValue(Firebase.java:238)
at professionalgrader.professionalgrader.SignUp.makeStudent(SignUp.java:94)
at professionalgrader.professionalgrader.SignUp.access$500(SignUp.java:16)
at professionalgrader.professionalgrader.SignUp$1.onClick(SignUp.java:45)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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: com.fasterxml.jackson.databind.JsonMappingException: length=0; index=0 (through reference chain: professionalgrader.professionalgrader.Student["listofTermClasses"])
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:184)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:605)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2593)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542)
at com.firebase.client.Firebase.setValueInternal(Firebase.java:336)
at com.firebase.client.Firebase.setValue(Firebase.java:238)
at professionalgrader.professionalgrader.SignUp.makeStudent(SignUp.java:94)
at professionalgrader.professionalgrader.SignUp.access$500(SignUp.java:16)
at professionalgrader.professionalgrader.SignUp$1.onClick(SignUp.java:45)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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.ArrayIndexOutOfBoundsException: length=0; index=0
at professionalgrader.professionalgrader.Student.getListofTermClasses(Student.java:252)
at java.lang.reflect.Method.invoke(Native Method)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:679)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:534)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:597)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2593)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542)
at com.firebase.client.Firebase.setValueInternal(Firebase.java:336)
at com.firebase.client.Firebase.setValue(Firebase.java:238)
at professionalgrader.professionalgrader.SignUp.makeStudent(SignUp.java:94)
at professionalgrader.professionalgrader.SignUp.access$500(SignUp.java:16)
at professionalgrader.professionalgrader.SignUp$1.onClick(SignUp.java:45)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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)
THE SECOND ERROR:
06-15 04:13:34.902 3089-3089/professionalgrader.professionalgrader E/AndroidRuntime: FATAL EXCEPTION: main
Process: professionalgrader.professionalgrader, PID: 3089
com.firebase.client.FirebaseException: Failed to parse to snapshot
at com.firebase.client.Firebase.setValueInternal(Firebase.java:347)
at com.firebase.client.Firebase.setValue(Firebase.java:238)
at professionalgrader.professionalgrader.SignUp.makeStudent(SignUp.java:94)
at professionalgrader.professionalgrader.SignUp.access$500(SignUp.java:16)
at professionalgrader.professionalgrader.SignUp$1.onClick(SignUp.java:45)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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.IllegalArgumentException: Attempt to invoke virtual method 'double professionalgrader.professionalgrader.Class.getGPA()' on a null object reference (through reference chain: professionalgrader.professionalgrader.Student["totalGPA"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2615)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542)
at com.firebase.client.Firebase.setValueInternal(Firebase.java:336)
at com.firebase.client.Firebase.setValue(Firebase.java:238)
at professionalgrader.professionalgrader.SignUp.makeStudent(SignUp.java:94)
at professionalgrader.professionalgrader.SignUp.access$500(SignUp.java:16)
at professionalgrader.professionalgrader.SignUp$1.onClick(SignUp.java:45)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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: com.fasterxml.jackson.databind.JsonMappingException: Attempt to invoke virtual method 'double professionalgrader.professionalgrader.Class.getGPA()' on a null object reference (through reference chain: professionalgrader.professionalgrader.Student["totalGPA"])
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:184)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:605)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2593)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542)
at com.firebase.client.Firebase.setValueInternal(Firebase.java:336)
at com.firebase.client.Firebase.setValue(Firebase.java:238)
at professionalgrader.professionalgrader.SignUp.makeStudent(SignUp.java:94)
at professionalgrader.professionalgrader.SignUp.access$500(SignUp.java:16)
at professionalgrader.professionalgrader.SignUp$1.onClick(SignUp.java:45)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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 'double professionalgrader.professionalgrader.Class.getGPA()' on a null object reference
at professionalgrader.professionalgrader.Student.getTotalGPA(Student.java:199)
at java.lang.reflect.Method.invoke(Native Method)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:679)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:534)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:597)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2593)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542)
at com.firebase.client.Firebase.setValueInternal(Firebase.java:336)
at com.firebase.client.Firebase.setValue(Firebase.java:238)
at professionalgrader.professionalgrader.SignUp.makeStudent(SignUp.java:94)
at professionalgrader.professionalgrader.SignUp.access$500(SignUp.java:16)
at professionalgrader.professionalgrader.SignUp$1.onClick(SignUp.java:45)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
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)
Possible source of error because it told me it couldn't obtain the Snapshot at the line in makeStudent(), but makeStudent() doesn't use Snapshot. Only userExists() does. Note: The variable 'exists' is defined as a static boolean in the SignUp class.
private boolean userExists() {
Firebase ref = new Firebase("MYAPPNAME.firebaseio.com");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapShot: snapshot.getChildren()) {
Student s = postSnapShot.getValue(Student.class);
if(s.getEmail().equals(mEmailView.getText().toString())) {
exists = true;
}
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
exists = true;
}
});
return exists;
}
SOLUTION TO MY ISSUE:
I realized that Snapshot was never used in makeStudent(), and that was where the error started. Thus, I looked in the only place it was used... userExists(), and it turned out that I was correct! The error was produced by trying to identify Student s as a nonexistent student! Thus, I actually needed a try like such:
private boolean userExists() {
Firebase ref = new Firebase("MYAPPNAME.firebaseio.com");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
Student s;
for (DataSnapshot postSnapShot: snapshot.getChildren()) {
try {
s = postSnapShot.getValue(Student.class);
if(s.getEmail().equals(mEmailView.getText().toString())) {
System.out.println(snapshot.getValue());
exists = true;
}
} catch(Exception e) {
exists = false;
}
}
}
#Override
public void onCancelled(FirebaseError firebaseError) {
exists = true;
}
});
return exists;
}