I am developing an app for android that has to access another class, but i don't know why it doesn't work.
When run the App in android 2.3.3 it force closes, and I don't understand why. I think that the method is correct.
Log in the force close the phone android:
> app_vercode:1
device_model:u8800
build_version:111180
condition:1
processName:beta.tester
pid:13277
uid:10088
tag:null
shortMsg:java.lang.NullPointerException
longMsg:java.lang.NullPointerException: Unable to start activity ComponentInfo{beta.tester/beta.tester.BetaTesterActivity}: java.lang.NullPointerException
stackTrace:java.lang.RuntimeException: Unable to start activity ComponentInfo{beta.tester/beta.tester.BetaTesterActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1664)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3703)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at beta.tester.BetaTesterActivity.onCreate(BetaTesterActivity.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1628)
... 11 more
Detail logs:
EDIT: This code already is correctly.
The code:
class BetaTesterActivity:
package beta.tester;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class BetaTesterActivity extends Activity {
public TextView text1;
private teste cmd;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text1 = (TextView) findViewById(R.id.text1);
//Start the function
cmd = new teste();
cmd.start(this);
}
}
class teste:
package beta.tester;
public class teste {
//Function that I will start
public void start(BetaTesterActivity zav){
zav.text1.setText("Hello");
}
//
}
In class teste, you are creating a new BetaTesterActivity, which is useless. You need to use the instance created by the framework. Change your class teste to this:
public class teste {
//Function that I will start
public void start(BetaTesterActivity zav){
zav.text1.setText("Hello");
}
}
Then in the onCreate method of your activity class, you need to initialize cmd and then call start like this:
cmd.start(this);
Related
Whenever I run my application on my phone, it works fine and opens Correctly but as soon as I clicked the TextView for typing some text in that, The Application gets crashed. I got a similar post like this on the forum but It couldn't help me out. Don't know at all What actually happened. New in Android Platform. (please, Ask me if need something regarding Question)
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public void ButtonClicked(View view){
EditText EnteredAmount = (EditText) findViewById(R.id.EnteredAmount);
Log.i("Amount Entered", EnteredAmount.getText().toString());
}
LogCat Error Log
--------- beginning of crash
09-08 22:29:11.790 25239-25239/com.example.admin.convertcurrency E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.convertcurrency, PID: 25239
java.lang.IllegalStateException: Could not find method clickTextView(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatEditText with id 'EnteredAmount'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:380)
at android.view.View.performClick(View.java:5215)
at android.view.View$PerformClick.run(View.java:21196)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
--------- beginning of system
09-08 22:29:11.858 25239-25239/com.example.admin.convertcurrency I/Process: Sending signal. PID: 25239 SIG: 9
java.lang.IllegalStateException: Could not find method clickTextView(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatEditText with id 'EnteredAmount'
In your layout, you appear to have android:onClick="clickTextView". In your activity, you do not have a method named clickTextView(). You need to implement that method in your activity:
public void clickTextView(View v) {
// whatever you want to do
}
Alternatively, you could remove the android:onClick="clickTextView" attribute from your <EditText> in your layout resource.
Perhaps you can use the multiple clickListener for views:
EditText EnteredAmount = (EditText) findViewById(R.id.EnteredAmount);
EnteredAmount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(this, "I am clicked", Toast.LENGTH_SHORT).show;
}
});
I am quite new to android development and I am facing a NullPointerException when I am trying to call an AsyncTask which is an inner class in another class.
I believe it is the way I am instantiating it but this is what I have:
// This onClick is in my adapter class - which is a separate Java File and class
#Override
public void onClick(View v) {
UploadRes uploadRes = new UploadRes();
UploadRes.UploadResConfirm uploadResConfirm = uploadRes.new UploadResConfirm(v.getContext());
uploadResConfirm.execute(fileName, filePath);
}
public class UploadResConfirm extends AsyncTask<String, String, String> {
Dialog dialog;
Context context;
public UploadResConfirm(Context context){
this.context = context;
}
#Override
protected void onPreExecute(){
dialog = new Dialog(UploadRes.this);
dialog.setTitle("Currently uploading");
dialog.show();
}
I believe it has got something to do with the dialog box itself being instantiated in the AsyncTask class.
The error stack trace on Logcat - its the Dialog, I think its in the wrong place...
java.lang.NullPointerException
at codeman.androapp.UploadRes$UploadResConfirm.onPreExecute(UploadRes.java:246)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Some help if any would be much obliged.
Instead of dialog = new Dialog(UploadRes.this);
Try
dialog = new Dialog(context);
your are passing a wrong context to create dialog.
I'm new at android programming, I want to fetch Parse in ListView arrangement and i found the perfect guide, but i cant really implement it on my code.
I have browse a lot for 2 days and i give up.
Here's the error
E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.osbert.buttons, PID: 2732
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.RuntimeException: You must call Parse.initialize(context, oauthKey, oauthSecret) before using the Parse library.
at com.parse.ParseUser.checkApplicationContext(ParseUser.java:1426)
at com.parse.ParseUser.getCurrentUserAsync(ParseUser.java:1048)
at com.parse.ParseUser.access$900(ParseUser.java:27)
at com.parse.ParseUser$9.then(ParseUser.java:1041)
at com.parse.ParseUser$9.then(ParseUser.java:1038)
at com.parse.TaskQueue.enqueue(TaskQueue.java:61)
at com.parse.ParseUser.getCurrentUserAsync(ParseUser.java:1038)
at com.parse.ParseUser.getCurrentUserAsync(ParseUser.java:1001)
at com.parse.ParseQuery.getUserAsync(ParseQuery.java:352)
at com.parse.ParseQuery.access$1600(ParseQuery.java:78)
at com.parse.ParseQuery$15.call(ParseQuery.java:1000)
at com.parse.ParseQuery$15.call(ParseQuery.java:997)
at com.parse.ParseQuery.doWithRunningCheck(ParseQuery.java:936)
at com.parse.ParseQuery.findInBackground(ParseQuery.java:997)
at com.parse.ParseQuery.find(ParseQuery.java:588)
at com.example.osbert.buttons.RestaurantList$RemoteDataTask.doInBackground(RestaurantList.java:60)
at com.example.osbert.buttons.RestaurantList$RemoteDataTask.doInBackground(RestaurantList.java:34)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/WindowManager﹕ android.view.WindowLeaked: Activity com.example.osbert.buttons.RestaurantList has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{27bb5583 V.E..... R......D 0,0-729,322} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:298)
at com.example.osbert.buttons.RestaurantList$RemoteDataTask.onPreExecute(RestaurantList.java:46)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at com.example.osbert.buttons.RestaurantList.onCreate(RestaurantList.java:30)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
And here's my code
package com.example.osbert.buttons;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class RestaurantList extends Activity {
// Declare Variables
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ListViewAdapter adapter;
private List<WorldPopulation> worldpopulationlist = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
// Execute RemoteDataTask AsyncTask
new RemoteDataTask().execute();
}
// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(RestaurantList.this);
// Set progressdialog title
mProgressDialog.setTitle("Loading");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// Create the array
worldpopulationlist = new ArrayList<WorldPopulation>();
try {
// Locate the class table named "Country" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"Country");
// Locate the column named "ranknum" in Parse.com and order list
// by ascending
query.orderByAscending("ranknum");
ob = query.find();
for (ParseObject country : ob) {
// Locate images in flag column
ParseFile image = (ParseFile) country.get("flag");
WorldPopulation map = new WorldPopulation();
map.setRank((String) country.get("rank"));
map.setCountry((String) country.get("country"));
map.setPopulation((String) country.get("population"));
map.setFlag(image.getUrl());
worldpopulationlist.add(map);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(RestaurantList.this,
worldpopulationlist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
finish();
}
}
}
Pls help :((
Thanks in advance
Have you even tried resolving this?
Caused by: java.lang.RuntimeException: You must call Parse.initialize(context, oauthKey, oauthSecret) before using the Parse library?
Its the cause of the crash.
Also why are you calling finish() i onPostExecute?! It will close your activity...and i suppose you would like to see the results in your list view?
U error Logs says the solution.....
Caused by: java.lang.RuntimeException: You must call Parse.initialize(context, oauthKey, oauthSecret) before using the Parse library.
at com.parse.ParseUser.checkApplicationContext(ParseUser.java:1426)
I have a simple Activity with a button on it. Upon the button's onClick method, I want to run some code in another thread, getting a HandlerThread's Looper object and then posting a Runnable to the Handler associated with it.
My objective is to call a thread's method from another thread (and run it from its own thread) using Handlers (probably a bad idea, but I wanted to learn this instead of abusing static methods)
So I decided to extend HandlerThread. The new class does not really do much.
public class EpicThread extends HandlerThread {
public EpicThread(String name) {
super(name);
}
}
The activity does not really do much either. I did this for some testing.
public class MainActivity extends Activity {
EpicThread et;
Looper l;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EpicThread et = new EpicThread("The epic thread");
et.start();
}
public void click(View v) {
l = et.getLooper();
Handler h = new Handler(l);
h.post(new Runnable() {
#Override
public void run() {
System.out.println("This is running on the epic thread");
}
});
}
}
So I test this by pressing the button, which gives me a InvocationTargetException when getLooper() is called:
E/AndroidRuntime(23262): FATAL EXCEPTION: main
E/AndroidRuntime(23262): java.lang.IllegalStateException: Could not execute method of the activity
E/AndroidRuntime(23262): at android.view.View$1.onClick(View.java:3660)
E/AndroidRuntime(23262): at android.view.View.performClick(View.java:4162)
E/AndroidRuntime(23262): at android.view.View$PerformClick.run(View.java:17088)
E/AndroidRuntime(23262): at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(23262): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(23262): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(23262): at android.app.ActivityThread.main(ActivityThread.java:4867)
E/AndroidRuntime(23262): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(23262): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(23262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
E/AndroidRuntime(23262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
E/AndroidRuntime(23262): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(23262): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime(23262): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(23262): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(23262): at android.view.View$1.onClick(View.java:3655)
E/AndroidRuntime(23262): ... 11 more
E/AndroidRuntime(23262): Caused by: java.lang.NullPointerException
E/AndroidRuntime(23262): at com.asdf.qwert.MainActivity.click(MainActivity.java:49)
E/AndroidRuntime(23262): ... 14 more
Fiddling a little with this code, I noticed that :
Getting the Looper with l = et.getLooper(); works right after creating the thread, after et.start();.
If I use HandlerThread directly instead of EpicThread, it works regardless of when getLooper() is called.
So I have no idea why this exception is thrown, and why simply by not using a subclass things started to work.
I am having trouble with developing my Android application, I am currently using Eclipse and a Phidget RFID. My aim is to display the ID of the RFID in a piece of text on the Android device.
I have managed to display the ID through a println in the following pieces of code.
package myFood.myFood;
import com.phidgets.*;
import com.phidgets.event.*;
public class RFID {
static String x ="NULL";
public static final Object main(String args[]) throws Exception {
RFIDPhidget rfid;
rfid = new RFIDPhidget();
rfid.openAny();
//Begin the TagGained event, allowing for users to read the RFID values
rfid.addTagGainListener(new TagGainListener()
{
public void tagGained(TagGainEvent oe)
{
Object y = (oe.getValue());
x= y.toString();
}
});
long StartTime,RunTime;
StartTime=System.currentTimeMillis();
do{
RunTime=System.currentTimeMillis();
if (x.equals("NULL")) {
//Continue waiting for input
}
else
StartTime = 10000; //Overload the result so the loop ends
}
while (RunTime-StartTime<5000);
rfid.close();
return x;
}
}
and then.
package myFood.myFood;
public class RFIDresult {
public static final Object main(String args[]) throws Exception {
System.out.println("Results");
Object x = RFID.main(args);
System.out.println(x);
return x;
}
}
However I want the ID to be displayed in a piece of text so I tried to develop the second piece of code into Android.
package myFood.myFood;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AddFood extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addfood);
Button mybutton = (Button) findViewById(R.id.Button1);
mybutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String[] args = null;
Object x = null;
try {
x = RFID.main(args);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TextView mytext=(TextView)findViewById(R.id.widget96);
mytext.setText((CharSequence) x);
}
});
}
}
And this just generates a force close. I am a bit of a novice at this and will appreciate all the advice I get.
LogCat Report;
ERROR/AndroidRuntime(519): FATAL EXCEPTION: main
ERROR/AndroidRuntime(519): java.lang.ExceptionInInitializerError
ERROR/AndroidRuntime(519): at myFood.myFood.RFID.main(RFID.java:9)
ERROR/AndroidRuntime(519): at myFood.myFood.AddFood$1.onClick(AddFood.java:26)<br/>
ERROR/AndroidRuntime(519): at android.view.View.performClick(View.java:2408)
ERROR/AndroidRuntime(519): at android.view.View$PerformClick.run(View.java:8816)
ERROR/AndroidRuntime(519): at android.os.Handler.handleCallback(Handler.java:587)
ERROR/AndroidRuntime(519): at android.os.Handler.dispatchMessage(Handler.java:92)
ERROR/AndroidRuntime(519): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(519): at android.app.ActivityThread.main(ActivityThread.java:4627)
ERROR/AndroidRuntime(519): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(519): at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(519): at om.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/AndroidRuntime(519): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/AndroidRuntime(519): at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(519): Caused by: java.lang.ExceptionInInitializerError: Library phidget21 not found
ERROR/AndroidRuntime(519): Could not locate the Phidget C library (libphidget21.so).
ERROR/AndroidRuntime(519): Make sure it is installed, and add it's path to LD_LIBRARY_PATH.
ERROR/AndroidRuntime(519): at com.phidgets.Phidget.<clinit>(Phidget.java:34)
ERROR/AndroidRuntime(519): ... 13 more
You need to get myText from 'view' parameter. Try this:
TextView mytext=(TextView)**view**.findViewById(R.id.widget96);