Fatal Error On main Thread - java

I am receiving this fatal Error, when I try to access one particular function zipIt().
It doesn't matter from where I try to access it I always receive this error. This function simply zips a folder but program don't even go inside this function.
Logcat is displayed below:
Process: com.test.shahid, PID: 14839
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5050)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1264)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1080)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5050)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1264)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1080)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at java.io.File.fixSlashes(File.java:185)
at java.io.File.<init>(File.java:134)
at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
at com.test.shahid.MainActivity.zipIt(MainActivity.java:170)
at com.test.shahid.MainActivity.dispatchTakePictureIntent(MainActivity.java:490)
at com.test.shahid.MainActivity.onClickPhoto(MainActivity.java:468)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5050)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1264)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1080)
            at dalvik.system.NativeStart.main(Native Method)
05-23 09:48:22.023 14839-14839/com.test.shahid I/Process﹕ Sending signal. PID: 14839 SIG: 9
Here is the function and variables associated with this function.
private static final File INPUT_FOLDER = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
private static final String ZIPPED_FOLDER = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString();
protected void zipIt(File inputFolder, String zipFilePath) {
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFilePath);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
String myname =inputFolder.toString();
ZipEntry folderZipEntry = new ZipEntry(myname);
zipOutputStream.putNextEntry(folderZipEntry);
File[] contents = inputFolder.listFiles();
for (File f : contents) {
if (f.isFile())
zipFile(f, zipOutputStream);
}
zipOutputStream.closeEntry();
zipOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void zipFile(File inputFile, ZipOutputStream zipOutputStream) {
try { // A ZipEntry represents a file entry in the zip archive
// We name the ZipEntry after the original file's name
ZipEntry zipEntry = new ZipEntry(inputFile.getName());
zipOutputStream.putNextEntry(zipEntry);
FileInputStream fileInputStream = new FileInputStream(inputFile);
byte[] buf = new byte[1024];
int bytesRead;
// Read the input file by chucks of 1024 bytes
// and write the read bytes to the zip stream
while ((bytesRead = fileInputStream.read(buf)) > 0) {
zipOutputStream.write(buf, 0, bytesRead);
}
// close ZipEntry to store the stream to the file
zipOutputStream.closeEntry();
System.out.println("Regular file :" + inputFile.getCanonicalPath() + " is zipped to archive :" + ZIPPED_FOLDER);
} catch (IOException e) {
e.printStackTrace();
}
}
Well this is from where i am calling this function. I called this function just to check otherwise i have called this function from button and from several other functions. But nothing works.
public void onSync(View v) throws JSONException, IOException {
zipIt(INPUT_FOLDER, ZIPPED_FOLDER);
if (!isConnected()) {
AlertDialog.Builder mBuilder = new Builder(this);
mBuilder.setMessage("Please Enable Wifi to use this service");
mBuilder.setTitle("Enable WIFI");
mBuilder.setCancelable(false);
mBuilder.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(
Settings.ACTION_WIFI_SETTINGS);
startActivity(i);
}
}
);
mBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// This code might cause problem so check when
// device is available
MainActivity.this.finish();
}
}
);
// create alert dialog
AlertDialog alertDialog = mBuilder.create();
// show it
alertDialog.show();
tvIsConnected.setText("You are NOT conncted");
// Intent myIntent = new
// Intent(Settings.ACTION_WIFI_SETTINGS);
// startActivity(myIntent);
return;
}
tvIsConnected.setBackgroundColor(0xFF00CC00);
tvIsConnected.setText("You are conncted");
handler.sendEmptyMessage(MSG_SYNC);
}

NullPointerException
Do you have "WriteExternalStorage" permision in your manifest?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Did you check the file path?
Environment.getExternalStorageDirectory() + "/some_foulder/file.txt"

Related

Android app seems to be crashing on launch on KitKat 4.4

My app when installed on my relatively new Huawei running Oreo 8.0 is able to launch and run the app smoothly. The app also runs fine on the Nexus Emulator running Lollipop. However, on my other phone running KitKat 4.4 the app crashes when launched.
I think the problem occured when I added a default NavigationDrawerActivity.
09-05 14:05:57.285 30968-30968/? E/AndroidRuntime: FATAL EXCEPTION:
main
Process: com.example.a_phi.myapplication, PID: 30968
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a_phi.myapplication/com.example.a_phi.myapplication.LoginActivity}:
android.view.InflateException: Binary XML file line #2: Error
inflating class
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
at android.app.ActivityThread.access$1100(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1958)
at com.example.a_phi.myapplication.LoginActivity.onCreate(LoginActivity.java:44)
at android.app.Activity.performCreate(Activity.java:5340)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313) 
at android.app.ActivityThread.access$1100(ActivityThread.java:141) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
at android.app.Activity.setContentView(Activity.java:1958) 
at com.example.a_phi.myapplication.LoginActivity.onCreate(LoginActivity.java:44) 
at android.app.Activity.performCreate(Activity.java:5340) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313) 
at android.app.ActivityThread.access$1100(ActivityThread.java:141) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException
at com.huawei.android.content.res.ResourcesEx.getThemeDrawable(ResourcesEx.java:655)
at com.huawei.android.content.res.ResourcesEx.loadDrawable(ResourcesEx.java:623)
at android.content.res.TypedArray.getDrawable(TypedArray.java:616)
at android.view.View.(View.java:3573)
at android.view.ViewGroup.(ViewGroup.java:470)
at android.widget.LinearLayout.(LinearLayout.java:179)
at android.widget.LinearLayout.(LinearLayout.java:175)
at java.lang.reflect.Constructor.constructNative(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
at android.view.LayoutInflater.createView(LayoutInflater.java:594) 
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
at android.app.Activity.setContentView(Activity.java:1958) 
at com.example.a_phi.myapplication.LoginActivity.onCreate(LoginActivity.java:44) 
at android.app.Activity.performCreate(Activity.java:5340) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313) 
at android.app.ActivityThread.access$1100(ActivityThread.java:141) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711) 
at dalvik.system.NativeStart.main(Native Method)
LoginActvity 
public class LoginActivity extends Activity {
private UserLoginTask mAuthTask = null;
// UI references.
private EditText mEmailView;
private EditText mPasswordView;
private TextView mRegistrationScreen;
private View mProgressView;
private View mLoginFormView;
public String id;
#Override
#TargetApi(19)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
setContentView(R.layout.activity_login_registration);
// Set up the login form.
mEmailView = (EditText) findViewById(R.id.email);
mPasswordView = (EditText) findViewById(R.id.password);
Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
mLoginFormView = findViewById(R.id.login_form);
mProgressView = findViewById(R.id.login_progress);
mRegistrationScreen = findViewById(R.id.registerLink);
mEmailSignInButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String emailAddress = mEmailView.getText().toString().trim();
String password = mPasswordView.getText().toString().trim();
UserLoginTask callDOIB = new UserLoginTask();
try {
String loginSuccessFail = callDOIB.execute(emailAddress, password).get();
try {
JSONObject jsonObject = new JSONObject(loginSuccessFail);
id = jsonObject.getString("id");
//JSONObject id = jArray.getJSONObject(0);
System.out.println("json object at LoginActivity id is "+id);
authoriseLogin(loginSuccessFail, id);
}catch (JSONException e){
e.printStackTrace();
}
}catch(InterruptedException e){
e.printStackTrace();
}
catch (ExecutionException e){
e.printStackTrace();
}
}
});
mRegistrationScreen.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent loadRegistration = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(loadRegistration);
}
});
}
public void authoriseLogin(String authorise, String id){
if (authorise.contains("id")) {
System.out.println(" accessLRA.authoriseLogin login has been called");
Session session = new Session(getApplicationContext());
session.setId(id);
Intent loadPersonal = new Intent(LoginActivity.this, MainActivity.class);
startActivity(loadPersonal);
System.out.println("Login has been authorised");
}
else{
System.out.println("Incorrect username or password");
}
}
}
It is giving me an inflate exception when loading the layout file using setContentView()
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.a_phi.myapplication/com.example.a_phi.myapplication.LoginActivity}:
android.view.InflateException: Binary XML file line #2: Error
inflating class
As suggested in the comments section, I was actually missing some other res -v21 folders which support API level 21 for Android KitKat. For some reason I didn't move them into my new project. It is working now, thank you.

Fatal exception at main android during dropbox authentication [duplicate]

This question already exists:
Dropbox Core API is not working. I don't want to hardcode app key and secret?
Closed 7 years ago.
I am getting fatal exception error at main nullpointer exception. basically i have 2 activities login and main activity.you will enter app key and app secret in login activity then after authentication it will take you to main activity any help ??
login activity
public class login_activity extends AppCompatActivity {
public String APP_kEY ;
public String APP_SECRET;
public String accessToken;
EditText app_key_view;
EditText App_secret_view;
Button dropbox;
public DropboxAPI<AndroidAuthSession> mDBApi;
#Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences pref = getSharedPreferences("login",MODE_PRIVATE);
APP_kEY = pref.getString("appkey",null);
APP_SECRET = pref.getString("appsecret",null);
accessToken = pref.getString("access",null);
if (accessToken != null){
AppKeyPair appkeys = new AppKeyPair(APP_kEY,APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appkeys);
session.setOAuth2AccessToken(accessToken);
Intent i = new Intent(login_activity.this,MainActivity.class);
startActivity(i);
finish();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_activity);
app_key_view=(EditText)findViewById(R.id.app_key_text);
App_secret_view=(EditText)findViewById(R.id.app_secret_text);
dropbox = (Button)findViewById(R.id.link_dropbox);
//////////////////////////////////////////////////////////////
dropbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
APP_kEY = app_key_view.getText().toString();
APP_SECRET = App_secret_view.getText().toString();
AppKeyPair appKeys = new AppKeyPair(APP_kEY,APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys);
mDBApi = new DropboxAPI<>(session);
mDBApi.getSession().startOAuth2Authentication(login_activity.this);
}
});
}
#Override
protected void onResume () {
super.onResume();
if(mDBApi.getSession().authenticationSuccessful()){
try {
mDBApi.getSession().finishAuthentication();
accessToken = mDBApi.getSession().getOAuth2AccessToken();
SharedPreferences.Editor editor = getSharedPreferences("login",MODE_PRIVATE).edit();
editor.putString("appkey",APP_kEY);
editor.putString("appsecret",APP_SECRET);
editor.putString("access",accessToken);
editor.commit();
Intent i = new Intent(login_activity.this,MainActivity.class);
startActivity(i);
finish();
}
catch (IllegalStateException e){
Log.i("DbAuthLog", "Error authenticationg", e);
}
}
}
Logcat
09-11 21:06:01.947 3842-3842/? I/art﹕ Late-enabling -Xcheck:jni
09-11 21:06:01.975 3842-3851/? I/art﹕ Debugger is no longer active
09-11 21:06:02.370 3842-3857/? I/art﹕ Background sticky concurrent mark sweep GC freed 2909(253KB) AllocSpace objects, 2(32KB) LOS objects, 0% free, 50MB/50MB, paused 36.891ms total 133.660ms
09-11 21:06:02.803 3842-3842/? D/AndroidRuntime﹕ Shutting down VM
09-11 21:06:02.803 3842-3842/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ayzaz.timescopev1.timescope, PID: 3842
java.lang.RuntimeException: Unable to resume activity {com.example.ayzaz.timescopev1.timescope/com.example.ayzaz.timescopev1.timescope.login_activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.dropbox.client2.session.Session com.dropbox.client2.DropboxAPI.getSession()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2986)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.dropbox.client2.session.Session com.dropbox.client2.DropboxAPI.getSession()' on a null object reference
at com.example.ayzaz.timescopev1.timescope.login_activity.onResume(login_activity.java:77)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
at android.app.Activity.performResume(Activity.java:6076)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2975)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
09-11 21:06:05.849 3842-3842/com.example.ayzaz.timescopev1.timescope I/Process﹕ Sending signal. PID: 3842 SIG: 9
onResume() always gets called after onCreate().
When onResume() begins, mDBApi is null, because this field is only ever assigned in the onClick() method.
You either need to assign mDBApi in onCreate() directly, or you need to check that it is not null before you try to use it.

Passing extras from Activity to Intent throws NullPointerException

This is a problem that forked off a different issue.
I'm passing two strings from my main activity to a child service, which is fine, but when the main activity dies, the service throws a NullPointerException trying to grab the two strings.
From MainActivity:
Intent i = new Intent(this, PebbleService.class);
i.putExtra("quote", quote[0]);
i.putExtra("author", quote[1]);
startService(i);
From PebbleService:
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
final String author = intent.getStringExtra("author");
final String quote = intent.getStringExtra("quote");
// Define AppMessage behavior
if (appMessageReciever == null) {
appMessageReciever = new PebbleKit.PebbleDataReceiver(WATCHAPP_UUID) {
#Override
public void receiveData(Context context, int transactionId, PebbleDictionary data) {
// Always ACK
PebbleKit.sendAckToPebble(context, transactionId);
// Send KEY_QUOTE to Pebble
PebbleDictionary out = new PebbleDictionary();
out.addString(KEY_QUOTE, quote);
out.addString(KEY_AUTHOR, author);
PebbleKit.sendDataToPebble(getApplicationContext(), WATCHAPP_UUID, out);
}
};
// Add AppMessage capabilities
PebbleKit.registerReceivedDataHandler(this, appMessageReciever);
}
return START_STICKY;
}
Error from Logcat:
05-26 10:29:52.972 4147-4147/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.thevgc.quotes, PID: 4147
java.lang.RuntimeException: Unable to start service net.thevgc.quotes.PebbleService#423eb138 with null: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2732)
at android.app.ActivityThread.access$2100(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at net.thevgc.quotes.PebbleService.onStartCommand(PebbleService.java:34)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2715)
            at android.app.ActivityThread.access$2100(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
As per the documentation:
intent - ... This may be null if the service is being restarted
Therefore check for null intent:
public int onStartCommand(Intent intent, int flags, int startId) {
int cmd = super.onStartCommand(intent, flags, startId);
if (intent == null) return cmd;
final String author = intent.getStringExtra("author");
final String quote = intent.getStringExtra("quote");
...
}

Error while Inflating class?

I migrated a project from Eclipse to Android Studio. In my project, I have added this library.
I have added the library as a dependency in my gradle file. I can import the library from my class.But it shows this
android.view.InflateException: Binary XML file line #8: Error inflating class com.digitalaria.gama.wheel.Wheel
while running the app.
Note - It worked fine while running it from Eclipse.
gradle
apply plugin: 'android'
dependencies {
// compile fileTree(dir: 'libs', include: '*.jar')
compile project(':RemoteIt Protocol')
compile project(':android-support-v7-appcompat')
compile files('libs/gama_wheel_v1.0.jar')
compile files('libs/PayPalAndroidSDK.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<com.digitalaria.gama.wheel.Wheel
android:id="#+id/wheel"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.digitalaria.gama.wheel.Wheel>
</LinearLayout>
Class
import com.digitalaria.gama.wheel.Wheel;
import com.digitalaria.gama.wheel.WheelAdapter;
public class Home extends MainActivity implements OnClickListener
{
private RemoteIt application;
private SharedPreferences preferences;
#Override
protected void onCreate(Bundle savedInstanceState)
{
this.application = (RemoteIt) this.getApplication();
this.preferences = this.application.getPreferences();
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(s);
this.checkOnCreate();
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.home, null, false);
mDrawer.addView(contentView, 0);
init();
wheel();
}
private void wheel()
{
wheel.setOnItemClickListener(new WheelAdapter.OnItemClickListener()
{
#Override
public void onItemClick(WheelAdapter<?> parent, View view, int position, long id)
{
switch (position)
{
case 0:
Intent ac = new Intent(Home.this, ConnectionListActivity.class);
startActivity(ac);
break;
case 1:
Intent b = new Intent(Home.this, ControlActivity.class);
startActivity(b);
break;
case 2:
Intent c = new Intent(Home.this, FileExplorerActivity.class);
startActivity(c);
// this.toggleKeyboard();
break;
case 3:
Intent d = new Intent(Home.this, Presentation.class);
startActivity(d);
break;
case 4:
Intent e = new Intent(Home.this, Media.class);
startActivity(e);
break;
case 5:
Intent f = new Intent(Home.this, Shortcuts.class);
startActivity(f);
break;
case 6:
Intent g = new Intent(Home.this, Browser.class);
startActivity(g);
break;
}
}
});
}
private Wheel wheel;
private Resources res;
private int[] icons = {
R.drawable.conn, R.drawable.mouse, R.drawable.file, R.drawable.present, R.drawable.media, R.drawable.shortc, R.drawable.browser
};
private void init()
{
res = getApplicationContext().getResources();
wheel = (Wheel) findViewById(R.id.wheel);
wheel.setItems(getDrawableFromData(icons));
wheel.setWheelDiameter((int) getResources().getDimension(R.dimen.diameter));
}
private Drawable[] getDrawableFromData(int[] data)
{
Drawable[] ret = new Drawable[data.length];
for (int i = 0; i < data.length; i++)
{
ret[i] = res.getDrawable(data[i]);
}
return ret;
}
LogCat
Process: com.RemoteIt.client, PID: 1825
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.RemoteIt.client/com.RemoteIt.client.activity.Home}: android.view.InflateException: Binary XML file line #8: Error inflating class com.digitalaria.gama.wheel.Wheel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.digitalaria.gama.wheel.Wheel
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.RemoteIt.client.activity.Home.onCreate(Home.java:50)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at com.RemoteIt.client.activity.Home.onCreate(Home.java:50)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsupportedOperationException: Can't convert to integer: type=0x3
at android.content.res.TypedArray.getInteger(TypedArray.java:368)
at com.digitalaria.gama.wheel.WheelBehavior.<init>(WheelBehavior.java:117)
at com.digitalaria.gama.wheel.Wheel.<init>(Wheel.java:83)
at com.digitalaria.gama.wheel.Wheel.<init>(Wheel.java:68)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
            at android.view.LayoutInflater.createView(LayoutInflater.java:594)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at com.RemoteIt.client.activity.Home.onCreate(Home.java:50)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
Wheel.java
public final class Wheel extends android.widget.FrameLayout {
public static final int CCW = -1;
public static final int CW = 1;
private com.digitalaria.gama.wheel.WheelBehavior _wheelBehavior;
private android.view.ViewStub _backgroundViewDummy;
protected android.view.View _backgroundView;
protected boolean _hasBackgroundImage;
private android.widget.FrameLayout.LayoutParams params;
private int _reservedPositionLeft;
private int _reservedPositionTop;
private boolean isLayout;
private int _storedLeft;
private int _storedTop;
private int _storedRight;
private int _storedBottom;
private int _centerX;
private int _centerY;
public Wheel(android.content.Context context) { /* compiled code */ }
public Wheel(android.content.Context context, android.util.AttributeSet attrs) { /* compiled code */ }
public Wheel(android.content.Context context, android.util.AttributeSet attrs, int defStyle) { /* compiled code */ }
protected void onLayout(boolean changed, int l, int t, int r, int b) { /* compiled code */ }
public void addView(android.view.View child) { /* compiled code */ }
public void addView(android.view.View child, int index) { /* compiled code */ }
public void removeView(android.view.View child) { /* compiled code */ }
public void removeViewAt(int index) { /* compiled code */ }
public float getCenterX() { /* compiled code */ }
public float getCenterY() { /* compiled code */ }
public final void setOnItemClickListener(com.digitalaria.gama.wheel.WheelAdapter.OnItemClickListener listener) { /* compiled code */ }
public final com.digitalaria.gama.wheel.WheelAdapter.OnItemClickListener getOnItemClickListener() { /* compiled code */ }
public final void setOnWheelRotationListener(com.digitalaria.gama.wheel.WheelAdapter.OnWheelRotationListener listener) { /* compiled code */ }
public final com.digitalaria.gama.wheel.WheelAdapter.OnWheelRotationListener getOnWheelRotationListener() { /* compiled code */ }
public void setOnItemSelectionUpdatedListener(com.digitalaria.gama.wheel.WheelAdapter.OnItemSelectionUpdatedListener listener) { /* compiled code */ }
public void setPosition(int left, int top) { /* compiled code */ }
public void setSelectionAngle(int angle) { /* compiled code */ }
public void setItems(android.content.res.TypedArray items) { /* compiled code */ }
public void setItems(android.graphics.drawable.Drawable[] drawables) { /* compiled code */ }
public void setWheelDiameter(int diameter) { /* compiled code */ }
public void setWheelBackground(int inflatedId, int layoutResource) { /* compiled code */ }
public void setRotatedItem(boolean flag) { /* compiled code */ }
public void configureWheelBackground(int initRotationAngle, boolean rotatedItem) { /* compiled code */ }
public void configureWheelBackground(boolean rotatedItem) { /* compiled code */ }
protected boolean hasBackgroundImage() { /* compiled code */ }
public void setTouchArea(int from, int to) { /* compiled code */ }
public int nextItem() { /* compiled code */ }
public int previousItem() { /* compiled code */ }
public boolean isRotationFinished() { /* compiled code */ }
public void flingStartUsingAngle(float angle) { /* compiled code */ }
public void flingStartUsingVelocity(int vx, int vy, boolean scroolToSlot) { /* compiled code */ }
public void flingStartUsingVelocityWithDirection(int vx, int vy, int direction) { /* compiled code */ }
public int getSelectedItem() { /* compiled code */ }
public int getSelectedItem(boolean stopScroll) { /* compiled code */ }
public boolean setSelectedItem(int index) { /* compiled code */ }
public void setItemClickEventAtSelectionPosition(boolean enable) { /* compiled code */ }
public boolean getItemClickEventAtSelectionPosition() { /* compiled code */ }
public void setEnabled(boolean enabled) { /* compiled code */ }
public boolean isLayouted() { /* compiled code */ }
}
The relevant exception :
Caused by: java.lang.UnsupportedOperationException: Can't convert to integer: type=0x3
at android.content.res.TypedArray.getInteger(TypedArray.java:368)
at com.digitalaria.gama.wheel.WheelBehavior.<init>(WheelBehavior.java:117)
WheelBehavior.java, line 117
setWheelDiameter(arr.getInteger(R.styleable.Wheel_wheel_diameter, Configuration.DEFAULT_WHEEL_DIAMETER));
Here are the styleable attributes :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Wheel">
<attr name="wheel_rotation_duration" format="integer"/> <!-- default:400 -->
<attr name="wheel_diameter" format="integer"/> <!-- default:250 -->
<attr name="items" format="integer"/>
<attr name="item_selected_index" format="integer"/>
</declare-styleable>
</resources>
wheel_diameter must be defined somewhere in your project, with something else than a integer inside it. That's why it crashed. But according to your layout, it is not. Try a global search on the string wheel_diameter
I think my answer is a rave, but you can try to set layout_gravity="center" or layout_gravity="center_horizontal" cause I think the creator of the library tries to get this property.
Try to replace this code :
compile files('libs/gama_wheel_v1.0.jar')
compile files('libs/PayPalAndroidSDK.jar')
With this code :
compile fileTree(dir: 'libs', include: '*.jar')
Caused by: java.lang.UnsupportedOperationException: Can't convert to integer: type=0x3
at android.content.res.TypedArray.getInteger(TypedArray.java:368)
at com.digitalaria.gama.wheel.WheelBehavior.<init>(WheelBehavior.java:117)
at com.digitalaria.gama.wheel.Wheel.<init>(Wheel.java:83)
at com.digitalaria.gama.wheel.Wheel.<init>(Wheel.java:68)
I think you need to look into this one. Not sure if Wheel is something you made or downloaded, but it seems to be having a problem in its setup.

Cannot save files to SDcard android app

Im having a problem saving/ creating a file on my sdcard to save text to, i am trying to check if the file is created and if not create it, then write certain data to it on button click. Here is my code and error output:
This is the error output:
java.lang.RuntimeException: Unable to start activity ComponentInfo{ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmeds/ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmeds.MainActivity}: java.lang.IllegalArgumentException: File /sdcard/output.txt contains a path separator
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
at android.app.ActivityThread.access$800(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5034)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1270)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1086)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: File /sdcard/output.txt contains a path separator
at android.app.ContextImpl.makeFilename(ContextImpl.java:2165)
at android.app.ContextImpl.getFileStreamPath(ContextImpl.java:964)
at android.content.ContextWrapper.getFileStreamPath(ContextWrapper.java:195)
at ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmeds.MainActivity.onCreate(MainActivity.java:207)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2151)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
            at android.app.ActivityThread.access$800(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5034)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
and here is the code for the file saving:
String SDRoot = Environment.getExternalStorageDirectory().getPath();
final File output = getFileStreamPath(SDRoot + "/output.txt");
if(!output.exists()){
try {
output.createNewFile();
Context context = getApplicationContext();
CharSequence text = "Output File Created!";
int duration = Toast.LENGTH_LONG;
Toast fileCreated = Toast.makeText(context, text, duration);
fileCreated.show();
} catch (IOException e) {
Context context = getApplicationContext();
CharSequence text = "Output File Not Created!";
int duration = Toast.LENGTH_LONG;
Toast fileNotCreated = Toast.makeText(context, text, duration);
fileNotCreated.show();
e.printStackTrace();
}
}
Button addbutton = (Button)findViewById(R.id.addMeds);
addbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String outputLine = medOut.toString() + doseOut.getText() + dayout.getText() + timeOut.getText();
try {
FileOutputStream fOut = new FileOutputStream(output);
OutputStreamWriter myOutputStreamWriter = new OutputStreamWriter(fOut);
myOutputStreamWriter.append(outputLine);
myOutputStreamWriter.flush();
myOutputStreamWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
}
http://developer.android.com/reference/android/content/Context.html#getFileStreamPath(java.lang.String)
Parameters : name The name of the file for which you would like to get its path.
If you give a path (containing "/") instead, it will crash as you have already noticed.
Besides, this function applies only for your application private files, created with openFileOuput for instance.
http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int)
Just do like this :
String SDRoot = Environment.getExternalStorageDirectory().getPath();
final File output = new File(SDRoot + "/output.txt");

Categories

Resources