java.lang.NullPointerException on onClickListener - java

Can you see if there is anything wrong with my code?
I tried to create dialog box when the button is pressed.
I don't see something wrong here, But the logcat shows java.lang.NullPointerException in this line "agree.setOnClickListener(new OnClickListener() {"
package com.sociyo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //Lock Orientation
requestWindowFeature(Window.FEATURE_NO_TITLE); //Hide Action menu
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //Load main activity
//viewPager default page
ViewPagerAdapter adapter = new ViewPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.pager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
//textView clickable
Button agree = (Button)findViewById(R.id.btnTerms);
agree.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder tpDialog = new AlertDialog.Builder(null);
tpDialog.setTitle("Terms and Policy");
tpDialog.setMessage(R.string.action_settings)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
MainActivity.this.finish();
}
});
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And this is the complete log cat :
E/AndroidRuntime(13076): FATAL EXCEPTION: main
E/AndroidRuntime(13076): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sociyo/com.sociyo.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(13076): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
E/AndroidRuntime(13076): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
E/AndroidRuntime(13076): at android.app.ActivityThread.access$600(ActivityThread.java:137)
E/AndroidRuntime(13076): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
E/AndroidRuntime(13076): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(13076): at android.os.Looper.loop(Looper.java:213)
E/AndroidRuntime(13076): at android.app.ActivityThread.main(ActivityThread.java:4793)
E/AndroidRuntime(13076): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(13076): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(13076): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
E/AndroidRuntime(13076): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
E/AndroidRuntime(13076): at dalvik.system.NativeStart.main(Native Method)
**E/AndroidRuntime(13076): Caused by: java.lang.NullPointerException**
E/AndroidRuntime(13076): at com.sociyo.MainActivity.onCreate(MainActivity.java:34)
E/AndroidRuntime(13076): at android.app.Activity.performCreate(Activity.java:5008)
E/AndroidRuntime(13076): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
E/AndroidRuntime(13076): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
E/AndroidRuntime(13076): ... 11 more
My activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
And my activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#android:color/transparent"
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"
android:orientation="vertical" >
<TextView
android:id="#+id/tvRegisterTitle"
style="#style/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/register_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/etNameReg"
style="#style/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spMlmList"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="#string/name_hint"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/etEmailReg"
style="#style/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etNameReg"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="#string/email_hint"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/etPasswordReg"
style="#style/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/etEmailReg"
android:layout_below="#+id/etEmailReg"
android:layout_centerVertical="true"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="#string/password_hint"
android:inputType="textPassword" />
<Spinner
android:id="#+id/spMlmList"
style="#style/textfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvRegisterTitle"
android:layout_below="#+id/tvRegisterTitle"
android:layout_marginTop="15dp"
android:entries="#array/mlm_list" />
<Button
android:id="#+id/btnRegister"
style="#style/buttonBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/etPasswordReg"
android:layout_below="#+id/chkAgree"
android:layout_marginTop="15dp"
android:text="#string/register_text" />
<CheckBox
android:id="#+id/chkAgree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/etPasswordReg"
android:text="#string/agree_text"
android:layout_marginTop="15dp"
style="#style/checkbox"/>
<Button
android:id="#+id/btnTerms"
android:background="#android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/chkAgree"
android:layout_alignBottom="#+id/chkAgree"
android:layout_toRightOf="#+id/tvRegisterTitle"
android:layout_marginLeft="10dp"
android:text="#string/terms_policy"
style="#style/smallLink"/>
</RelativeLayout>

Yes the button doesnt exist at that point.
Your R.id.btnTermsR.id.btnTerms is defined in activity_register.xml, but that layout is never inflated. or not accessible at that point :)
Its the button that has the Problem, not the OnClickListener. A nullpointer within the Listener would appear the moment you click the button, not while attaching it.

// Before Replace
AlertDialog.Builder tpDialog = new AlertDialog.Builder(null);
// After Replace
AlertDialog.Builder tpDialog = new AlertDialog.Builder(MainActivity.this);

AlertDialog.Builder tpDialog = new AlertDialog.Builder(null);
change this line too
AlertDialog.Builder tpDialog = new AlertDialog.Builder(MainActivity.this);

Related

I am facing runtime error while building an android application [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
My Error message:
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.blood_doner, PID: 9696
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.blood_doner/com.example.blood_doner.MainActivity}: android.view.InflateException: Binary XML file line #11 in com.example.blood_doner:layout/activity_main: Binary XML file line #11 in com.example.blood_doner:layout/activity_main: Error inflating class LinearLayoutCompat
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.view.InflateException: Binary XML file line #11 in com.example.blood_doner:layout/activity_main: Binary XML file line #11 in com.example.blood_doner:layout/activity_main: Error inflating class LinearLayoutCompat
Caused by: android.view.InflateException: Binary XML file line #11 in com.example.blood_doner:layout/activity_main: Error inflating class LinearLayoutCompat
Caused by: java.lang.ClassNotFoundException: android.view.LinearLayoutCompat
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at android.view.LayoutInflater.createView(LayoutInflater.java:813)
at android.view.LayoutInflater.createView(LayoutInflater.java:774)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:911)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:928)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:948)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1002)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
at com.example.blood_doner.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.LinearLayoutCompat" on path: DexPathList[[zip file "/data/app/~~E_zA6DvG2UIaGgWdJTrtLw==/com.example.blood_doner-WiH1_nFAcKgFOSBhTUwffw==/base.apk"],nativeLibraryDirectories=[/data/app/~~E_zA6DvG2UIaGgWdJTrtLw==/com.example.blood_doner-WiH1_nFAcKgFOSBhTUwffw==/lib/x86, /system/lib, /system_ext/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 33 more
I/Process: Sending signal. PID: 9696 SIG: 9
My activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:background="#drawable/background_image"
android:id="#+id/drawer_layout"
tools:context=".MainActivity">
<LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include
layout="#layout/main_toolbar"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home"
android:textSize="50sp"
android:textStyle="bold"
android:gravity="center"/>
</LinearLayoutCompat>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:gravity="start"
android:background="#drawable/background_image">
</RelativeLayout>
<include
layout="#layout/main_nav_drawer"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:id="#+id/top_layout"
>
<ImageView
android:id="#+id/helpline_button"
android:layout_width="180dp"
android:layout_height="150dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:background="#drawable/rounded_image"
android:padding="5dp"
android:layout_below="#+id/request_button"
app:srcCompat="#drawable/helpline"
tools:ignore="MissingConstraints"
/>
<ImageView
android:id="#+id/request_button"
android:layout_width="180dp"
android:layout_height="150dp"
android:layout_marginRight="20dp"
android:layout_marginTop="50dp"
android:background="#drawable/rounded_image"
android:padding="5dp"
app:srcCompat="#drawable/request"
android:layout_alignParentLeft="true"
/>
<ImageView
android:id="#+id/organization_button"
android:layout_width="180dp"
android:layout_height="150dp"
android:layout_alignParentRight="true"
android:layout_marginTop="50dp"
android:background="#drawable/rounded_image"
android:padding="5dp"
app:srcCompat="#drawable/organization" />
<ImageView
android:id="#+id/plasma_button"
android:layout_width="180dp"
android:layout_height="150dp"
android:background="#drawable/rounded_image"
android:padding="5dp"
android:layout_marginTop="10dp"
app:srcCompat="#drawable/plasma"
android:layout_below="#+id/organization_button"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="30dp"
android:layout_below="#+id/top_layout"
android:id="#+id/blood_group"
>
<ImageView
android:id="#+id/a_positive_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="30dp"
android:background="#drawable/rounded_image"
app:srcCompat="#drawable/a_positive" />
<ImageView
android:id="#+id/a_negative_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="30dp"
android:background="#drawable/rounded_image"
android:layout_toRightOf="#+id/a_positive_button"
app:srcCompat="#drawable/a_negative"
tools:layout_editor_absoluteY="420dp" />
<ImageView
android:id="#+id/b_positive_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="30dp"
android:background="#drawable/rounded_image"
android:layout_toRightOf="#+id/a_negative_button"
app:srcCompat="#drawable/b_positive" />
<ImageView
android:id="#+id/b_negative_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#drawable/rounded_image"
android:layout_toRightOf="#+id/b_positive_button"
app:srcCompat="#drawable/b_negative"
/>
<ImageView
android:id="#+id/ab_positive_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:layout_marginRight="30dp"
android:background="#drawable/rounded_image"
android:layout_below="#+id/a_positive_button"
app:srcCompat="#drawable/ab_positive" />
<ImageView
android:id="#+id/ab_negative_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:layout_marginRight="30dp"
android:background="#drawable/rounded_image"
android:layout_below="#+id/a_negative_button"
android:layout_toRightOf="#+id/ab_positive_button"
app:srcCompat="#drawable/ab_negative" />
<ImageView
android:id="#+id/o_positive_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:layout_marginRight="30dp"
android:background="#drawable/rounded_image"
android:layout_below="#+id/a_negative_button"
android:layout_toRightOf="#+id/ab_negative_button"
app:srcCompat="#drawable/o_positive" />
<ImageView
android:id="#+id/o_negative_button"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:background="#drawable/rounded_image"
android:layout_toRightOf="#+id/o_positive_button"
android:layout_below="#+id/b_negative_button"
app:srcCompat="#drawable/o_negative" />
</RelativeLayout>
<Button
android:id="#+id/log_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:onClick="logout"
android:text="Log out"
android:layout_below="#+id/blood_group"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
/>
</RelativeLayout>
</androidx.drawerlayout.widget.DrawerLayout>
My main Activity.java
package com.example.blood_doner;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity {
//Initialize variable
DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = findViewById(R.id.drawer_layout);
}
public void ClickMenu(View view){
//open drawer
openDrawer(drawerLayout);
}
public static void openDrawer(DrawerLayout drawerLayout) {
//open drawer layout
drawerLayout.openDrawer(GravityCompat.START);
}
public void ClickLogo(View view){
closeDrawer(drawerLayout);
}
public static void closeDrawer(DrawerLayout drawerLayout) {
//close drawer layout
//checks condition
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
//when drawer is open close the drawer
drawerLayout.closeDrawer(GravityCompat.START);
}
}
public void ClickHome(View view){
//recreate activity
recreate();
}
public void ClickDashboard(View view){
//redirect activity to dashboard
redirectActivity(this, Dashboard.class); //Dashboard.class
}
public void ClickAboutUs(View view){
redirectActivity(this, AboutUs.class); //AboutUs.class
}
public void ClickLogout(View view){
Logout(this);
}
//temporary
public static void Logout(Activity activity) {
// AlertDialog.Builder = new AlertDialog.Builder(activity);
// builder.setTitle("Logout");
// builder.setMessage("Are you sure you want to logout?");
// builder.setPositiveButton("YES", new DialogInterface.OnClickListener(){
// #Override
// public void onClick(DialogInterface dialog, int which){
// activity.finishAffinity();
System.exit(0);
// }
// });
// builder.setNegativeButton("NO", new DialogInterface.OnClickListener(){
// #Override
// public void onClick(DialogInterface dialog, int which){
// dialog.dismiss();
//
// }
//
// });
// builder.show();
}
public static void redirectActivity(Activity activity, Class aClass) {
Intent intent = new Intent(activity, aClass);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}
#Override
protected void onPause() {
super.onPause();
closeDrawer(drawerLayout);
}
public void logout(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getApplicationContext(), Login.class));
finish();
}
}
I didn't understand what's the issue is? I'am new to android development and I don't understand any of the error message. Please help me fix this problem! The application is crashing when I'm trying to run! I have given my code sample and error message on the top!
Replace
<LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
with
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"

Getting NullPointerException at NavigationView

I'm trying to make a Drawer Menu but i'm having NullPointer, but i think everything is corect.
I'll post all my code here:
package wagner.com.meuartesanato.home;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import wagner.com.meuartesanato.R;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showHome(null);
TextView textMenu = (TextView) findViewById(R.id.text_menu);
TextView textHome = (TextView) findViewById(R.id.text_home);
TextView textSimulation = (TextView) findViewById(R.id.text_simulation);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
loadDrawer();
}
public void showMenu(View v){
//drawer.openDrawer(GravityCompat.END);
}
public void showHome(View v){
//loadFragment(Home1Fragment.class);
}
public void showSimulation(View v){
Toast.makeText(this,"Mostrar simulação",Toast.LENGTH_SHORT).show();
}
private void loadDrawer(){
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View view = navigationView.getHeaderView(0);
Button about = (Button)view.findViewById(R.id.button_about);
Button howToUse = (Button)view.findViewById(R.id.button_how_to_use);
Button addAccount = (Button)view.findViewById(R.id.button_add_account);
Button changeRegister = (Button)view.findViewById(R.id.button_change_register);
Button changePassword = (Button)view.findViewById(R.id.button_change_password);
Button help = (Button)view.findViewById(R.id.button_help);
Button exit = (Button)view.findViewById(R.id.button_exit);
about.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Mostra tela sobre o app
//loadFragment(SobreFragment.class);
drawer.closeDrawer(GravityCompat.END);
}
});
howToUse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Mostrar como usar",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
addAccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Mostrar algo",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
changeRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Alterar cadastro",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
changePassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Alterar cadastro",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
help.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Help",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//fecha a activity, voltando pra tela de login
finish();
}
});
}
private void loadFragment(Class fragmentClass){
try {
Fragment fragment = (Fragment) fragmentClass.newInstance();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_holder, fragment).commit();
}catch (Exception e) {
e.printStackTrace();
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
drawer.closeDrawer(GravityCompat.END);
return false;
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
} else {
drawer.openDrawer(GravityCompat.END);
}
}
}
And my xml files:
activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
app:headerLayout="#layout/nav_header_main"
android:fitsSystemWindows="true"
android:background="#color/colorWhite"/>
</android.support.v4.widget.DrawerLayout>
activity_main.xml
<?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:paddingTop="10dp"
android:background="#drawable/degrade"
tools:context="wagner.com.meuartesanato.home.MainActivity">
<LinearLayout
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/top_bar">
<LinearLayout
android:onClick="showMenu"
android:layout_width="0dp"
android:layout_weight="3"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#mipmap/menu"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_menu"
android:layout_marginTop="5dp"
android:textSize="12sp"
android:textColor="#FFF"
android:text="Menu"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:onClick="showHome"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#mipmap/home"/>
<TextView
android:id="#+id/text_home"
android:layout_marginTop="5dp"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="Home"
/>
</LinearLayout>
<LinearLayout
android:onClick="showSimulation"
android:layout_width="0dp"
android:layout_weight="3"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#mipmap/simulacao"
/>
<TextView
android:id="#+id/text_simulation"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:textSize="12sp"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="Simulação"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#id/top_bar">
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="#drawable/shadow"/>
<FrameLayout
android:id="#+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:layout_marginBottom="#dimen/activity_vertical_margin">
</FrameLayout>
</LinearLayout>
</RelativeLayout>
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="#FFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/drawer_avatar"
android:layout_marginTop="20dp"
android:layout_width="115dp"
android:layout_height="115dp" />
<TextView
android:id="#+id/full_name"
android:layout_width="180dp"
android:gravity="center"
android:textColor="#color/colorDarkGray"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorLightGray"
android:text="Sobre o App"
android:id="#+id/button_about"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorWhite"
android:text="Como usar o App"
android:id="#+id/button_how_to_use"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorLightGray"
android:text="Adicionar Conta Bancaria?"
android:id="#+id/button_add_account"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorWhite"
android:text="Alterar Cadastro?"
android:id="#+id/button_change_register"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorLightGray"
android:text="Alterar Senha?"
android:id="#+id/button_change_password"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorWhite"
android:text="Ajuda"
android:id="#+id/button_help"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorLightGray"
android:text="Sair"
android:id="#+id/button_exit"
android:textColor="#color/colorPurple"
android:textSize="#dimen/normal_text"
android:textAllCaps="false"
/>
<TextView
android:layout_marginTop="50dp"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Copyright"
android:textColor="#color/colorGray"
/>
</LinearLayout>
The error log is:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: wagner.com.meuartesanato, PID: 9112
java.lang.RuntimeException: Unable to start activity ComponentInfo{wagner.com.meuartesanato/wagner.com.meuartesanato.home.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView$OnNavigationItemSelectedListener)'
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 'void
android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView$OnNavigationItemSelectedListener)'
on a null object reference
at wagner.com.meuartesanato.home.MainActivity.loadDrawer(MainActivity.java:59)
at wagner.com.meuartesanato.home.MainActivity.onCreate(MainActivity.java:41)
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)
I really don't know why i'm getting null pointer exception.
Thanks, for the answers!
The stack trace clearly says what is missing. You could have actually found out the programming error by carefully reviewing your code. Anyways modify the following line in your onCreate method. You were inflating the wrong layout
What it is now - setContentView(R.layout.activity_main);
What you should change it to - setContentView(R.layout.activity_home);

How to use auto complete text in android [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am new in android. I created simple application,that contains autocomplete text field.I wrote some code but unfortunately application stopped when i run. there is no error, How do i fix...
Please any one help!
Mycode here:
Mainactivity.java
package com.h2o;
import android.app.Activity;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.location.Address;
import android.location.Geocoder;
import android.support.v4.app.DialogFragment;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.support.annotation.Nullable;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
CustomDrawerAdapter adapter;
List<DrawerItem> dataList;
EditText mEdit;
private String[] states;
private Spinner spinner;
AutoCompleteTextView autoTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] city= getResources().getStringArray(R.array.city);
autoTextView = (AutoCompleteTextView) findViewById(R.id.city_autoCompleteTextView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,city);
autoTextView.setThreshold(1);
autoTextView.setAdapter(arrayAdapter);
// Initializing
dataList = new ArrayList<DrawerItem>();
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// Add Drawer Item to dataList
dataList.add(new DrawerItem(true)); // adding a spinner to the list - 0
dataList.add(new DrawerItem("Wallet")); // adding a header to the list - 1
dataList.add(new DrawerItem("Balance", R.drawable.ic_balance)); // - 2
dataList.add(new DrawerItem("Profile"));// adding a header to the list - 3
dataList.add(new DrawerItem("Personal", R.drawable.ic_account));
dataList.add(new DrawerItem("Work", R.drawable.ic_account));
dataList.add(new DrawerItem("Address", R.drawable.ic_account));
dataList.add(new DrawerItem("Vehicle", R.drawable.ic_car));
dataList.add(new DrawerItem("Preference", R.drawable.ic_pref));
dataList.add(new DrawerItem("Other Option")); // adding a header to the list
dataList.add(new DrawerItem("About", R.drawable.ic_action_about));
dataList.add(new DrawerItem("Settings", R.drawable.ic_action_settings));
dataList.add(new DrawerItem("Help", R.drawable.ic_action_help));
adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
dataList);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
//Calendar picker
public void selectDate(View view) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getSupportFragmentManager(), "DatePicker");
}
public void populateSetDate(int year, int month, int day) {
mEdit = (EditText)findViewById(R.id.dobText);
mEdit.setText(month + "/" + day + "/" + year);
}
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm + 1, dd);
}
}
//Spinner
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void SelectItem(String item, int possition) {
Fragment fragment = null;
Bundle args = new Bundle();
switch(item) {
case "Balance": fragment = new BalanceFragment(); break;
case "Personal": fragment = new PersonalFragment(); break;
case "Work": fragment = new WorkFragment(); break;
case "Address": fragment = new AddressFragment(); break;
case "Vehicle": fragment = new VehicleFragment(); break;
case "Preference": fragment = new PreferenceFragment(); break;
case "About": fragment = new AboutFragment(); break;
case "Settings": fragment = new SettingsFragment(); break;
case "Help": fragment = new HelpFragment(); break;
default: fragment = new DefaultFragment(); break;
}
fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
mDrawerList.setItemChecked(possition, true);
setTitle(dataList.get(possition).getItemName());
mDrawerLayout.closeDrawer(mDrawerList);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (dataList.get(position).getTitle() == null) {
SelectItem(dataList.get(position).getItemName(), position);
}
}
}
}
log cat:
06-03 12:23:32.446 2843-2843/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
06-03 12:23:32.637 2843-2843/com.h2o D/AndroidRuntime﹕ Shutting down VM
06-03 12:23:32.638 2843-2843/com.h2o E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.h2o, PID: 2843
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.h2o/com.h2o.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.AutoCompleteTextView.setThreshold(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.AutoCompleteTextView.setThreshold(int)' on a null object reference
at com.h2o.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="Address Details"
android:id="#+id/address"
android:textColor="#ff000000"
android:textSize="20dp"
android:layout_marginLeft="10dp"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:layout_weight="0.03">
<TextView
android:layout_width="wrap_content"
android:layout_height="33dp"
android:text="Tag Address"
android:id="#+id/residential"
android:textColor="#ff000000"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:gravity="center" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:weightSum="1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:id="#+id/home"
android:layout_gravity="center"
android:textColor="#FF000000"
android:checked="false"
android:layout_weight="0.22"
android:layout_marginLeft="10dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Office"
android:id="#+id/office"
android:textColor="#FF000000"
android:checked="false"
android:layout_gravity="center" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:layout_weight="0.06">
<TextView
android:layout_width="48dp"
android:layout_height="wrap_content"
android:text="Line 1"
android:id="#+id/address1"
android:textColor="#ff000000"
android:layout_marginLeft="10dp"
android:gravity="center" />
<EditText
android:layout_width="291dp"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="#+id/addressText"
android:layout_marginLeft="50dp"
android:textColor="#ff000000"
android:layout_weight="1.04" /> />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:layout_weight="0.06">
<TextView
android:layout_width="48dp"
android:layout_height="wrap_content"
android:text="Line 2"
android:id="#+id/address2"
android:textColor="#ff000000"
android:layout_marginLeft="10dp"
android:gravity="center" />
<EditText
android:layout_width="291dp"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="#+id/address2Text"
android:layout_marginLeft="50dp"
android:textColor="#ff000000"
android:layout_weight="1.04" /> />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:layout_weight="0.06">
<TextView
android:layout_width="48dp"
android:layout_height="wrap_content"
android:text="City"
android:id="#+id/city"
android:textColor="#ff000000"
android:layout_marginLeft="5dp"
android:gravity="center"
android:layout_gravity="center" />
<AutoCompleteTextView
android:layout_width="112dp"
android:layout_height="wrap_content"
android:id="#+id/city_autoCompleteTextView"
android:ems="10"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="33dp"
android:text="Zipcode"
android:id="#+id/zipcode"
android:textColor="#ff000000"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:gravity="center" />
<EditText
android:layout_width="291dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/zipcodeText"
android:layout_marginLeft="10dp"
android:textColor="#ff000000"
android:layout_weight="1.04" /> />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:weightSum="1"
android:layout_weight="0.09">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country"
android:id="#+id/country"
android:textColor="#ff000000"
android:layout_marginLeft="10dp"
android:layout_gravity="center" />
<Spinner
android:layout_width="75dp"
android:layout_height="31dp"
android:id="#+id/countrySpinner"
android:entries="#array/country_list"
android:layout_marginLeft="40dp"
android:spinnerMode="dropdown"
android:outlineProvider="bounds"
android:layout_gravity="center" />
<TextView
android:layout_width="51dp"
android:layout_height="match_parent"
android:text="State"
android:id="#+id/state"
android:textColor="#ff000000"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:gravity="center" />
<Spinner
android:layout_width="75dp"
android:layout_height="31dp"
android:id="#+id/stateSpinner"
android:entries="#array/state_list"
android:spinnerMode="dropdown"
android:outlineProvider="bounds"
android:layout_weight="0.10"
android:layout_gravity="center"
android:layout_marginLeft="10dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:layout_weight="0.06">
<TextView
android:layout_width="wrap_content"
android:layout_height="33dp"
android:text="Landmark"
android:id="#+id/landmark"
android:textColor="#ff000000"
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:gravity="center" />
<EditText
android:layout_width="291dp"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="#+id/landmarkText"
android:layout_marginLeft="40dp"
android:textColor="#ff000000"
android:layout_weight="1.04" /> />
</LinearLayout>
</LinearLayout>
Thanks for advance!!
Once check ID for below line in layout activity_main file
autoTextView = (AutoCompleteTextView) findViewById(R.id.city_autoCompleteTextView);
Here autoTextView is giving null.

Call another activity from button on camera Android

I'm implementing a Camera app on Android, I'd like to have a button on it which will lead the user to the developer website, or my website.
This button is placed right of the shutter button.
I was trying to call the webview activity from this button, but it's giving me errors and I'm confused on this case, because there are plenty of examples of calling an activity inside another one, but no from a camera app.
I don't know what am I doing wrong.
Here's the piece of code in MainActivity:
public class MainActivity extends Activity /**implements OnClickListener**/ {
ImageView image;
Activity context;
Preview preview;
Camera camera;
Button exitButton;
ImageView fotoButton;
Button webButton;
LinearLayout progressLayout;
String path = "/sdcard/KutCamera/cache/images/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
webButton = (Button) findViewById(R.id.imageView_world);
fotoButton = (ImageView) findViewById(R.id.imageView_foto);
exitButton = (Button) findViewById(R.id.button_exit);
image = (ImageView) findViewById(R.id.imageView_photo);
progressLayout = (LinearLayout) findViewById(R.id.progress_layout);
preview = new Preview(this,
(SurfaceView) findViewById(R.id.KutCameraFragment));
FrameLayout frame = (FrameLayout) findViewById(R.id.preview);
frame.addView(preview);
preview.setKeepScreenOn(true);
fotoButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
takeFocusedPicture();
} catch (Exception e) {
}
exitButton.setClickable(false);
fotoButton.setClickable(false);
webButton.setClickable(true);
progressLayout.setVisibility(View.VISIBLE);
}
});
webButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainActivity.this, WebActivity.class);
MainActivity.this.startActivity(myIntent);
}
});
}
Everytime I run it it throws me this error:
4-04 00:33:43.929 4237-4237/com.kut.kutcamera E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kut.kutcamera/com.kut.kutcamera.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
at com.kut.kutcamera.MainActivity.onCreate(MainActivity.java:56)
at android.app.Activity.performCreate(Activity.java:5283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$700(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:175)
at android.app.ActivityThread.main(ActivityThread.java:5279)
at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
The layout declaration of the world button is just Okay, I don't really think it's because of that, I guess there is something inside that camera method that doesn't allows me to properly make the call.
Can anybody shed some light on this?
Thanks in advance!
EDIT
activiy_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<SurfaceView
android:id="#+id/KutCameraFragment"
android:name="com.kut.camera.KutCameraFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<RelativeLayout
android:id="#+id/rel_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="1"
android:background="#android:color/black"
android:orientation="vertical"
android:padding="10dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textViewReferan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Photo"
android:textColor="#android:color/white"
android:textSize="16sp" />
<Button
android:id="#+id/button_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:text="Ok"
android:textColor="#2799CF" />
</RelativeLayout>
<LinearLayout
android:id="#+id/progress_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/islem_value_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading..." />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:alpha="0.9"
android:background="#android:color/black"
android:padding="10dp" >
<ImageView
android:id="#+id/imageView_foto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/camera"
android:contentDescription="1" />
<ImageView
android:id="#+id/imageView_photo"
android:layout_width="80dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/fotocekicon"
android:contentDescription="2" />
<ImageView
android:id="#+id/imageView_world"
android:layout_width="80dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/world"
android:contentDescription="2" />
</RelativeLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/mark3"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</FrameLayout>
</FrameLayout>
Hi I have update your activity class, just replace with my code let me know you still face the problem.
public class MainActivity extends Activity /**implements OnClickListener**/ {
ImageView image;
Activity context;
Preview preview;
Camera camera;
Button exitButton;
ImageView fotoButton;
ImageView webButton;
LinearLayout progressLayout;
String path = "/sdcard/KutCamera/cache/images/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
webButton = (ImageView) findViewById(R.id.imageView_world);
fotoButton = (ImageView) findViewById(R.id.imageView_foto);
exitButton = (Button) findViewById(R.id.button_exit);
image = (ImageView) findViewById(R.id.imageView_photo);
progressLayout = (LinearLayout) findViewById(R.id.progress_layout);
preview = new Preview(this,
(SurfaceView) findViewById(R.id.KutCameraFragment));
FrameLayout frame = (FrameLayout) findViewById(R.id.preview);
frame.addView(preview);
preview.setKeepScreenOn(true);
fotoButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
takeFocusedPicture();
} catch (Exception e) {
}
exitButton.setClickable(false);
fotoButton.setClickable(false);
webButton.setClickable(true);
progressLayout.setVisibility(View.VISIBLE);
}
});
webButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainActivity.this, WebActivity.class);
MainActivity.this.startActivity(myIntent);
}
});
}
Thank you

Android application crashes every time on startup

I started coding my first app in Android Studio, what it should do at this stage is that you click a button with the digit and it outputs it into the textfield. When i run this code the application crashes right on startup and I have no idea what's wrong.
MyActivity.java
package com.example.david.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.w3c.dom.Text;
public class MyActivity extends Activity {
EditText results;
private int number;
final EditText result = (EditText) findViewById(R.id.number);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BtnPressed(1);
}
});
}
private void BtnPressed(int i) {
result.setText(Integer.toString(i));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, 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.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_my.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<EditText
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/number"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="0"
android:background="#ffe7e7e7"
android:textSize="30sp"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:editable="true"
android:enabled="true"
android:numeric="integer|signed|decimal" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="1"
android:id="#+id/button"
android:layout_below="#+id/number"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_marginTop="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="2"
android:id="#+id/button2"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="3"
android:id="#+id/button3"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="4"
android:id="#+id/button4"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="5"
android:id="#+id/button5"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button4"
android:layout_toRightOf="#+id/button4"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="6"
android:id="#+id/button6"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button5"
android:layout_toRightOf="#+id/button5"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="7"
android:id="#+id/button7"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="8"
android:id="#+id/button8"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button7"
android:layout_toRightOf="#+id/button7"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="9"
android:id="#+id/button9"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button8"
android:layout_toRightOf="#+id/button8"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="0"
android:id="#+id/button11"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_below="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="160dp"
android:layout_height="75dp"
android:text="="
android:id="#+id/button12"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button11"
android:layout_toRightOf="#+id/button11"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="+"
android:id="#+id/button13"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button3"
android:layout_toRightOf="#+id/button3"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="-"
android:id="#+id/button14"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button6"
android:layout_toRightOf="#+id/button6"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="*"
android:id="#+id/button15"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button9"
android:layout_toRightOf="#+id/button9"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="/"
android:id="#+id/button16"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button12"
android:layout_toRightOf="#+id/button12"
android:layout_marginLeft="10dp" />
Logcat:
07-24 16:30:07.894 26564-26564/com.example.david.calculator D/AndroidRuntime﹕ Shutting down VM
07-24 16:30:07.894 26564-26564/com.example.david.calculator W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4190aba8)
07-24 16:30:07.899 26564-26564/com.example.david.calculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.david.calculator, PID: 26564
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.david.calculator/com.example.david.calculator.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1884)
at com.example.david.calculator.MyActivity.<init>(MyActivity.java:18)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Thank you very much for all the replies.
            
You are setting your EditText in your main class and not in your classes onCreate method. You can't find a view if the activity hasn't been created yet.
public class MyActivity extends Activity {
EditText results;
private int number;
final EditText result = (EditText) findViewById(R.id.number);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BtnPressed(1);
}
});
}
Should become
public class MyActivity extends Activity {
EditText results;
private int number;
private EditText result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
result = (EditText) findViewById(R.id.number);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BtnPressed(1);
}
});
}
You were getting a NullPointerException in your log, this should fix it.
You should move
final EditText result = (EditText) findViewById(R.id.number);
inside onCreate(...) after setContentView(..)
Corrected Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
EditText result = (EditText) findViewById(R.id.number);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BtnPressed(1);
}
});
}
Check your code:
final EditText result = (EditText) findViewById(R.id.number);
This should come after setContentView() method just like with the button.
You should do this:
final EditText result = (EditText) findViewById(R.id.number);
in your onCreate after the setContentView.

Categories