-1

这是我的代码 LoginActivity.java 文件

 package com.example.crims;
 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Intent;
 import android.view.View;
 import android.widget.TextView;

 public class LoginActivity extends Activity {

TextView screen;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);
    screen = (TextView) findViewById(R.id.link_to_register);
     // Listening to register new account link
    screen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Switching to Register screen
            Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
            startActivity(i);
        }
    });
 }
}

这是我的“activity_login.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: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=".LoginActivity" >

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>`

最后这是我的清单文件

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.crims.LoginActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".RegisterActivity"
              android:label="Register New Account">

    </activity>
</application>

</manifest>  

所有这些文件都在这里,我想找到我的错误,因为我是 android 应用程序开发的新手......

4

2 回答 2

2

NullPointerException在这里有(NPE):

10-01 21:02:07.580: E/AndroidRuntime(1299):
   at com.example.crims.LoginActivity.onCreate(LoginActivity.java:17)

因此,检查 LoginActivity.java 的第 17 行(您只需在 logcat 视图中双击此消息,您将被导航到此行)。

看来,第 17 行是这样的:

registerScreen.setOnClickListener(new View.OnClickListener() {

因为这是 NPE,registerScreen所以是null. 因此,您应该检查它为什么为空。这是因为 Activity 在上面的行中找不到它,而是返回null

TextView registerScreen = (TextView) findViewById(R.id.link_to_register);

这可能是以下两种可能性之一:要么您在 activity_login.xml 中没有 ID 为“link_to_register”的小部件,要么是其他错误:)

请检查一下并出示您的activity_login.xml文件。

于 2013-10-02T06:50:27.077 回答
0

听兄弟拿这个代码。根据你的改变。

public class MainActivity extends Activity {

TextView screen;

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



    screen = (TextView) findViewById(R.id.screen);

            // Listening to register new account link
           screen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // Switching to Register screen
                Intent i = new Intent(getApplicationContext(), Register.class);
                startActivity(i);
            }
        });
    } 


}

如果您不知道,您也需要在 manifest.xml 文件中注册您的活动。

于 2013-10-02T07:09:33.403 回答