我是新來的android開發,並嘗試創建一個簡單的註冊應用程序,但每次點擊Register Here TextView時應用程序崩潰。除此之外,我編寫登錄按鈕來嘗試調用另一個活動,但點擊按鈕後它沒有響應。我很努力地想找到錯誤,但我不確定在編碼中錯過了什麼。java.lang.RuntimeException:無法啓動活動ComponentInfo和在Android Studio中調用另一個活動
這是主要的活動
package com.fyp4201.universityguide;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
public class MainActivity extends AppCompatActivity {
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabHost host = (TabHost) findViewById(R.id.tabHost);
host.setup();
//Tab 1
TabHost.TabSpec spec = host.newTabSpec("tab1");
spec.setContent(R.id.tab1);
spec.setIndicator("Home");
host.addTab(spec);
//Tab 2
spec = host.newTabSpec("tab2");
spec.setContent(R.id.tab2);
spec.setIndicator("Search Courses");
host.addTab(spec);
//Tab 3
spec = host.newTabSpec("tab3");
spec.setContent(R.id.tab3);
spec.setIndicator("Search University");
host.addTab(spec);
//Tab4
spec = host.newTabSpec("tab4");
spec.setContent(R.id.tab4);
spec.setIndicator("Compare Universities");
host.addTab(spec);
//Tab5
spec = host.newTabSpec("tab5");
spec.setContent(R.id.tab5);
spec.setIndicator("Ranking of Universities");
host.addTab(spec);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch(item.getItemId()){
//noinspection SimplifiableIfStatement
case R.id.action_settings: {
return true;
}
case R.id.action_login:{
Intent intent= new Intent(getApplicationContext(),LoginActivity.class);
startActivity(intent);
}
default:
return super.onOptionsItemSelected(item);
}
}}
這是登錄活動代碼
package com.fyp4201.universityguide;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView regis = (TextView) findViewById(R.id.register);
Button login = (Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Homeafterlogin.class);
LoginActivity.this.startActivity(intent);
}
});
regis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, Register.class);
LoginActivity.this.startActivity(intent);
}
});
}}
這是註冊活動代碼
package com.fyp4201.universityguide;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Map;
public class Register extends AppCompatActivity {
private static final String REGISTER_URL = "http://jrfyp4201.site88.net/volleyRegister.php";
public static final String KEY_USERNAME = "username";
public static final String KEY_NAME = "name";
public static final String KEY_PASSWORD = "password";
public static final String KEY_EMAIL = "email";
public static final String KEY_CONTACTNO = "contactno";
private EditText etname,etuname,etpass,etconpass,etemail,etcontactno;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
etname= (EditText) findViewById(R.id.name);
etuname= (EditText) findViewById(R.id.uname);
etpass= (EditText) findViewById(R.id.pass);
etconpass= (EditText) findViewById(R.id.conpass);
etemail= (EditText) findViewById(R.id.email);
etcontactno= (EditText) findViewById(R.id.contactno);
}
public void onRegisterbtnClick(View view){
registerUser();
}
public void registerUser(){
final String username = etuname.getText().toString().trim();
final String password = etpass.getText().toString().trim();
final String name = etname.getText().toString().trim();
final String email = etemail.getText().toString().trim();
final String contactno = etcontactno.toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(Register.this,response,Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Register.this,error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_USERNAME,username);
params.put(KEY_PASSWORD,password);
params.put(KEY_NAME, name);
params.put(KEY_EMAIL, email);
params.put(KEY_CONTACTNO, contactno);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
這是logcat的錯誤文件
Process: com.fyp4201.universityguide, PID: 20646
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyp4201.universityguide/com.fyp4201.universityguide.Register}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2264)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
at android.app.ActivityThread.access$1100(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.fyp4201.universityguide.Register.onCreate(Register.java:20)
at android.app.Activity.performCreate(Activity.java:5340)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2228)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2313)
at android.app.ActivityThread.access$1100(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711)
at dalvik.system.NativeStart.main(Native Method)
個
寄存器活動的XML文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.fyp4201.universityguide.Register"
tools:showIn="@layout/activity_register">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name :"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editname"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username :"
android:id="@+id/textView3" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edituname"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password :"
android:id="@+id/textView4" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editpass"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confirm Password :"
android:id="@+id/textView7" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editconpass"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-mail address :"
android:id="@+id/textView5" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editemail"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact Number :"
android:id="@+id/textView6" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editcontactno"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:id="@+id/registerbtn"
android:layout_gravity="center_horizontal"
android:onClick="onRegisterbtnClick" />
</LinearLayout>
登錄活動的XML文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.fyp4201.universityguide.LoginActivity"
tools:showIn="@layout/activity_login2">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="@+id/textView1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/loginuname" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="@+id/textView" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/loginpass" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/login"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:focusableInTouchMode="false"
android:onClick="onLoginClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Register Here!"
android:id="@+id/register"
android:layout_gravity="center_horizontal"
android:textColor="@color/accent_material_light"
android:clickable="true"
android:onClick="onRegisterClick" />
</LinearLayout>
請告訴我什麼,我錯過了。謝謝。
感謝您回覆我的問題。我檢查了XML文件的ID,但在我看來沒有錯。所有的ID都是獨一無二的,指的是正確的活動,或者我錯過了一些東西。我應該在這裏上傳XML文件嗎? – ishidaxkun