我对android开发并不陌生,但我无法弄清楚为什么会出现这个愚蠢的问题。我正在做的就是在应用程序启动时启动一个活动,它崩溃了nullpointerException
。请检查下面的代码。
可能是上班时间让我失明。
Logcat:
08-23 18:19:19.359: E/AndroidRuntime(798): FATAL EXCEPTION: main
08-23 18:19:19.359: E/AndroidRuntime(798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.click4tab.orderformnew/com.click4tab.orderformnew.Login}: java.lang.NullPointerException
Login.java:
public class Login extends Activity {
Button loginButton;
EditText id, password;
String sID, sPassword;
TestAdapter obj;
Context loginContext;
public static int loginSuccessful;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//Initialising variables
loginButton = (Button)findViewById(R.id.button1);
id = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
obj = new TestAdapter(loginContext);
sID = id.getText().toString();
sPassword = password.getText().toString();
StringBuffer buf = new StringBuffer();
buf.append(sID + ":::" + sPassword);
TestAdapter.params = new ArrayList<NameValuePair>();
TestAdapter.params.add(new BasicNameValuePair("tag", "login"));
TestAdapter.params.add( new BasicNameValuePair("query", buf.toString()));
loginButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
//send login information to authenticate user
obj.new Read().execute("");
// Intent i = new Intent("mainActivity");
// startActivity(i);
}
});
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.click4tab.orderformnew"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="mainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Login"
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>
login.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="194dp"
android:text="ID"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignLeft="@+id/button1"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginTop="29dp"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginLeft="19dp"
android:layout_marginTop="16dp"
android:text="Login" />
</RelativeLayout>
答案 0 :(得分:3)
您尚未实例化loginContext
待办事项
loginContext = this;
前
obj = new TestAdapter(loginContext);