0

I am trying to upload a file via my Android phone (Sony Xperia SP).

I used the follwing code:

Activity:

package com.jebinga.ftpUpload;

import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    @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;
    }


    public void ente(){

        FTPClient con = null;


        try
        {
            con = new FTPClient();
            con.connect("ftp-web.funpic.de");

            if (con.login("MyUsername", "MyPassword"))
            {
                con.enterLocalPassiveMode();
                con.setFileType(FTP.BINARY_FILE_TYPE);
                String data = "/storage/sdcard0/testordner/testdatei.txt";

                FileInputStream in = new FileInputStream(new File(data));
                boolean result = con.storeFile("/testdatei.txt", in);
                in.close();
                if (result) Log.v("upload result", "succeeded");
                con.logout();
                con.disconnect();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }






    }





    @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);
    }



}

Layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jebinga.ftpUpload.MainActivity"
    tools:ignore="MergeRootFrame" >

    <Button
        android:id="@+id/button1"
        android:layout_width="170dp"
        android:layout_height="134dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:onClick="ente"
        android:text="Button" />

</FrameLayout>

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jebinga.ftpUpload"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.jebinga.ftpUpload.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The problem is that when i press the button the app crashes though it should upload the file.

Can anyone help me please?

The logcat says:

05-10 16:44:18.783: W/dalvikvm(707): threadid=1: thread exiting with uncaught exception (group=0x41749450)

05-10 16:44:18.813: E/AndroidRuntime(707): FATAL EXCEPTION: main

05-10 16:44:18.813: E/AndroidRuntime(707): java.lang.IllegalStateException: Could not find a method ente(View) in the activity class com.jebinga.ftpUpload.MainActivity for onClick handler on view class android.widget.Button with id 'button1'

05-10 16:44:18.813: E/AndroidRuntime(707): java.lang.IllegalStateException: Could not find a method ente(View) in the activity class com.jebinga.ftpUpload.MainActivity for onClick handler on view class android.widget.Button with id 'button1'

05-10 16:44:18.813: E/AndroidRuntime(707):  at android.view.View$1.onClick(View.java:3658)

05-10 16:44:18.813: E/AndroidRuntime(707):  at android.view.View.performClick(View.java:4171)

05-10 16:44:18.813: E/AndroidRuntime(707):  at android.view.View$PerformClick.run(View.java:17186)

05-10 16:44:18.813: E/AndroidRuntime(707):  at android.os.Handler.handleCallback(Handler.java:615)

05-10 16:44:18.813: E/AndroidRuntime(707):  at android.os.Handler.dispatchMessage(Handler.java:92)

05-10 16:44:18.813: E/AndroidRuntime(707):  at android.os.Looper.loop(Looper.java:213)

05-10 16:44:18.813: E/AndroidRuntime(707):  at android.app.ActivityThread.main(ActivityThread.java:4793)

05-10 16:44:18.813: E/AndroidRuntime(707):  at java.lang.reflect.Method.invokeNative(Native Method)

05-10 16:44:18.813: E/AndroidRuntime(707):  at java.lang.reflect.Method.invoke(Method.java:511)

05-10 16:44:18.813: E/AndroidRuntime(707):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)

05-10 16:44:18.813: E/AndroidRuntime(707):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)

05-10 16:44:18.813: E/AndroidRuntime(707):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)

05-10 16:44:18.813: E/AndroidRuntime(707):  at dalvik.system.NativeStart.main(Native Method)

05-10 16:44:18.813: E/AndroidRuntime(707): Caused by: java.lang.NoSuchMethodException: ente [class android.view.View]

05-10 16:44:18.813: E/AndroidRuntime(707):  at java.lang.Class.getConstructorOrMethod(Class.java:460)

05-10 16:44:18.813: E/AndroidRuntime(707):  at java.lang.Class.getMethod(Class.java:915)

05-10 16:44:18.813: E/AndroidRuntime(707):  at android.view.View$1.onClick(View.java:3651)

05-10 16:44:18.813: E/AndroidRuntime(707):  ... 11 more
A.S.
  • 4,574
  • 3
  • 26
  • 43
jebinga
  • 3
  • 3
  • What does the Logcat give you? Does it say `NetworkOnMainThreadException` if so you have to sart the ftpclient from a seperate thread using asynctask – A.S. May 10 '14 at 14:37

2 Answers2

0

Methods used as android:onClick attributes must have a View parameter in their signature. Change

public void ente()

to

public void ente(View v)
laalto
  • 150,114
  • 66
  • 286
  • 303
0

Please look at your code and think whether you have any connectivity in the onCreate() method with public void ente() so please do call the method somewhere in the onCreate() and pass the view inside the public void ente(View v) . That should work.

UPDATE :

If you get NetworkOnThreadMainException in the future, please add the following two lines on the onCreate() method.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

But please do consider using AsyncTask for Network related issues as it should not be done in UI threads.

UPDATE 2: (Due to extra question in the comment section)

You are required to call the public void ente() method at the onCreate() like the following :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View v = findViewById(R.layout.activity_main);   
        ente(v);

    }

Please try using the above code, I am no expert myself cause I am not sure whether you have to pass View as your parameter to the ente() function. Please try and leave your comments if it worked.

San
  • 2,078
  • 1
  • 24
  • 42
  • I inserted those two lines, but if u say i should call the method does that mean i should write "ente(null);" into the onCreate()? Because if i do so it crashes when I open the app? What did I do wrong? – jebinga May 10 '14 at 15:19