FTP上传 - 应用程序崩溃

时间:2014-05-10 14:26:52

标签: android xml file-upload upload ftp

我正在尝试通过Android手机(Sony Xperia SP)上传文件。

我使用了以下代码:

的活动:

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



}

布局:

<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>

清单:

<?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>

问题在于,当我按下按钮时,应用程序会崩溃,但它应该上传文件。

有人可以帮我吗?

logcat说:

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

2 个答案:

答案 0 :(得分:0)

用作android:onClick属性的方法必须在其签名中包含View个参数。变化

public void ente()

public void ente(View v)

答案 1 :(得分:0)

请查看您的代码并考虑onCreate()方法中是否与public void ente()有任何连接,因此请在onCreate()中的某处调用该方法并传递public void ente(View v)内的视图{1}}。这应该工作。

更新:

如果您将来获得NetworkOnThreadMainException,请在onCreate()方法上添加以下两行。

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

但是请考虑将AsyncTask用于网络相关问题,因为它不应该在UI线程中完成。

更新2:(由于评论部分中的额外问题)

您需要在public void ente()上调用onCreate()方法,如下所示:

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

    }

请尝试使用上面的代码,我不是专家,因为我不确定您是否必须将View作为参数传递给ente()函数。如果有效,请尝试留下您的评论。