2013-10-20 42 views
0

我正在做一个可以上传文件的Apps功能。Android布局问题 - 不显示任何东西

结束后,所有的调试和启动程序

的logcat的没有显示有关错误什么。

但它只是一个空白的白页。

请帮忙,我不知道发生了什么事。

java代码:

package com.example.adc; 

     import java.io.File; 
     import java.nio.charset.Charset; 
     import java.util.ArrayList; 
     import java.util.List; 

     import org.apache.http.HttpResponse; 
     import org.apache.http.NameValuePair; 
     import org.apache.http.client.HttpClient; 
     import org.apache.http.client.ResponseHandler; 
     import org.apache.http.client.methods.HttpPost; 
     import org.apache.http.entity.mime.HttpMultipartMode; 
     import org.apache.http.entity.mime.MultipartEntity; 
     import org.apache.http.entity.mime.content.ByteArrayBody; 
     import org.apache.http.entity.mime.content.StringBody; 
     import org.apache.http.impl.client.DefaultHttpClient; 

     import org.apache.http.entity.mime.content.FileBody; 
     import org.apache.http.entity.mime.content.StringBody; 
     import org.apache.http.impl.client.BasicResponseHandler; 
     import org.apache.http.impl.client.DefaultHttpClient; 
     import org.apache.http.message.BasicNameValuePair; 
     import org.apache.http.protocol.HTTP; 

     import android.os.Bundle; 
     import android.app.Activity; 
     import android.view.Menu; 
     import android.view.View; 
     import android.widget.EditText; 
     import android.widget.Toast; 

     public class MainActivity extends Activity { 

private void doMultiPost(String url, List<NameValuePair> params){ 
    HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost(url); 
    try{ 
     //setup multipart entity 
     MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 

     for(int i=0;i< params.size();i++){ 
     //identify param type by Key 
     if(params.get(i).getName().equals("file")){ 
     File f=new File(params.get(i).getValue()); 
     FileBody fileBody=new FileBody(f); 
     entity.addPart("image"+i,fileBody); 
     }else{ 
     entity.addPart(params.get(i).getName(),new StringBody(params.get(i).getValue(), Charset.forName("UTF-8"))); 
     } 
     } 

     post.setEntity(entity); 

     //create response handler 
     ResponseHandler <String> handler=new BasicResponseHandler(); 
     //execute and get response 
     String response=new String(client.execute(post,handler).getBytes(),HTTP.UTF_8); 
     Toast.makeText(this, response,Toast.LENGTH_SHORT).show(); 
     }catch(Exception e){ 
     e.printStackTrace(); 
     }  
    } 

public void onSubmit(View v){ 
    String path = ((EditText)findViewById(R.id.filePath)).getText().toString(); 
    String des = ((EditText)findViewById(R.id.desTxt)).getText().toString(); 

    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    params.add(new BasicNameValuePair("file",path)); 
    params.add(new BasicNameValuePair("des",des)); 

    doMultiPost("10.0.0.2/MultipartPost.php",params); 
    } 
    } 

清单:

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


<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.READ_OWNER_DATA" /> 
<uses-permission android:name="android.permission.WRITE_OWNER_DATA" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

<uses-sdk 
    android:minSdkVersion="10" 
    android:targetSdkVersion="10" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.adc.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> 

布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:gravity="center_horizontal" 
    > 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

<TextView 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:text="file Description:"/> 

<EditText 
    android:id="@+id/desTxt" 
    android:layout_width="160dp" 
    android:layout_height="wrap_content" 
/> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
<TextView 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:text="File path:"/> 

<EditText 
    android:id="@+id/filePath" 
    android:layout_width="160dp" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

<Button 
    android:id="@+id/submitBtn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Submit" 
    android:onClick="onSubmit"  
/> 

</LinearLayout> 
+2

哪里是活动里面的'的onCreate()'方法? –

+1

没有'onCreate()'。 LOL ... – johntheripp3r

+0

'onCreate()'是唯一相关的方法。请张贴该方法。 – nhgrif

回答

1

在您的活动添加该代码..

@Override 
public void onCreate(Bundle icicle) 
{ 
    super.onCreate(icicle); 
    setContentView(R.layout.Your_layout); 
} 

瞧...

+0

它的作品! 谢谢你的帮助, – user2900491

+0

@ user2900491很明显。我根本没有批评,并且记住它会从错误中吸取教训。不客气。 – johntheripp3r

+0

问题已修复,但不能上传文件:( ,但是我会继续练习 – user2900491