2014-12-27 174 views
0

获取错误“无法启动myWeb *(我的应用程序的名称)*” 任何人都可以帮我吗? 我是一个新手,刚开始编码android。应用程序无法启动Android

我在eclipse上运行这个应用程序,当它从ADT启动时它给出了错误!

的AndroidManifest.xml

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

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

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

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@style/AppTheme" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" 
    android:id="@+id/mainLayout" 
    > 

    <EditText 
     android:id="@+id/email" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="105dp" 
     android:ems="10" 
     android:textColor="#00FF00" 
     android:typeface="monospace" 
     android:hint="Email Address or Phone Number" /> 

    <EditText 
     android:id="@+id/password" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/password" 
     android:layout_centerVertical="true" 
     android:ems="10" 
     android:textColor="#00FF00" 
     android:typeface="monospace" 
     android:hint="Password" 
     android:inputType="textPassword"/> 

    <Button 
     android:id="@+id/login1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/password" 
     android:layout_below="@+id/password" 
     android:layout_marginTop="18dp" 
     android:minWidth="180dip" 
     android:text="Log In" /> 

    <ProgressBar 
     android:id="@+id/progressBar1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/login1" 
     android:layout_toRightOf="@+id/login1" 
     android:visibility="invisible" /> 

</RelativeLayout> 

MainActivity.java

package com.example.myweb; 

import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.List; 


import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 

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

public class MainActivity extends Activity { 

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

     ImageButton button = (ImageButton) findViewById(R.id.login1); 
     EditText emailBox = (EditText)findViewById(R.id.email); 
     EditText passwordBox = (EditText)findViewById(R.id.password); 
     final String emailId = emailBox.getText().toString(); 
     final String passwordId = passwordBox.getText().toString(); 
     button.setOnClickListener(new View.OnClickListener() {    
      public void onClick(View v) {    
      try { 
       getUserLoggedIn(emailId,passwordId); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace();      
      }    
      } 
     });   
    } 

    @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 getUserLoggedIn(String email,String password) throws ClientProtocolException, IOException{ 

     HttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost("localhost/testand.php"); 

     List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
     pairs.add(new BasicNameValuePair("email", email)); 
     pairs.add(new BasicNameValuePair("password", password)); 
     post.setEntity(new UrlEncodedFormEntity(pairs)); 
     HttpResponse response = client.execute(post); 

    } 
} 
+0

您应该使用'AsyncTask'进行网页调用。如果您在模拟器中使用应用程序,那么使用'10.0.2.2'而不是'localhost'什么是AsyncTask? – Bills 2014-12-27 18:30:40

+0

什么是AsyncTask?以及如何使用它@Bill – 2014-12-27 18:34:21

+0

它在UI线程之外异步执行任务,因此不会阻止该线程获得快速的用户体验。 http://developer.android.com/reference/android/os/AsyncTask.html – mraviator 2014-12-27 18:37:52

回答

0

ù SE的AsyncTask像下面并调用它onCreate

private class DownloadFilesTask extends AsyncTask<emailId, passwordId> { 
    protected Long doInBackground(URL... urls) { 
     getUserLoggedIn(emailId,passwordId); 
     return totalSize; 
    } 

    protected void onPostExecute(Long result) { 
     Log.v("Webpage","Done"); 
    } 
} 

onCreate

  try { 
       new DownloadFilesTask().execute(emailId,passwordId); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace();      
      } 
+0

不能正常工作.......... – 2014-12-27 19:14:09

0

致电解决的课题![解决这里]

其实这是解决...

activity_main.xml我用<button>标签,但在.java我用ImageButton来调用它的ID。

虽然傻我解决它在Java文件使用Button类,而不是ImageButton

改变

ImageButton button = (ImageButton) findViewById(R.id.login1);

Button button = (Button) findViewById(R.id.login1);

此修复程序才特定于我的代码。因此,如果有人像这样得到错误,那么就应该重新检查代码,而不是随便问问题。

+0

请同时使用查看android日志文件来检出错误和运行时异常,最好还是附上你的android catlog以及:) – Bills 2014-12-30 07:48:06