Reputation: 51
I am a bit of a novice at writing applications and I'm working on a school project. I've just about finished the program, just debugging now. I'm having a major issue that can't seem to get around involving calling a method on the click of a button. The program is intended to download a file after the user types in a search term.
Originally, my logcat was telling me the method could not be found in the HelloTabWidget class. I originally had the findSong method in the SearchActivity class, so I moved pretty much all of the code from SearchActivity into HelloTabWidget, since that seemed like an easy way to see everything anyway. Now, I've tried the tip on here of changing the path in the xml fro onClick but that didn't fix it either. Rather, I got the past error from before: "could not find method com.android.iPirate.HelloTabWidget.findSong in activity HelloTabWidget..." I'm beginning to think it has something to do with the added exception in the method. But I'm virtually clueless as to what can be done to fix this. Any help would be appreciated.
Here's HelloTabWidget:
package com.android.iPirate;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
>import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
public class HelloTabWidget extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, SearchActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
// Do the same for the other tabs
intent = new Intent().setClass(this, SongsActivity.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_artists))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, SettingsActivity.class);
spec = tabHost.newTabSpec("settings").setIndicator("Instructions",
res.getDrawable(R.drawable.ic_tab_settings))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2); }
public String searchTerm;
public String quotes = "";
public String title;
public EditText edittext;
public String youTubeURL;
public Set<String> names = new HashSet<String>();
public void findSong(View view) throws Exception {
edittext = (EditText) findViewById(R.id.edittext);
searchTerm = edittext.getText().toString();
String address;
address = getURL(searchTerm);
UrlDownload.fileUrl(address, title + ".mp3", "/");
}
public void findPlaylist(View view) throws Exception{
findSong(view);
String[] pageNames = getLinks(youTubeURL);
for(String name : pageNames)
if (!names.contains(name))
UrlDownload.fileUrl(getURL(name), title + ".mp3", "/");
}
public static String[] getLinks(String link) throws Exception {
URL url = new URL(link);
BufferedReader in = new BufferedReader(
new InputStreamReader(
url.openStream()));
Scanner input = new Scanner(in);
String[] songs = new String[15];
for(int i = 0; i < 15; i++)
songs[i] = nextTitle(input);
return songs;
}
public static String nextTitle(Scanner input){
while (input.hasNextLine()){
String inputLine = input.nextLine();
if (inputLine.contains("<span dir=\"ltr\" class=\"title\" title=\"")){
return inputLine.substring
(inputLine.indexOf("<span dir=\"ltr\" class=\"title\" title=\"") + 37, inputLine.indexOf(">")-1);
}
}
return null;
}
public String getURL(String searchString) throws Exception {
String searchQuery = searchString.replace(' ','+');
String siteURL = new String("http://www.youtube.com/results?search_query="+searchQuery+ "&aq=f\"");
URL url;
url = new URL(siteURL);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
Scanner input = new Scanner(in);
String videoLink = "";
String fLink = null;
while(input.hasNextLine()){
String line = input.nextLine();
if(line.contains("<!-- start search results -->")){
for(int i = 0; i <= 44; i++){
line = input.nextLine();
}
youTubeURL = "www.youtube.com/"+line.substring(line.indexOf("<a href=")+8, line.indexOf("class")-2);
videoLink = "http://www.video2mp3.net/index.php?url=" + youTubeURL;
title = line.substring(line.indexOf("title="+6),line.indexOf(quotes,line.indexOf("title")+7)-1);
names.add(title);
break;
}
}
URL vurl = null;
vurl = new URL(videoLink);
BufferedReader in2;
in2 = new BufferedReader(new InputStreamReader(vurl.openStream()));
Scanner input2 = new Scanner(in2);
while(input2.hasNextLine()){
String line = input.nextLine();
if(line.contains("Conversion successfully completed!")){
line = input.nextLine();
line = input.nextLine();
fLink = line.substring(line.indexOf("http"),line.indexOf("/'"));
break;
}
}
URL furl;
furl = new URL(fLink);
BufferedReader in3 = new BufferedReader(new InputStreamReader(furl.openStream()));
Scanner input3 = new Scanner(in3);
while(input3.hasNextLine()){
String line = input.nextLine();
if(line.contains("Wait 20 seconds or click here")){
line = input.nextLine();
String line2 = input.nextLine();
String line3 = input.nextLine();
String line4 = input.nextLine();
if(line2.contains(".mp3"))
return line.substring(line.indexOf("tt"+3)) + line2.substring(0,line2.indexOf(".mp3")+4);
else if(line3.contains(".mp3"))
return line.substring(line.indexOf("tt"+3)) + line2 + line3.substring(0,line3.indexOf(".mp3")+4);
else if(line4.contains(".mp3")){
return line.substring(line.indexOf("tt"+3)) + line2 + line3 + line4.substring(0,line4.indexOf(".mp3")+4);
}
}
}
return "";
}
public void savemp3(String s) throws Exception{
URLConnection conn = new URL(s).openConnection();
InputStream is = conn.getInputStream();
OutputStream outstream = new FileOutputStream(new File(title + ".mp3"));
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
outstream.write(buffer, 0, len);
}
outstream.close();
}
}
Now the XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Find Song"
android:onClick="findSong" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Find Playlist"
android:onClick="findPlaylist" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"/>
</LinearLayout>
</TabHost>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.iPirate"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloTabWidget"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
</activity>
<activity android:name=".SearchActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
</activity>
<activity android:name=".SongsActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
and Logcat:
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): FATAL EXCEPTION: main
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): java.lang.IllegalStateException: Could not execute method of the activity
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at android.view.View$1.onClick(View.java:2072)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at android.view.View.performClick(View.java:2408)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at android.view.View$PerformClick.run(View.java:8816)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at android.os.Handler.handleCallback(Handler.java:587)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at android.os.Looper.loop(Looper.java:123)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at java.lang.reflect.Method.invoke(Method.java:521)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at dalvik.system.NativeStart.main(Native Method)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): Caused by: java.lang.reflect.InvocationTargetException
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at com.android.iPirate.HelloTabWidget.findSong(HelloTabWidget.java:71)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at java.lang.reflect.Method.invoke(Method.java:521)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at android.view.View$1.onClick(View.java:2067)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): ... 11 more
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): Caused by: java.net.SocketException: Permission denied
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at java.net.Socket.checkClosedAndCreate(Socket.java:873)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at java.net.Socket.connect(Socket.java:1020)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1152)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at java.net.URL.openStream(URL.java:653)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): at com.android.iPirate.HelloTabWidget.getURL(HelloTabWidget.java:111)
12-06 14:35:33.603: ERROR/AndroidRuntime(4157): ... 15 more
Thanks for any help.
New logcat:
>12-06 16:34:30.585: ERROR/AndroidRuntime(4870): FATAL EXCEPTION: main
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): java.lang.IllegalStateException: Could not execute method of the activity
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at android.view.View$1.onClick(View.java:2072)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at android.view.View.performClick(View.java:2408)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at android.view.View$PerformClick.run(View.java:8816)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at android.os.Handler.handleCallback(Handler.java:587)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at android.os.Looper.loop(Looper.java:123)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at java.lang.reflect.Method.invoke(Method.java:521)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at dalvik.system.NativeStart.main(Native Method)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): Caused by: java.lang.reflect.InvocationTargetException
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at com.android.iPirate.HelloTabWidget.findSong(HelloTabWidget.java:71)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at java.lang.reflect.Method.invoke(Method.java:521)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at android.view.View$1.onClick(View.java:2067)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): ... 11 more
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): Caused by: java.net.MalformedURLException: Protocol not found:
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at java.net.URL.<init>(URL.java:275)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at java.net.URL.<init>(URL.java:159)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): at com.android.iPirate.HelloTabWidget.getURL(HelloTabWidget.java:129)
12-06 16:34:30.585: ERROR/AndroidRuntime(4870): ... 15 more
And thanks to the people helping me with the quotes. Can't figure them out! :D
Upvotes: 3
Views: 9506
Reputation: 48587
My suggestion is to not set onCLickLIsteners
in XML. Do it in code.
Upvotes: -1
Reputation: 1
I used a TabHost once and I got a very similar error like you. The fault in my case was in my Manifest. I made a spell error, the activity i added to the manifest was written like this: .list instead of .List that's what made me get the error. The only thing what was wrong was just one letter not being upper case :). To find an error, firstly get your code in a try catch block for more control, and this helps you to find out what exceptions exactly is thrown and why. An after that you will probably find your problem.
Upvotes: 0
Reputation: 11
Never try to rename main class when you create from eclipse.
create a new project and add the files. and try to run again.
Upvotes: 1
Reputation: 39605
Judging from the last logcat output it seems like the URL you are passing it is malformed. try to log the URL using Log.d()
and check out what the URL really looks like.
Upvotes: 0
Reputation: 52002
I suspect it's because you're opening a socket, but didn't declare the INTERNET
permission. Try adding this to your manifest outside the <application> tag.
<uses-permission android:name="android.permission.INTERNET"/>
Upvotes: 2
Reputation: 134714
Which line in your HelloTabWidget.java file is line 111? You have a SocketException
being raised in your 'getURL()' method.
Upvotes: 0