2013-10-02 56 views
-1

這是我的代碼LoginActivity.java文件NullPointerException異常而創建活動

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應用程序開發...

+1

你有 TextView registerScreen =(TextView)findViewById(R.id.link_to_register); 在您的佈局。 – URAndroid

+0

@URAndroid在哪裏佈局先生,以及如何在我的佈局中引發它。 – maiz

+0

你應該先閱讀Android開發者的API指南...... http://developer.android.com/guide/topics/appwidgets/index.html下面是關於小部件的內容,但我建議從頭開始閱讀:http: //developer.android.com/guide/components/index.html – Prizoff

回答

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,registerScreennull。所以,你應該檢查它爲什麼是空的。這是因爲活動不能在上面一行找到它,它返回null代替:

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

這可能是兩種可能性:要麼你沒有ID爲「link_to_register」小部件activity_login.xml,或其他是錯誤的:)

請檢查此,請顯示您的activity_login.xml文件。

+0

這裏是我的activity_login.xml文件sir: – maiz

+0

先生,我沒有id爲'link_to-register在activity_login.xml文件中的小部件,我怎麼能把它放入我的xml文件。 – maiz

+0

即時通訊新的stackoverflow,以及如何顯示你我的'activity_login.xml'文件,因爲我不能把它放在評論框 – maiz

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文件的活動,如果你不知道這件事。

+0

我已經註冊了,你可以在我的清單文件中查看它,我現在提供了 – maiz

+0

好的..我編輯了一些代碼。嘗試在新項目中編寫相同的代碼。如果您正在使用eclipse IDE,那麼有時會出現這些問題。交叉檢查你的api級別和你正在運行的模擬器。 – Abb

相關問題