-1

过去两天我面临一个问题,但没有任何帮助。请有人看看我的代码,分析它并指出我的错误。(我是 Android BTW 的初学者)。

错误日志:

12-28 16:09:02.087: E/AndroidRuntime(366): FATAL EXCEPTION: main
12-28 16:09:02.087: E/AndroidRuntime(366): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{admin.HazuriNet.hukam/admin.HazuriNet.hukam.HazuriNet_AdminActivity}: java.lang.NullPointerException
12-28 16:09:02.087: E/AndroidRuntime(366):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
12-28 16:09:02.087: E/AndroidRuntime(366):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-28 16:09:02.087: E/AndroidRuntime(366):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-28 16:09:02.087: E/AndroidRuntime(366):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-28 16:09:02.087: E/AndroidRuntime(366):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 16:09:02.087: E/AndroidRuntime(366):  at android.os.Looper.loop(Looper.java:123)
12-28 16:09:02.087: E/AndroidRuntime(366):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-28 16:09:02.087: E/AndroidRuntime(366):  at java.lang.reflect.Method.invokeNative(Native Method)
12-28 16:09:02.087: E/AndroidRuntime(366):  at java.lang.reflect.Method.invoke(Method.java:521)
12-28 16:09:02.087: E/AndroidRuntime(366):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-28 16:09:02.087: E/AndroidRuntime(366):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-28 16:09:02.087: E/AndroidRuntime(366):  at dalvik.system.NativeStart.main(Native Method)
12-28 16:09:02.087: E/AndroidRuntime(366): Caused by: java.lang.NullPointerException
12-28 16:09:02.087: E/AndroidRuntime(366):  at android.app.Activity.findViewById(Activity.java:1637)
12-28 16:09:02.087: E/AndroidRuntime(366):  at admin.HazuriNet.hukam.HazuriNet_AdminActivity.<init>(HazuriNet_AdminActivity.java:81)
12-28 16:09:02.087: E/AndroidRuntime(366):  at java.lang.Class.newInstanceImpl(Native Method)
12-28 16:09:02.087: E/AndroidRuntime(366):  at java.lang.Class.newInstance(Class.java:1429)
12-28 16:09:02.087: E/AndroidRuntime(366):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-28 16:09:02.087: E/AndroidRuntime(366):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
12-28 16:09:02.087: E/AndroidRuntime(366):  ... 11 more

和活动:

package admin.HazuriNet.hukam;

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

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class HazuriNet_AdminActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                postData();

            }

        });
    }

    public void postData() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.hazurinet.com/set/hukum_class.php");

        //This is the data to send
        String UserName = txtUserName.getText().toString(); //any data to send
        String Password = txtPassword.getText().toString();

        try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("app", "zizzle"));
        nameValuePairs.add(new BasicNameValuePair("un", UserName));
        nameValuePairs.add(new BasicNameValuePair("pwd", Password));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler);

        //This is the response from a php application
        lblResponce.setText(response);

        } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        } catch (IOException e) {
        // TODO Auto-generated catch block
        }

        }//end postData()
    /** Declarations */
    EditText txtUserName = (EditText) findViewById(R.id.txtUserName);
    EditText txtPassword = (EditText) findViewById(R.id.txtPassword);
    Button btnLogin = (Button) findViewById(R.id.btnLogin);
    TextView lblResponce = (TextView) findViewById(R.id.lblResponce);
}

和清单:

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

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" android:permission="android.permission.INTERNET">
        <activity
            android:name=".HazuriNet_AdminActivity"
            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>

如果您想了解更多信息,请告诉我...提前致谢...

4

5 回答 5

0

您首先使用 findViewById 进行初始化btnLogin,然后再使用它。

Button btnLogin = (Button)findViewById(R.id.yourButtonId);

编辑:-在 onCreate() 方法中声明您的 EditText、Button 和 Textview,如下面的代码。

 public class HazuriNet_AdminActivity extends Activity 
 {
    EditText txtUserName,txtPassword;
    Button btnLogin ;
    TextView lblResponce;

   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       txtUserName = (EditText) findViewById(R.id.txtUserName);
       txtPassword = (EditText) findViewById(R.id.txtPassword);
       btnLogin = (Button) findViewById(R.id.btnLogin);
       lblResponce = (TextView) findViewById(R.id.lblResponce);
       btnLogin.setOnClickListener(new OnClickListener() 
       {
           @Override
           public void onClick(View v) 
           {
               postData();
           }
       });
   }
       // Remaining Code
}
于 2012-12-28T10:55:38.100 回答
0

问题出在button. 在btnLogin调用之前初始化按钮onClickListeneronCreate()

现在您正在初始化PostData()调用时未调用btnLogin.onClickListener()的按钮

public class HazuriNet_AdminActivity extends Activity {

EditText txtUserName;
EditText txtPassword;
Button btnLogin;
TextView lblResponce;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    /** Declarations */
    txtUserName = (EditText) findViewById(R.id.txtUserName);
    txtPassword = (EditText) findViewById(R.id.txtPassword);
    btnLogin    = (Button) findViewById(R.id.btnLogin);
    lblResponce = (TextView) findViewById(R.id.lblResponce);

        btnLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                postData();

            }

        });
    }
}
于 2012-12-28T10:56:14.420 回答
0

做这个声明:

/** Declarations */
    EditText txtUserName = (EditText) findViewById(R.id.txtUserName);
    EditText txtPassword = (EditText) findViewById(R.id.txtPassword);
    Button btnLogin = (Button) findViewById(R.id.btnLogin);
    TextView lblResponce = (TextView) findViewById(R.id.lblResponce);

刚过

setContentView(R.layout.main);

谢谢。

于 2012-12-28T10:56:55.893 回答
0

你没有btnLogin像这样创建按钮,按钮 btnLogin = (Button)findviewbyid(R.id.btnLogin);

于 2012-12-28T10:57:18.637 回答
0

setContentView(R.layout.main);在您的活动方法中写入下面的代码行onCreate()并从postdata()方法中删除。

EditText txtUserName = (EditText) findViewById(R.id.txtUserName);
EditText txtPassword = (EditText) findViewById(R.id.txtPassword);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
TextView lblResponce = (TextView) findViewById(R.id.lblResponce);

它会解决你的问题。

于 2012-12-28T11:04:34.923 回答