gpt4 book ai didi

android - 无法启动 Activity ComponentInfo : NullPointerException

转载 作者:搜寻专家 更新时间:2023-11-01 09:05:38 25 4
gpt4 key购买 nike

我不是 android 开发的新手,但我无法弄清楚为什么会出现这个愚蠢的问题。我所做的只是在应用程序启动时启动一个 Activity ,它崩溃并给出 nullpointerException。请在下面检查我的代码。

可能是最后办公时间让我失明了。

日志:

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

登录.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);

}
});

}



}

list :

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

登录.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>

最佳答案

你还没有实例化loginContext

loginContext = this;

之前

obj = new TestAdapter(loginContext);

关于android - 无法启动 Activity ComponentInfo : NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12092149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com