i have been searching this site for an answer but i cant find one . my android app isnt swapping from portrait to landscape in my emulator it just shows a sideways portrait screen.
I have made a layout and layout-land folder with matching xml names and so on . but it doesnt seem to pick up on the layout-land versions. i do not have an onConfig() function or anything so i was wondering if anyone can help???
mainpage.xml layout-land
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/main_background"
android:gravity="center"
android:padding="30dip">
<TextView
android:id="#+id/main_title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/main_title"
android:textColor="#color/text"
android:textSize="25sp" />
<TableLayout
android:id="#+id/tablelayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:stretchColumns="*">
<TableRow
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<Button
android:id="#+id/start_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/start_button"
android:textColor="#color/text" />
<Button
android:id="#+id/help_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/help_button"
android:textColor="#color/text" />
</TableRow>
<TableRow
android:layout_height="wrap_content"
android:layout_width="match_parent">
<Button
android:id="#+id/extra_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/extra_button"
android:textColor="#color/text" />
<Button
android:id="#+id/stop_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/stop_button"
android:textColor="#color/text" />
</TableRow>
</TableLayout>
</LinearLayout>
LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/main_background"
android:gravity="center"
android:padding="30dip">
<TextView
android:id="#+id/main_title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/main_title"
android:textColor="#color/text"
android:textSize="25sp" />
<Button
android:id="#+id/start_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/start_button"
android:textColor="#color/text" />
<Button
android:id="#+id/help_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/help_button"
android:textColor="#color/text" />
<Button
android:id="#+id/extra_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/extra_button"
android:textColor="#color/text" />
<Button
android:id="#+id/stop_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/stop_button"
android:textColor="#color/text" />
</LinearLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.MC.ChemPal"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".main"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.MC.ChemPal.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".mainpage" android:label="#string/app_name">
<intent-filter>
<action android:name="com.MC.ChemPal.MAINPAGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Extra"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.MC.ChemPal.EXTRA" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".help"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.MC.ChemPal.HELP" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".search_page"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.MC.ChemPal.SEARCH_PAGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
java
package com.MC.ChemPal;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class mainpage extends Activity {
/** Called when the activity is first created. */
Button start_button, help_button, extra_button, stop_button;
TextView display;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
start_button = (Button) findViewById(R.id.start_button);
help_button = (Button) findViewById(R.id.help_button);
extra_button = (Button) findViewById(R.id.extra_button);
stop_button= (Button) findViewById(R.id.stop_button);
display = (TextView) findViewById(R.id.main_title_text);
start_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openSearchPage = new Intent("com.MC.ChemPal.SEARCH_PAGE");
startActivity(openSearchPage);
}
});
help_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openHelp = new Intent("com.MC.ChemPal.HELP");
startActivity(openHelp);
}
});
extra_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openExtra = new Intent("com.MC.ChemPal.EXTRA");
startActivity(openExtra);
}
});
stop_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
onStop();
}
});
}
}
It is an emulator bug, when you try your activity start in landscape mode you will see that it will load mainpage.xml from layout-land ,
android:screenOrientation="landscape"
but ctrl+f12 doesnt work, it is a bug
http://code.google.com/p/android/issues/detail?id=13189
Related
I have two buttons on the main screen. When I click the sad one it works, but when I click on the happy one it doesn't work("Feliz" button). I have tried everything and really I don't know where to put the codes or what. Here is what I have:
package com.example.clara;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = findViewById(R.id.btn1);
Button btn2 = findViewById(R.id.btn2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn1:
Intent intent = new Intent(this,ThirdActivity.class);
startActivity(intent);
Toast.makeText(this, "Yaaay",Toast.LENGTH_SHORT).show();
break;
case R.id.btn2:
Intent tristeza = new Intent(this,SecondActivity.class);
startActivity(tristeza);
Toast.makeText(this, "Noooo",Toast.LENGTH_SHORT).show();
break;
}
}
}
I removed the methods but still doesn't work. Here is the manifest for this app:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.clara">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".ThirdActivity"></activity>
<activity android:name=".SecondActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#drawable/claralinda">
<TextView
android:id="#+id/text2"
android:layout_width="95dp"
android:layout_height="46dp"
android:layout_marginStart="150dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="300dp"
android:layout_marginEnd="150dp"
android:layout_marginRight="150dp"
android:background="#0c3d46"
android:text="#string/Hola"
android:textColor="#c1f1ab"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn1"
android:layout_width="125dp"
android:layout_height="55dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="190dp"
android:layout_marginEnd="220dp"
android:layout_marginRight="220dp"
android:layout_marginBottom="140dp"
android:background="#0c3d46"
android:text="#string/second_page"
android:textColor="#71cfae"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.101"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text2" />
<Button
android:id="#+id/btn2"
android:layout_width="125dp"
android:layout_height="55dp"
android:layout_marginStart="220dp"
android:layout_marginLeft="220dp"
android:layout_marginTop="190dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:layout_marginBottom="140dp"
android:background="#0c3d46"
android:text="#string/second_page_sad"
android:textColor="#71cfae"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.902"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text2" />
</android.support.constraint.ConstraintLayout>
Try this
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ThirdActivity"></activity>
<activity android:name=".SecondActivity"></activity>
</application>
For some odd reason setContentView() is not calling my LoginActivity class. It displays the layout but doesn't read the context? I added a button and the onclicklistener and it isn't being called. Only the layout is being displayed.
This is quite bizarre I've seen to of done every single thing but the LoginActivity is being called? Is it because of the nested layout?
LoginActivity
public class LoginActivity extends BaseActivity implements View.OnClickListener {
#BindView(R.id.button_signin)
Button button_signin;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
//button_login.setOnClickListener(this);
button_signin.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button_signin:
Toast.makeText(this, "test321", Toast.LENGTH_SHORT).show();
break;
}
}
}
BaseActivity
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
}
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/ghostWhiteColor"
android:orientation="vertical"
android:context="test.testing.core.BaseActivity"
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/f_roboto_bold"
android:textColor="#color/holo_blue_light"
android:gravity="center"
android:paddingTop="140dp"
android:textSize="50sp"
android:text="#string/brandingtext" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="200dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_login_username"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/login_username"
android:layout_width="285dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:inputType="text"
android:backgroundTint="#color/ghostWhiteColor"
android:hint="#string/hint_name"
android:singleLine="true"
android:textColor="#color/myBlack"
android:textColorHighlight="#color/antiqueWhiteColor"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/input_login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/login_password"
android:layout_width="285dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:inputType="textPassword"
android:backgroundTint="#color/ghostWhiteColor"
android:hint="#string/hint_name"
android:singleLine="true"
/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/button_signin"
android:layout_width="125dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#drawable/blue_oval"
android:text="#string/btn_login"
android:textColor="#color/ghostWhiteColor" />
<TextView
android:id="#+id/label_register"
android:layout_width="match_parent"
android:layout_height="60dp"
android:fontFamily="#font/f_roboto_lightitalic"
android:gravity="center"
android:text="hello321"
android:textColor="#color/myBlack"
android:textSize="#dimen/fui_heading_padding_bottom" />
</LinearLayout>
</RelativeLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.luxx.market.propg.luxx">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".BaseActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".core.activities.LoginActivity">
</activity>
<activity android:name=".core.activities.RegisterActivity">
</activity>
</application>
</manifest>
You are starting with BaseActivity which is incorrect, baseactivity should be an abstract class.
Put this inside your loginactivity and remove it from the baseactivity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
I'm implementing an Android app for guitar chord recognition. My first layout xml is fine and it's updated, but when I click on login button it goes to next page and didn't show anything on it.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/background"
tools:context="com.example.guitarchordrecognition.MainActivity" >
<EditText
android:id="#+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"
android:ems="10"
android:hint="#string/user" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/user"
android:layout_below="#+id/user"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="#string/pass"
android:inputType="textPassword" />
<Button
android:id="#+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/password"
android:layout_alignRight="#+id/user"
android:layout_below="#+id/password"
android:layout_marginTop="41dp"
android:text="#string/login"
/>
</lativeLayout>
afterlogin.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: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:background="#drawable/background" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_centerVertical="true"
android:text="Button 2" />
</RelativeLayout>`
Mainactivity.java
public class MainActivity extends ActionBarActivity {
Button mybutton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mybutton = (Button)this.findViewById(R.id.login);
mybutton.setOnClickListener(new View.OnClickListener() {
private void buttonclick()
{
startActivity(new Intent("com.example.guitarchordrecognition.Afterlogin"));
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.login:
buttonclick();
break;
}
}
});
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.guitarchordrecognition"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Afterlogin"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.guitarchordrecognition.Afterlogin" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I don't know why you use action constructor, but this time
try startActivity(new Intent(MainActivity.this, Afterlogin.class)); instead.
I have a simple app that updates a counter with plus/minus buttons. I have implemented onClickListeners but I can't get the counter to update based on clicks. I am running my app in an emulator, the application uploads and installs successfully. I also don't see the print statements in the logcat. Please help. Thank you.
package testapp.two;
import testapp.two.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
Button okButton, minusButton, plusButton;
TextView textScore, scoreCard;
int score = 95;
private static final String TAG = "GolfScore";
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate started");
okButton = (Button)findViewById(R.id.buttonOK);
minusButton = (Button)findViewById(R.id.buttonMinus);
plusButton = (Button)findViewById(R.id.buttonPlus);
textScore = (TextView)findViewById(R.id.textScore);
scoreCard = (TextView)findViewById(R.id.scoreCard);
//set button listeners
okButton.setOnClickListener(this);
minusButton.setOnClickListener(this);
plusButton.setOnClickListener(this);
textScore.setText(String.valueOf(score));
Log.d(TAG, "onCreate finished");
}//onCreate
#Override
public void onClick(View v) {
Log.d(TAG, "in onClick");
switch (v.getId()){
case R.id.buttonMinus:
score--;
textScore.setText(String.valueOf(score));
break;
case R.id.buttonPlus:
score++;
textScore.setText(String.valueOf(score));
break;
case R.id.buttonOK:
break;
}//end of switch
}//end of my onclick
}//end of MainActivity
activity 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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="false"
android:layout_centerVertical="false"
android:text="Golf Score App"
android:textAlignment="center" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/title"
android:layout_marginTop="14dp"
android:baselineAligned="false"
android:gravity="fill"
android:orientation="horizontal" >
<Button
android:id="#+id/buttonMinus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="5dp"
android:layout_marginTop="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="-" />
<Button
android:id="#+id/buttonPlus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="0dp"
android:layout_weight="1"
android:text="+" />
<TextView
android:id="#+id/textScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="91"
android:textSize="20sp" />
<Button
android:id="#+id/buttonOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_marginLeft="0dp"
android:layout_marginRight="33dp"
android:layout_weight="1"
android:text="Ok"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="#+id/scoreCard"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/LinearLayout1"
android:layout_marginTop="22dp"
android:text="Score card info goes here" />
</RelativeLayout>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="testapp.two"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You should probably just declare a new View.OnClickListener() rather than having Main activity implement it... that just seems weird to me. Then set the click listener to that
I am not so new to android development, but I am not able to figure out why this silly issue is coming up. All I am doing is starting an activity on application start and it crashes giving nullpointerException. Please check my code below.
May be last-office-hours are making me blind.
Logcat:
08-23 18:19:19.359: E/AndroidRuntime(798): FATAL EXCEPTION: main
08-23 18:19:19.359: E/AndroidRuntime(798): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.click4tab.orderformnew/com.click4tab.orderformnew.Login}: java.lang.NullPointerException
Login.java :
public class Login extends Activity {
Button loginButton;
EditText id, password;
String sID, sPassword;
TestAdapter obj;
Context loginContext;
public static int loginSuccessful;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//Initialising variables
loginButton = (Button)findViewById(R.id.button1);
id = (EditText)findViewById(R.id.editText1);
password = (EditText)findViewById(R.id.editText2);
obj = new TestAdapter(loginContext);
sID = id.getText().toString();
sPassword = password.getText().toString();
StringBuffer buf = new StringBuffer();
buf.append(sID + ":::" + sPassword);
TestAdapter.params = new ArrayList<NameValuePair>();
TestAdapter.params.add(new BasicNameValuePair("tag", "login"));
TestAdapter.params.add( new BasicNameValuePair("query", buf.toString()));
loginButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
//send login information to authenticate user
obj.new Read().execute("");
// Intent i = new Intent("mainActivity");
// startActivity(i);
}
});
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.click4tab.orderformnew"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="mainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
login.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="194dp"
android:text="ID"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignLeft="#+id/button1"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1"
android:layout_marginTop="29dp"
android:text="Password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignLeft="#+id/button1"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginLeft="19dp"
android:layout_marginTop="16dp"
android:text="Login" />
</RelativeLayout>
You have not instantiated loginContext
Do
loginContext = this;
before
obj = new TestAdapter(loginContext);