<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kartiggessan.srmp1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity" />
<activity android:name=".Admin" />
<activity android:name=".Kemaskini" />
<activity android:name=".Padam" />
<activity android:name=".Papar" />
<activity android:name=".Tambah" />
<activity android:name=".Daftar"></activity>
</application>
这是Android Manifest文件
package com.example.kartiggessan.srmp1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btnpensyarah,btnadmin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnpensyarah = (Button) findViewById(R.id.btnpensyarah);
btnadmin = (Button) findViewById(R.id.btnadmin);
btnpensyarah.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent pensyarah = new Intent(MainActivity.this,LoginActivity.class);
startActivity(pensyarah);
}
});
btnadmin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent admin = new Intent(MainActivity.this, Admin.class);
startActivity(admin);
}
});
}
这是MainActivity.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.kartiggessan.srmp1.MainActivity">
<Button
android:text="admin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnadmin"
android:layout_alignParentBottom="true"
android:layout_alignEnd="@+id/btnpensyarah" />
<Button
android:text="Pensyarah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnpensyarah"
android:layout_above="@+id/btnadmin"
android:layout_centerHorizontal="true"
android:layout_marginBottom="88dp" />
</RelativeLayout>
这是activity_main.xml。
10-08 10:22:56.739 7018-7018/com.example.kartiggessan.srmp1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kartiggessan.srmp1, PID: 7018
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kartiggessan.srmp1/com.example.kartiggessan.srmp1.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.example.kartiggessan.srmp1.Session.loggedin()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.example.kartiggessan.srmp1.Session.loggedin()' on a null object reference
at com.example.kartiggessan.srmp1.LoginActivity.onCreate(LoginActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是创建的日志
public static final String TAG = DbHelper.class.getSimpleName();
如何更改此代码
package com.example.kartiggessan.srmp1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnLogin,btnDaftar;
private EditText etNamaPensyarah,etKatalaluan;
private DbHelper db;
private Session session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin = (Button)findViewById(R.id.btnLogin);
btnDaftar = (Button)findViewById(R.id.btnDaftar);
etNamaPensyarah = (EditText)findViewById(R.id.etNamaPensyarah);
etKatalaluan = (EditText)findViewById(R.id.etKataLaluan);
btnLogin.setOnClickListener(this);
btnDaftar.setOnClickListener(this);
if(session.loggedin()){
startActivity(new Intent(LoginActivity.this,Menu.class));
finish();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnLogin:
Intent login = new Intent(LoginActivity.this, Menu.class);
startActivity(login);
break;
case R.id.btnDaftar:
Intent daftar = new Intent(LoginActivity.this, Daftar.class);
startActivity(daftar);
break;
default:
}
}
private void login(){
String namapensyarah = etNamaPensyarah.getText().toString();
String katalaluan = etKatalaluan.getText().toString();
if(db.getUser(namapensyarah,katalaluan)) {
session.setLoggedin(true);
startActivity(new Intent(LoginActivity.this, Menu.class));
finish();
}else{
Toast.makeText(getApplicationContext(), "Nama pensyarah dan KataLaluan adalah Salah",Toast.LENGTH_SHORT).show();
}
}
}
这是login.java
问题是,当我点击按钮pensyarah时,应用程序不幸停止了。 请解决它并立即评论。 你的评论非常适用。