My QRcode scanner function is not working - android-studio

I'm setting up a QRcode scanner function in my TrackActivity. The code seems fine but when I run the apps and click the scan button, nothing happens.
I have tried QRcode scanner function in a new project and it works fine.
This is my TrackActivity.class
package com.example.majugoscreen;
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.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class TrackActivity extends AppCompatActivity {
public static TextView resultTExtview;
Button scan_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_track);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = 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();
resultTExtview = (TextView) findViewById(R.id.result_text);
scan_btn = (Button) findViewById(R.id.btn_scan);
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new
Intent(getApplicationContext(),ScanCodeActivity.class));
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Bundle bundle = getIntent().getExtras();
if(bundle != null){
if(bundle.getString("Track") !=null){
Toast.makeText(getApplicationContext(),
"Activity" + bundle.getString("Track"),
Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
This is my ScanCodeActivity.class
package com.example.majugoscreen;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
class ScanCodeActivity extends AppCompatActivity implements
ZXingScannerView.ResultHandler{
ZXingScannerView ScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScannerView = new ZXingScannerView(this);
setContentView(ScannerView);
}
#Override
public void handleResult(Result result) {
TrackActivity.resultTExtview.setText(result.getText());
onBackPressed();
}
#Override
protected void onPause(){
super.onPause();
ScannerView.stopCamera();
}
#Override
protected void onResume(){
super.onResume();
ScannerView.setResultHandler(this);
ScannerView.startCamera();
}
}
This is my activity_track.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:orientation="vertical"
android:gravity='center'
tools:context=".TrackActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_track" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan QR and Code "
android:layout_gravity="center_horizontal"
android:layout_marginTop="290dp"
android:textSize="16sp"
android:id="#+id/btn_scan"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result"
android:gravity="center"
android:textSize="22sp"
android:layout_marginTop="350dp"
android:id="#+id/result_text"
/>
</android.support.design.widget.CoordinatorLayout>

Related

Android Studio Changing TextView using a Button

I want to change the text in message when the button is clicked to "Hello World!".The idea would be that it would use a method inorder to change the text from a blank TextView into one that has writing on.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="Button"
tools:layout_editor_absoluteX="158dp"
tools:layout_editor_absoluteY="282dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.clickmeapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View message){
message.text="Hello World!";
}
}
Any help in solving this issue would be amazing.
In my code, when you click the button, you will see the "Hello World!" text.
xml
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//add this code
//Button Click
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Set TextView
setMessage("Hello World!");
}
});
}
public void setMessage(String message){
findViewById(R.id.message).setText(message);
}

Button is null object reference (Android Studio)

I have a recyclerview and view holder to list all my data from firestore. Each data will have a delete button to be deleted. But whenever I put the delete button on admin class, it show me void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference. But if i put delete button on view holder class it work. So how can i put the delete button on my admin class?
admin class
package com.example.sossystem;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import org.jetbrains.annotations.NotNull;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class admin extends AppCompatActivity {
Button gotoRegister, deletebutton;
FirebaseFirestore db = FirebaseFirestore.getInstance();
RecyclerView mFirestoreList;
CollectionReference query = db.collection("Users");
//recycler options
FirestoreRecyclerOptions<SosRecords> options;
FirestoreRecyclerAdapter<SosRecords,MyViewHolder>adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
deletebutton = (Button)findViewById(R.id.delete_button);
gotoRegister = findViewById(R.id.gotoRegister);
mFirestoreList = findViewById(R.id.firestore_list);
mFirestoreList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mFirestoreList.setHasFixedSize(true);
//register button click event
gotoRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), RegisterOfficer.class));
}
});
ReadData();
}
private void ReadData() {
options = new FirestoreRecyclerOptions.Builder<SosRecords>().setQuery(query,SosRecords.class).build();
adapter=new FirestoreRecyclerAdapter<SosRecords, MyViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull #NotNull MyViewHolder holder, int position, #NonNull #NotNull SosRecords model) {
holder.email.setText(model.getEmailAddress());
holder.name.setText(model.getFullName());
holder.phone.setText(model.getPhoneNumber());
holder.position.setText(model.getPosition());
deletebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference noteRef = db.collection("Users").document(model.getFirebaseUid());
noteRef.delete().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull #NotNull Task<Void> task) {
if(task.isSuccessful()){
Log.d("TAG","delete"+model.getFirebaseUid());
}
else{
Log.d("TAG","error");
}
}
});
}
});
}
#NonNull
#NotNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull #NotNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_singleofficer,parent,false);
return new MyViewHolder(view);
}
};
adapter.startListening();
mFirestoreList.setAdapter(adapter);
}
public void logoutAdmin(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(), Login.class));
finish();
}
}
Viewholder class
package com.example.sossystem;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;
import org.jetbrains.annotations.NotNull;
class MyViewHolder extends RecyclerView.ViewHolder {
TextView name,phone,email,position;
FirebaseAuth fAuth;
FirebaseFirestore fStore;
public MyViewHolder(#NonNull #NotNull View itemView) {
super(itemView);
//firebase auth instances assign
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
name=itemView.findViewById(R.id.name_text);
phone=itemView.findViewById(R.id.surname_text);
email=itemView.findViewById(R.id.email_text);
position=itemView.findViewById(R.id.position_text);
}
}
XML for single item
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_margin="10dp"
android:padding="20dp"
app:cardCornerRadius="10dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">s
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Email : "
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/email_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="gasg"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Name : "
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="gasg"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Phone : "
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/surname_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="ga"
android:textSize="17sp"
android:textStyle="bold" />
<Button
android:id="#+id/buttonCall"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Call"
android:translationX="70dp" />sg
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Position : "
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/position_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="ga"
android:textSize="17sp"
android:textStyle="bold" />
</LinearLayout>
<Button
android:id="#+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="delete"
android:text="Delete Account"
android:translationX="125dp"
app:backgroundTint="#B84B4B" />
</LinearLayout>
</androidx.cardview.widget.CardView>
In Admin Class your delete button is inside onbindViewHolder you need to write it as
holder.deletebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v){
//delete
}
});

how can make full screen app without AppBarLayout in android studio

how can delete this blue and green section!? do some different ways but not work!
mainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_page);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.addTab(tabLayout.newTab().setText("News"));
tabLayout.addTab(tabLayout.newTab().setText("Archive"));
tabLayout.addTab(tabLayout.newTab().setText("Shop"));
tabLayout.addTab(tabLayout.newTab().setText("Beat Store"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
XML layout :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:background="#71ff76"
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:background="#color/black"
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
App Bar Layout is green but when i delete that my tabs gone! and that blue section seems not any where but always there :|
Extend class with AppCompatActivity
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Other initializations
}
}
Declare different theme for this Activity in Manifest File
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme.FullScreenTheme">
...
Now in styles.xml
<style name="AppTheme.FullScreenTheme" parent="#style/AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
If your layout is having any android.support.v7.widget.Toolbar component, remove that also
remove android:background="#71ff76" from android.support.design.widget.AppBarLayout and remove the mentioned padding android:paddingTop="#dimen/appbar_padding_top"

java.lang.RuntimeException: Unable to start activity ComponentInfo and calling of another activity in Android Studio

I'm new to android development and trying to create a simple register application but the application crash every time I click the Register Here TextView. Other than that, I code the login button to try the calling of another activity but it has no respond after I click the button. I tried so hard to find the mistakes but I'm not sure that what I missed in the coding.
This is the main activity
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);
}
}}
This is codes for login activity
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);
}
});
}}
This is codes for Register activity
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);
}
}
This is the logcat error file
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 files of register activity
<?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 file of login activity
<?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>
Please tell me what I missed. Thank you.
Check all the ids in the xml files and correct their name in java.
It seems like you are referencing an id of another xml file thats not associated with this activity like you might be refering a username field of login xml file in registration activity and vice versa
Nvm, i fixed the problem. There's no problem with the codings just the application is not updated after changes had been made. It is fixed after i clean and rebuild the project. Thank you .

My buttons do not appear when I try to findViewById

I know that there are tons of similar questions because I have read them ALL but my I cannot solve my problem. I have the simpliest setup ever...
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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_marginBottom="60dp"
android:layout_marginRight="46dp"
android:text="Button" />
java
import android.widget.Button;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button my_btn = (Button) findViewById(R.layout.button1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Error
button1 cannot be resolved or is not a field MainActivity.java
I don't know if this is make any difference but I am using the latest API and the Eclipse Version is Juno
Any kind of help is much appreciated!!!!!
Idiot alert....
Button my_btn = (Button) findViewById(R.layout.button1);
This should be
Button my_btn = (Button) findViewById(R.id.button1);
Sorry for wasting your time!

Resources