CursorLoader throws NullPointerException if argument 'selection' is null - java

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!

Related

MP3 downloader Fatal Exception

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;
}
}
the reason is that the id that you've set to Downloader (or probably the id was never set at all) is wrong, and there is no track with such an id in soundcloud. the track variable cannot be initialized and remains null.
the reason why it worked before is, that time you were using a valid track id

SoundCloud Fatal Exception

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.

Null Pointer Exception while setting value of TextView in an Array Adapter class - Android

I'm getting a NullPointerException while trying to start an Activity which contains a ListView .
In the getView method of the adapter class, the exception happens when the setText function of a textView is being called .
The code bellow is my adapter class:
public class QuestionsListAdapter extends ArrayAdapter<Question> {
Context context;
List<Question> questions;
public QuestionsListAdapter(Context context, List<Question> questions){
super(context, R.layout.list_item_question, questions);
this.context = context;
this.questions = questions;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.list_item_question, null);
Question question = questions.get(position);
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
Log.i(TableCreator.LOG_TAG, question.toString()); //this works fine and the string is not null .
tv.setText(question.toString()+""); //NULL POINTER EXCEPTION .
return view;
}
}
As you see, I've logged the string in the logcat and it works just fine, but the next line makes the mistake .
And this is the logcat output:
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist I/Operation Checklist﹕ |-Question-> id:1 summary:mySummary comment:myComment solution:mySolution ownerList:dummyOwner
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist D/AndroidRuntime﹕ Shutting down VM
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0f5f648)
05-27 13:24:02.979 5325-5325/org.kabiri.operationcheklist E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at org.kabiri.operationchecklist.adapter.QuestionsListAdapter.getView(QuestionsListAdapter.java:43)
at android.widget.AbsListView.obtainView(AbsListView.java:2177)
at android.widget.ListView.makeAndAddView(ListView.java:1840)
at android.widget.ListView.fillDown(ListView.java:675)
at android.widget.ListView.fillFromTop(ListView.java:736)
at android.widget.ListView.layoutChildren(ListView.java:1655)
at android.widget.AbsListView.onLayout(AbsListView.java:2012)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14289)
at android.view.ViewGroup.layout(ViewGroup.java:4562)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
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)
The logcat shows that the error happens on this line of my adapter class:
tv.setText(question.toString()+"");
I really appreciate your help .
You already know where the problem is!
tv.setText(question.toString()+"");
is causing the NPE that means the TextView tv is null. And that means that the line
TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);
is not able to find the TextView. Check the question_list_item_string id and make sure it matches the id in your list_item_question.xml file

Android App crashes right on start

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

If the current browser url contains a word, do something

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.

Categories