I have an app that's supposed to get some string from a server depending on the webpage, and it crashes every time I run it. I don't understand how to even debug it, and I've tried numerous changes. It started crashing ever since I messed with the GUI, and added the ability to switch views if that provides any sort of help.
Here is the code:
public class MainActivity extends Activity implements TextToSpeech.OnInitListener, OnClickListener{
private WebView mWebview;
EditText addressBar;
String currentWebpage = "http://www.aljazeera.com/news/americas/2013/07/20137113200544375.html";
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.linearview);
View main = (View) findViewById(R.layout.activity_main);
View readOut = (View) findViewById(R.layout.read_out);
#Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("Showing Activity_Main...");
viewGroup.addView(View.inflate(this, R.layout.activity_main, null));
super.onCreate(savedInstanceState);
//setContentView(R.menu.main);
addressBar = (EditText)findViewById(R.id.addressBar);
addressBar.setText(currentWebpage);
mWebview = (WebView)findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true); // enables javascript
mWebview.setWebViewClient(new WebViewClient());
System.out.println("Loading Webpage...");
mWebview.loadUrl(currentWebpage);
}
public void speakOut(String text) {
TextToSpeech tts = new TextToSpeech(this, this);
System.out.println("Speaking");
if(tts.isLanguageAvailable(Locale.ENGLISH) != -1){
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
public int speakFull(String text){
switchToRead();
String[] sentences = text.split("\n|\\.(?!\\d)|(?<!\\d)\\."); // Regex that splits the body of text into the sentences of that body which are stored in a String array.
for(int i = 0; i < sentences.length; i++){
speakOut(sentences[i]);
if(i == sentences.length - 1){
return 1;
}
}
return 0;
}
public String getText(String webPage) throws ParseException, IOException{
HttpResponse response = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://someserver.net:8080/" + webPage));
response = client.execute(request);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String responseBody = "No text found on webpage.";
int responseCode = response.getStatusLine().getStatusCode();
switch(responseCode) {
case 200:
HttpEntity entity = response.getEntity();
if(entity != null) {
responseBody = EntityUtils.toString(entity);
}
}
System.out.println("Returning Response..");
System.out.println(responseBody);
return responseBody;
}
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
}
private class tts extends AsyncTask<String, Void, String>{//Async http request to get text
#Override
protected String doInBackground(String... arg0) {
try {
System.out.println("Running seperate thread for TTS.");
int complete = 0;
while(complete == 0){
System.out.println("Speaking full..");
complete = speakFull(getText(mWebview.getUrl()));
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result){
}
}
public void clickPlay(View v){
new tts().execute("");
}
public void clickGo(View v){
if(addressBar.getText() != null){
currentWebpage = addressBar.getText().toString();
System.out.println("Current webpage changed to: " + currentWebpage);
mWebview.loadUrl(currentWebpage);
}
}
public void clickPause(View v){
System.out.println("Clicked pause.");
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
public void switchToRead(){// Switches to the reading view which displays the text that the tts engine reads off.
System.out.println("Swtiching view to Read.");
viewGroup.removeView(main);
viewGroup.addView(View.inflate(this, R.layout.read_out, null));
}
public void switchToMain(){
System.out.println("Switching view to Main.");
viewGroup.removeView(readOut);
viewGroup.addView(View.inflate(this, R.layout.activity_main, null));
}
}
Also here are the numerous errors I encounter when launching my app:
08-01 14:53:10.210: E/Trace(812): error opening trace file: No such file or directory (2)
08-01 14:53:10.600: D/AndroidRuntime(812): Shutting down VM
08-01 14:53:10.631: W/dalvikvm(812): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-01 14:53:10.660: E/AndroidRuntime(812): FATAL EXCEPTION: main
08-01 14:53:10.660: E/AndroidRuntime(812): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.webview/com.example.webview.MainActivity}: java.lang.NullPointerException
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.os.Looper.loop(Looper.java:137)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 14:53:10.660: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 14:53:10.660: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 14:53:10.660: E/AndroidRuntime(812): at dalvik.system.NativeStart.main(Native Method)
08-01 14:53:10.660: E/AndroidRuntime(812): Caused by: java.lang.NullPointerException
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.Activity.findViewById(Activity.java:1839)
08-01 14:53:10.660: E/AndroidRuntime(812): at com.example.webview.MainActivity.<init>(MainActivity.java:39)
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.Class.newInstanceImpl(Native Method)
08-01 14:53:10.660: E/AndroidRuntime(812): at java.lang.Class.newInstance(Class.java:1319)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-01 14:53:10.660: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-01 14:53:10.660: E/AndroidRuntime(812): ... 11 more
08-01 14:53:27.439: D/AndroidRuntime(860): Shutting down VM
08-01 14:53:27.439: W/dalvikvm(860): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-01 14:53:27.449: E/AndroidRuntime(860): FATAL EXCEPTION: main
08-01 14:53:27.449: E/AndroidRuntime(860): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.webview/com.example.webview.MainActivity}: java.lang.NullPointerException
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.os.Looper.loop(Looper.java:137)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 14:53:27.449: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-01 14:53:27.449: E/AndroidRuntime(860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-01 14:53:27.449: E/AndroidRuntime(860): at dalvik.system.NativeStart.main(Native Method)
08-01 14:53:27.449: E/AndroidRuntime(860): Caused by: java.lang.NullPointerException
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.Activity.findViewById(Activity.java:1839)
08-01 14:53:27.449: E/AndroidRuntime(860): at com.example.webview.MainActivity.<init>(MainActivity.java:39)
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.Class.newInstanceImpl(Native Method)
08-01 14:53:27.449: E/AndroidRuntime(860): at java.lang.Class.newInstance(Class.java:1319)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
08-01 14:53:27.449: E/AndroidRuntime(860): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
08-01 14:53:27.449: E/AndroidRuntime(860): ... 11 more
Move your view initialization inside onCreate after setContentView
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.linearview);
...// rest of the code
Related
I have this source code for an mp3 download app, and it worked fine last time I used it, few months ago. Now when I try it, it gives me an error, when I click on a song to download it, from search results. Here's the error:
08-01 13:06:09.227: E/AndroidRuntime(2075): FATAL EXCEPTION: main
08-01 13:06:09.227: E/AndroidRuntime(2075): Process: com.soundload, PID: 2075
08-01 13:06:09.227: E/AndroidRuntime(2075): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Boolean de.voidplus.soundcloud.Track.isStreamable()' on a null object reference
08-01 13:06:09.227: E/AndroidRuntime(2075): at com.baixavideos.Downloader.onPostExecute(Downloader.java:41)
08-01 13:06:09.227: E/AndroidRuntime(2075): at com.baixavideos.Downloader.onPostExecute(Downloader.java:1)
08-01 13:06:09.227: E/AndroidRuntime(2075): at android.os.AsyncTask.finish(AsyncTask.java:636)
08-01 13:06:09.227: E/AndroidRuntime(2075): at android.os.AsyncTask.access$500(AsyncTask.java:177)
08-01 13:06:09.227: E/AndroidRuntime(2075): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
08-01 13:06:09.227: E/AndroidRuntime(2075): at android.os.Handler.dispatchMessage(Handler.java:102)
08-01 13:06:09.227: E/AndroidRuntime(2075): at android.os.Looper.loop(Looper.java:135)
08-01 13:06:09.227: E/AndroidRuntime(2075): at android.app.ActivityThread.main(ActivityThread.java:5254)
08-01 13:06:09.227: E/AndroidRuntime(2075): at java.lang.reflect.Method.invoke(Native Method)
08-01 13:06:09.227: E/AndroidRuntime(2075): at java.lang.reflect.Method.invoke(Method.java:372)
08-01 13:06:09.227: E/AndroidRuntime(2075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
08-01 13:06:09.227: E/AndroidRuntime(2075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
08-01 13:06:11.353: W/System.err(2174): java.io.IOException: open failed: EACCES (Permission denied)
08-01 13:06:11.353: W/System.err(2174): at java.io.File.createNewFile(File.java:941)
08-01 13:06:11.354: W/System.err(2174): at de.voidplus.soundcloud.SoundCloud.<init>(Unknown Source)
08-01 13:06:11.354: W/System.err(2174): at com.baixavideos.ui.MainActivity.<clinit>(MainActivity.java:63)
08-01 13:06:11.354: W/System.err(2174): at java.lang.reflect.Constructor.newInstance(Native Method)
08-01 13:06:11.354: W/System.err(2174): at java.lang.Class.newInstance(Class.java:1606)
08-01 13:06:11.354: W/System.err(2174): at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
Downloader.java:
package com.baixavideos;
import com.baixavideos.ui.SongDetails;
import de.voidplus.soundcloud.SoundCloud;
import de.voidplus.soundcloud.Track;
import android.os.AsyncTask;
import android.util.Log;
public class Downloader extends AsyncTask<String, Void, Track>{
private int id;
private SoundCloud soundcloud;
private SongDetails ui;
public Downloader(){
soundcloud = new SoundCloud("cd07b57592f6d914f437f8ab2856363d", "841953bd718a3ceb427b3e3be170a014");
}
public int getId() {
return id;
}
public Downloader setId(int id) {
this.id = id;
return this;
}
#Override
protected Track doInBackground(String... arg0) {
Track track = soundcloud.getTrack(getId());
return track;
}
#Override
protected void onPostExecute(Track track) {
super.onPostExecute(track);
if(track.isStreamable()){
getUi().onSongLoaded(track);
} else {
getUi().onStreamingError();
}
}
public SongDetails getUi() {
return ui;
}
public Downloader setUi(SongDetails ui) {
this.ui = ui;
return this;
}
}
Anyone has any idea what's going on here?
It sounds like permission issues for your temp directory for your app. Your app can only put files into it's own app directory and for some reason permission is getting denied. That's why it's essentially failing ultimately in the isStreamable() being null.
Also have you tried adding permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE></uses-permission>
Also onPostExecute() you should do a check to see if track!=null, then catch the error and log it.
I am trying to caal my Utill.java class from my Profile.java class.
But here ia m facing issue on run time.
I am getting error of unable to find activity compmonent.
This is my code file.
Profile.java
public class Profile extends ListActivity implements OnItemLongClickListener{
//creating context menu
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
// creating context item
#Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.item1:
//calling vibration method from here
startActivity(new Intent(Profile.this,Util.class));
return true;
case R.id.item2 :
startActivity(new Intent(Profile.this,Custom.class));
return true;
default:
return super.onContextItemSelected(item);
}
}
}
Utill.java
public class Util extends Activity{
private AudioManager audi = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
audi = (AudioManager) getSystemService(AUDIO_SERVICE);
//creating object to call methods
Util mytest = new Util();
mytest.activateVibration(); // calling method
activateVibration();
}
//Method for activate vibration
private void activateVibration() {
// TODO Auto-generated method stub
audi.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}
This is my logcat file
08-01 00:55:07.122: D/libEGL(26849): loaded /system/lib/egl/libGLES_hawaii.so
08-01 00:55:07.122: D/(26849): mem_init ++
08-01 00:55:07.122: D/(26849): gHwMemAllocator client 3
08-01 00:55:07.122: D/(26849): **** Using ION allocator ****
08-01 00:55:07.122: D/(26849): registered SIGUSR1[10] for pid[26849]
08-01 00:55:07.122: D/(26849): HwMemAllocatorImpl Static Counters 0 0
08-01 00:55:07.122: D/(26849): HwMemAllocatorImpl[4fa2edcc] totalDeviceAllocSize[0] totalFree[0] maxFree[0] in numSlabs[0]
08-01 00:55:07.122: D/(26849): mem_init 4fa2edcc--
08-01 00:55:07.122: D/ION(26849): config: version(0x10000) secure(0xf000) 256M(0x22d) fast(0x608) hwwr(0x608)
08-01 00:55:07.142: D/MM_DEVICE(26849): Waiting for mm thread to come up
08-01 00:55:07.142: D/MM_DEVICE(26849): mm_device_thread starting
08-01 00:55:07.152: D/HAWAII_EGL(26849): eglCreateContext() config: 18 context: 0x4fc28fa8, VC context 1, Thread 26849
08-01 00:55:07.152: D/HAWAII_EGL(26849): Set SWAP INTERVAL 0
08-01 00:55:07.162: D/HAWAII_EGL(26849): eglCreateWindowSurface() surface: 0x4fc28fc8, VC surface: 1, Thread: 26849
08-01 00:55:07.162: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x4fc28fc8, 0x4fc28fc8) Thread: 26849
08-01 00:55:07.162: D/OpenGLRenderer(26849): Enabling debug mode 0
08-01 00:55:08.233: D/AbsListView(26849): Get MotionRecognitionManager
08-01 00:55:08.253: D/AbsListView(26849): onVisibilityChanged() is called, visibility : 4
08-01 00:55:08.253: D/AbsListView(26849): unregisterIRListener() is called
08-01 00:55:08.253: D/AbsListView(26849): onVisibilityChanged() is called, visibility : 0
08-01 00:55:08.253: D/AbsListView(26849): unregisterIRListener() is called
08-01 00:55:08.273: D/HAWAII_EGL(26849): Set SWAP INTERVAL 0
08-01 00:55:08.273: D/HAWAII_EGL(26849): eglCreateWindowSurface() surface: 0x50672480, VC surface: 2, Thread: 26849
08-01 00:55:08.273: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x50672480, 0x50672480) Thread: 26849
08-01 00:55:08.273: D/AbsListView(26849): unregisterIRListener() is called
08-01 00:55:08.273: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x4fc28fc8, 0x4fc28fc8) Thread: 26849
08-01 00:55:08.283: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x50672480, 0x50672480) Thread: 26849
08-01 00:55:08.293: D/AbsListView(26849): unregisterIRListener() is called
08-01 00:55:08.293: D/WritingBuddyImpl(26849): getCurrentWritingBuddyView()
08-01 00:55:08.343: D/AbsListView(26849): unregisterIRListener() is called
08-01 00:55:09.174: D/AbsListView(26849): Get MotionRecognitionManager
08-01 00:55:09.254: D/HAWAII_EGL(26849): Set SWAP INTERVAL 0
08-01 00:55:09.254: D/HAWAII_EGL(26849): eglCreateWindowSurface() surface: 0x50541bf0, VC surface: 3, Thread: 26849
08-01 00:55:09.254: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x50541bf0, 0x50541bf0) Thread: 26849
08-01 00:55:09.264: D/AbsListView(26849): unregisterIRListener() is called
08-01 00:55:09.264: D/AbsListView(26849): unregisterIRListener() is called
08-01 00:55:09.274: D/AbsListView(26849): unregisterIRListener() is called
08-01 00:55:09.284: D/AbsListView(26849): unregisterIRListener() is called
08-01 00:55:09.294: D/AbsListView(26849): unregisterIRListener() is called
08-01 00:55:09.474: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x50672480, 0x50672480) Thread: 26849
08-01 00:55:09.865: D/HAWAII_EGL(26849): eglMakeCurrent(0x4fc28fa8, 0x50541bf0, 0x50541bf0) Thread: 26849
08-01 00:55:09.985: D/HAWAII_EGL(26849): eglMakeCurrent(NULL) Thread: 26849
08-01 00:55:09.985: D/HAWAII_EGL(26849): eglDestroySurface() surface: 0x50541bf0, android window 0x500cd850, Thread: 26849
08-01 00:55:10.005: D/AbsListView(26849): onDetachedFromWindow
08-01 00:55:10.015: D/AndroidRuntime(26849): Shutting down VM
08-01 00:55:10.015: W/dalvikvm(26849): threadid=1: thread exiting with uncaught exception (group=0x41d7c930)
08-01 00:55:10.045: E/AndroidRuntime(26849): FATAL EXCEPTION: main
08-01 00:55:10.045: E/AndroidRuntime(26849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.haider.first/com.haider.first.Util}: java.lang.NullPointerException
08-01 00:55:10.045: E/AndroidRuntime(26849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
08-01 00:55:10.045: E/AndroidRuntime(26849): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
08-01 00:55:10.045: E/AndroidRuntime(26849): at android.app.ActivityThread.access$700(ActivityThread.java:157)
08-01 00:55:10.045: E/AndroidRuntime(26849): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
08-01 00:55:10.045: E/AndroidRuntime(26849): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 00:55:10.045: E/AndroidRuntime(26849): at android.os.Looper.loop(Looper.java:176)
08-01 00:55:10.045: E/AndroidRuntime(26849): at android.app.ActivityThread.main(ActivityThread.java:5317)
08-01 00:55:10.045: E/AndroidRuntime(26849): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 00:55:10.045: E/AndroidRuntime(26849): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 00:55:10.045: E/AndroidRuntime(26849): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-01 00:55:10.045: E/AndroidRuntime(26849): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-01 00:55:10.045: E/AndroidRuntime(26849): at dalvik.system.NativeStart.main(Native Method)
08-01 00:55:10.045: E/AndroidRuntime(26849): Caused by: java.lang.NullPointerException
08-01 00:55:10.045: E/AndroidRuntime(26849): at com.haider.first.Util.activateVibration(Util.java:30)
08-01 00:55:10.045: E/AndroidRuntime(26849): at com.haider.first.Util.onCreate(Util.java:20)
08-01 00:55:10.045: E/AndroidRuntime(26849): at android.app.Activity.performCreate(Activity.java:5326)
08-01 00:55:10.045: E/AndroidRuntime(26849): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
08-01 00:55:10.045: E/AndroidRuntime(26849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
08-01 00:55:10.045: E/AndroidRuntime(26849): ... 11 more
i am facing error "unable to find activitycomponentinfo, java.lang.runtimeexception"
plzz helpppp !!
Your Util class doesn't need to extend Activity(when Activity is extended a new window will appear when you start it with Intent). So you need to do it like this(Right click on java folder--->New Java class(If you are using Android Studio, and don't extend Activity):
public class Util{
private AudioManager audi = null;
//Method for activate vibration
private void activateVibration() {
// TODO Auto-generated method stub
audi = (AudioManager) getSystemService(AUDIO_SERVICE);
audi.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}
And call the method like that in your activity:
switch(item.getItemId()){
case R.id.item1:
//Object initialization
Util myUtil = new Util();
//Call activateVibration() method
myUtil.activateVibration();
return true;
case R.id.item2:
startActivity(new Intent(Profile.this,Custom.class));
return true;
default:
return super.onContextItemSelected(item);
}
I think you need to learn more about Java and OOP concepts in order to start developing Android apps.
Hope it helps!
im making a simple calories calculator and it gives me that error, i already reviewed the code and searched here for the solution but im not able to see why is not running.
I thought it was because i had not initialized the variable, so i did it and still got the same error, maybe is something with the spinners, im new on using spinners
here is the code:
public class CaloriesCalculator extends ActionBarActivity {
EditText etAge, etWeight, etHeight;
Button btnCalculate;
TextView tvResult;
Spinner spinnerGender, spinnerActivity;
String itemGender, itemActivity;
int Height=0;
int Weight=0;
int Age=0;;
double bmr=0.0;
double tdee=0.0;
String result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.caloriescalculator);
spinnerGender=(Spinner)findViewById(R.id.spinnerGender);
spinnerActivity=(Spinner)findViewById(R.id.spinnerActivity);
etAge=(EditText)findViewById(R.id.etAge);
etWeight=(EditText)findViewById(R.id.etWeight);
etHeight=(EditText)findViewById(R.id.etHeight);
tvResult=(TextView)findViewById(R.id.tvResult);
List<String> list = new ArrayList<String>();
list.add("Male");
list.add("Female");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, list );
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerGender.setAdapter(dataAdapter);
List<String> list2 = new ArrayList<String>();
list.add("Sedentary");
list.add("Lightly Active");
list.add("Moderalety Active");
list.add("Very Active");
list.add("Extremely Active");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, list2 );
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerActivity.setAdapter(dataAdapter2);
btnCalculate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
spinnerGender.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
spinnerActivity.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
itemGender=String.valueOf(spinnerGender.getSelectedItem());
itemActivity=String.valueOf(spinnerActivity.getSelectedItem());
if(itemGender=="Male"){
Weight=Integer.parseInt(etWeight.getText().toString());
Height=Integer.parseInt(etHeight.getText().toString());
Age=Integer.parseInt(etAge.getText().toString());
if(itemActivity=="Sedentary"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.2;
}
else if(itemActivity=="Lightly Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.375;
}
else if(itemActivity=="Moderalety Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.55;
}
else if(itemActivity=="Very Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.725;
}
else if(itemActivity=="Extremely Active"){
bmr=66+((13.7 * Weight)+(5*Height))-(6.8*Age);
tdee=bmr*1.9;
}
}
else if(itemGender=="Female") {
Weight=Integer.parseInt(etWeight.getText().toString());
Height=Integer.parseInt(etHeight.getText().toString());
Age=Integer.parseInt(etAge.getText().toString());
if(itemActivity=="Sedentary"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.2;
}
else if(itemActivity=="Lightly Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.375;
}
else if(itemActivity=="Moderalety Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.55;
}
else if(itemActivity=="Very Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.725;
}
else if(itemActivity=="Extremely Active"){
bmr=655+((9.6*Weight)+(1.8*Height))-(4.7*Age);
tdee=bmr*1.9;
}
}
result=Double.toString(tdee);
tvResult.setText(result);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat:
11-28 17:20:05.501: E/AndroidRuntime(1455): FATAL EXCEPTION: main
11-28 17:20:05.501: E/AndroidRuntime(1455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.calculadoracalorias/com.app.calculadoracalorias.CaloriesCalculator}: java.lang.NullPointerException
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.os.Looper.loop(Looper.java:137)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 17:20:05.501: E/AndroidRuntime(1455): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:20:05.501: E/AndroidRuntime(1455): at java.lang.reflect.Method.invoke(Method.java:525)
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 17:20:05.501: E/AndroidRuntime(1455): at dalvik.system.NativeStart.main(Native Method)
11-28 17:20:05.501: E/AndroidRuntime(1455): Caused by: java.lang.NullPointerException
11-28 17:20:05.501: E/AndroidRuntime(1455): at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:67)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.Activity.performCreate(Activity.java:5133)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 17:20:05.501: E/AndroidRuntime(1455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 17:20:05.501: E/AndroidRuntime(1455): ... 11 more
11-28 17:24:42.341: D/AndroidRuntime(1507): Shutting down VM
11-28 17:24:42.341: W/dalvikvm(1507): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-28 17:24:42.371: E/AndroidRuntime(1507): FATAL EXCEPTION: main
11-28 17:24:42.371: E/AndroidRuntime(1507): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.calculadoracalorias/com.app.calculadoracalorias.CaloriesCalculator}: java.lang.NullPointerException
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.os.Looper.loop(Looper.java:137)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-28 17:24:42.371: E/AndroidRuntime(1507): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 17:24:42.371: E/AndroidRuntime(1507): at java.lang.reflect.Method.invoke(Method.java:525)
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-28 17:24:42.371: E/AndroidRuntime(1507): at dalvik.system.NativeStart.main(Native Method)
11-28 17:24:42.371: E/AndroidRuntime(1507): Caused by: java.lang.NullPointerException
11-28 17:24:42.371: E/AndroidRuntime(1507): at com.app.calculadoracalorias.CaloriesCalculator.onCreate(CaloriesCalculator.java:70)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.Activity.performCreate(Activity.java:5133)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-28 17:24:42.371: E/AndroidRuntime(1507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-28 17:24:42.371: E/AndroidRuntime(1507): ... 11 more
Yout btnCalculate is null, you should initialize before doing the onclickListener:
btnCalculate = (Button)findViewById(R.id.btnCalculate); //your id
Also note that you are initializing the dataAdapter2 but actually you are using dataAdapert, and list2 dont have any elements, because you declare it but u are filling always list1
If my Webview (mainWebView) current URL contains a word like "/start", I want let it do something. Currently throwing out errors, some ideas?
public class GatewayActivity extends Activity {
private String CurUrl;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gateway);
WebView mainWebView = (WebView) findViewById(R.id.WebView1);
// other Webview Data
mainWebView.loadUrl("https://url.com/start");
CurUrl = mainWebView.getOriginalUrl();
if(CurUrl.indexOf("/start") > -1) {
Toast error=Toast.makeText(this, "test", 2000);
error.show();
}
else {
Toast error=Toast.makeText(this, "test failed", 2000);
error.show();
}
}
Edit:
Thanks for help, but still getting error like that one:
08-01 17:42:40.971: E/AndroidRuntime(16741): FATAL EXCEPTION: main
08-01 17:42:40.971: E/AndroidRuntime(16741): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.XXX.XXX/com.XXX.XXX.GatewayActivity}: java.lang.NullPointerException
08-01 17:42:40.971: E/AndroidRuntime(16741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
08-01 17:42:40.971: E/AndroidRuntime(16741): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
08-01 17:42:40.971: E/AndroidRuntime(16741): at android.app.ActivityThread.access$600(ActivityThread.java:151)
08-01 17:42:40.971: E/AndroidRuntime(16741): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
08-01 17:42:40.971: E/AndroidRuntime(16741): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 17:42:40.971: E/AndroidRuntime(16741): at android.os.Looper.loop(Looper.java:155)
08-01 17:42:40.971: E/AndroidRuntime(16741): at android.app.ActivityThread.main(ActivityThread.java:5493)
08-01 17:42:40.971: E/AndroidRuntime(16741): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 17:42:40.971: E/AndroidRuntime(16741): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 17:42:40.971: E/AndroidRuntime(16741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
08-01 17:42:40.971: E/AndroidRuntime(16741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
08-01 17:42:40.971: E/AndroidRuntime(16741): at dalvik.system.NativeStart.main(Native Method)
08-01 17:42:40.971: E/AndroidRuntime(16741): Caused by: java.lang.NullPointerException
08-01 17:42:40.971: E/AndroidRuntime(16741): at com.XXX.XXX.GatewayActivity.onCreate(GatewayActivity.java:72)
08-01 17:42:40.971: E/AndroidRuntime(16741): at android.app.Activity.performCreate(Activity.java:5066)
08-01 17:42:40.971: E/AndroidRuntime(16741): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
08-01 17:42:40.971: E/AndroidRuntime(16741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
But only with included my If-query like above.
Still not work.
Same error, changed to:
mainWebView.loadUrl("https://url.com");
String cururl = null;
cururl = mainWebView.getOriginalUrl();
if(cururl.contains("/start")) {
//Toast error=Toast.makeText(this, "test", Toast.LENGTH_SHORT);
//error.show();
}
else {
//Toast error2=Toast.makeText(this, "nope", Toast.LENGTH_SHORT);
//error2.show();
}
One mistake I see in your code is in the syntax of Toast.makeTest(this, "test", 2000)
You have specified the duration to 2000 which is wrong. The only values supported by Toast duration are Toast.LENGTH_SHORT and Toast.LENGTH_LONG, whose values in integer are 0 and 1.
Read more about it here
What if You try to parse "/start" in onPageFinished (or even onPageStarted)
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(url.contains("/start")) {
Toast.makeText(GatewayActivity.this, "test", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(GatewayActivity.this, "test", Toast.LENGTH_LONG).show();
}
}
if You'll get the same error, so your problem in other stuff.
I'm trying to use the loader concept in Android for the first time.
Here is my testing code:
package at.powersoftware.hello.loader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
public class HelloLoaderActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
static final int LOADER_CONTACTS = 1; // ID of the loader
ListView list;
SimpleCursorAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loaderactivity);
list = (ListView) findViewById(R.id.listView);
getSupportLoaderManager().initLoader(LOADER_CONTACTS, null, this);
String[] from = {Contacts.DISPLAY_NAME, Contacts.TIMES_CONTACTED};
int[] to = {R.id.tvDisplayName, R.id.tvTimesContacted};
adapter = new SimpleCursorAdapter(this, R.layout.contactlist3, null, from, to, 0);
list.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.loaderactivity, menu);
return true;
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = {Contacts._ID, Contacts.DISPLAY_NAME, Contacts.TIMES_CONTACTED};
String selection = null; //"1=1" or even "" works;
String[] selectionArgs = null;
String sortOrder = Contacts.TIMES_CONTACTED + " desc, " + Contacts.DISPLAY_NAME;
CursorLoader cl = new CursorLoader(this, Contacts.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
return cl;
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
adapter.swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
adapter.swapCursor(null);
}
}
I want to select all records of the content provider, therefore I specify null for the 'selection' argument. But this gives me the error below.
If I supply a selection criteria like "1=1" or even "" it works perfectly.
Any ideas what the problem could be? - Thanks in advance!
08-01 13:28:06.092: E/AndroidRuntime(1528): FATAL EXCEPTION: main
08-01 13:28:06.092: E/AndroidRuntime(1528): java.lang.RuntimeException: Unable to start activity ComponentInfo{at.powersoftware.hello.loader/at.powersoftware.hello.loader.HelloLoaderActivity}: java.lang.NullPointerException: println needs a message
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.os.Looper.loop(Looper.java:123)
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-01 13:28:06.092: E/AndroidRuntime(1528): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 13:28:06.092: E/AndroidRuntime(1528): at java.lang.reflect.Method.invoke(Method.java:507)
08-01 13:28:06.092: E/AndroidRuntime(1528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-01 13:28:06.092: E/AndroidRuntime(1528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-01 13:28:06.092: E/AndroidRuntime(1528): at dalvik.system.NativeStart.main(Native Method)
08-01 13:28:06.092: E/AndroidRuntime(1528): Caused by: java.lang.NullPointerException: println needs a message
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.util.Log.println_native(Native Method)
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.util.Log.i(Log.java:158)
08-01 13:28:06.092: E/AndroidRuntime(1528): at at.powersoftware.hello.loader.HelloLoaderActivity.onCreateLoader(HelloLoaderActivity.java:73)
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.support.v4.app.LoaderManagerImpl.createLoader(LoaderManager.java:487)
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.support.v4.app.LoaderManagerImpl.createAndInstallLoader(LoaderManager.java:496)
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.support.v4.app.LoaderManagerImpl.initLoader(LoaderManager.java:550)
08-01 13:28:06.092: E/AndroidRuntime(1528): at at.powersoftware.hello.loader.HelloLoaderActivity.onCreate(HelloLoaderActivity.java:30)
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-01 13:28:06.092: E/AndroidRuntime(1528): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-01 13:28:06.092: E/AndroidRuntime(1528): ... 11 more
#Jens: you're so right. In my code I had the line:
Log.i("HLA", cl.getSelection());
(i cutted out all my Log stmts for better readability)
Of course, if selection is null, cl.getSelection() also returns null.
I searched everywhere but not there ...
So many thanks to you all!
#StenaviN: We saw that posting the whole code seems to be a good idea!