Unable to start activity ComponentInfo : NullPointerException - android

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);

Related

Loading a URL by using a webview by passing URL in a second activty

I want to pass URL string from main activity to second activity and load the URL in second activity.... but when I click the go button at main activity it goes to second activity but it shows nothing but blank.
here is my code ..
public class MainActivity extends AppCompatActivity {
EditText editText;
Button go;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.urltext);
go=findViewById(R.id.button6);
final String link=editText.getText().toString();
go.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),Webview.class);
intent.putExtra("link",link);
startActivity(intent);
}
});
}
}
Second Activity:
public class Webview extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
String one = getIntent().getExtras().getString("link");
String http="https://";
String url=http+one;
WebView webView = (WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
}
}
Use the following in your manifest :
<uses-permission android:name="android.permission.INTERNET"/>
Refer these examples Please
Web View Android Developers
Can someone give one exact example of webview implementation in android
webView android
your code seems to be fine, just add internet permission in your manifest file
<uses-permission android:name="android.permission.INTERNET"/>
The problem is in this line
final String link=editText.getText().toString();
You have made the variable "link" final that means this variable can be set value only once. Try putting some logs in your code and see what values are passed in the intent extra into the second activity.
Also you have to set WebViewClient to your WebView instance.
Try below code. It worked for me:
private EditText editText;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.urltext);
button = (Button) findViewById(R.id.btn_go);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
String link = editText.getText().toString();
intent.putExtra("link",link);
startActivity(intent);
}
});
}
In the second activity -
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
String one = getIntent().getExtras().getString("link");
String http="https://";
String url=http+one;
Log.d(WebViewActivity.class.getSimpleName(), url);
mWebView = (WebView)findViewById(R.id.wv_url);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl(url);
}
I have several buttons those I have not used yet. I have used button 6.
Activity mainXML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgroundone"
tools:context="com.example.rakib.webbrowser.MainActivity">
<EditText
android:id="#+id/urltext"
android:layout_width="250sp"
android:layout_height="wrap_content"
android:textStyle="italic"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/button"
android:layout_marginTop="20dp"
android:text=""
android:layout_marginRight="15sp"
android:textColor="#FFFF" />
<Button
android:id="#+id/button"
android:background="#drawable/buttonshapehome"
android:text="Facebook"
android:textColor="#fdf900"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/urltext"
android:layout_marginStart="24dp"
android:layout_marginTop="93dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:text="Youtube"
android:textColor="#fdf900"
android:background="#drawable/buttonshapehome"
android:layout_height="wrap_content"
android:textStyle="italic"
android:layout_alignBaseline="#+id/button"
android:layout_alignBottom="#+id/button"
android:layout_marginStart="23dp"
android:layout_toEndOf="#+id/button"
/>
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Google"
android:textStyle="italic"
android:textColor="#fdf900"
android:background="#drawable/buttonshapehome"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_marginStart="23dp"
android:layout_toEndOf="#+id/button2"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:background="#drawable/buttonshapehome"
android:text="linkdin"
android:textColor="#fdf900"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/button"
android:layout_below="#+id/button"
android:textStyle="italic"
android:layout_marginTop="72dp"
/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/buttonshapehome"
android:layout_alignBaseline="#+id/button3"
android:layout_alignBottom="#+id/button3"
android:text="gmail"
android:textStyle="italic"
android:textColor="#fdf900"
android:layout_alignStart="#+id/button2"
/>
<Button
android:id="#+id/newage"
android:layout_width="wrap_content"
android:background="#drawable/buttonshapehome"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button4"
android:text="new age"
android:textStyle="italic"
android:textColor="#fdf900"
android:layout_alignBottom="#+id/button4"
android:layout_alignStart="#+id/button5" />
<Button
android:id="#+id/button6"
android:text="GO"
android:layout_width="50dp"
android:textStyle="bold"
android:layout_height="35dp"
android:layout_alignBaseline="#+id/urltext"
android:layout_alignBottom="#+id/urltext"
android:layout_toEndOf="#+id/urltext"
android:background="#039337"
/>
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/button3"
android:layout_marginBottom="53dp"
android:background="#drawable/settings_icon"
android:text=""
android:textColor="#ffaa00" />``
<TextView
android:id="#+id/textView"
android:text="settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/button8"
android:layout_alignParentBottom="true"
android:layout_marginBottom="27dp"
android:layout_marginEnd="16dp"
android:textColor="#ffff" />
</RelativeLayout>
Activity webviewXML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rakib.webbrowser.Webview">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="470dp">
</WebView>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_below="#id/webview"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="#00ffffff"
>
<ImageView
android:id="#+id/home"
android:src="#drawable/homeicon"
android:clickable="true"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="36sp"
tools:ignore="OnClick" />
<ImageView
android:id="#+id/reload"
android:layout_weight="1"
android:clickable="true"
android:src="#drawable/reload"
android:layout_height="36sp"
android:layout_width="wrap_content"
/>
</LinearLayout>
and this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rakib.webbrowser">
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<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=".Webview">
</activity>
</application>
</manifest>

setContentView() not calling my LoginActivity

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>

linked page didn't update in eclipse

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.

pass intent with extra to defined layout text view

I am at the beginning of an app, I'm an absolute amateur and what i want to do is simple.
I am trying to pull Extras from an intent on my first activity into a textview on my second activity
First page: what is your name? (type a name and push a button)
On the next page: okay so your name is ...(the name they entered)
I had an answer to this but had a little trouble understanding it, in the code i have, when i click the okay button just the word okay shows up in the textview rather than the edited text
here is my code....
main.java
Button ok;
EditText name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.editText);
ok=(Button)findViewById(R.id.button);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String a=ok.getText().toString();
Intent intent = new Intent(getApplicationContext(), secondactivity.class);
intent.putExtra("NAMEDATA",a);
startActivity(intent);
}
});
}
main.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/okay_button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:textStyle="bold|italic"
android:textSize="23sp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignBottom="#+id/button"
android:hint="#string/text"
android:textSize="25sp"
android:textStyle="bold|italic"
android:layout_toLeftOf="#+id/button"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:src="#drawable/start"
android:contentDescription="#string/startimage"
android:layout_below="#+id/button"
android:paddingBottom="20dp"
android:paddingTop="20dp"/>
</RelativeLayout>
secondactivity.java
public class secondactivity extends Activity {
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
t = (TextView)findViewById(R.id.textView3);
String n = this.getIntent().getStringExtra("NAMEDATA");
t.setText(n);
}
activity_second.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=".SecondActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name_sent"
android:textSize="23sp"
android:id="#+id/textView2"
android:textStyle="bold|italic"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/what_to_do"
android:id="#+id/textView"
android:layout_marginTop="13dp"
android:textSize="23sp"
android:layout_below="#+id/textView2"
android:textStyle="bold|italic"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bath_time"
android:id="#+id/button"
android:layout_marginTop="29dp"
android:layout_below="#+id/textView"
android:layout_alignRight="#+id/textView"
android:layout_alignLeft="#+id/textView"
android:textSize="25sp"
android:textStyle="bold|italic"
android:onClick="viewimage"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/school"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_alignRight="#+id/button"
android:layout_alignLeft="#+id/button"
android:textSize="25sp"
android:textStyle="bold|italic"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bed"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_alignRight="#+id/button2"
android:textSize="25sp"
android:textStyle="bold|italic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:layout_alignTop="#+id/textView2"
android:layout_toRightOf="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:paddingStart="4dp"
android:layout_alignParentRight="true"
android:textSize="23dp"
android:textStyle="bold|italic" />
</RelativeLayout>
androidmainfest.xml
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".main"
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=".secondactivity"
android:label="#string/title_activity_second" >
</activity>
<activity
android:name=".bathactivity"
android:label="#string/title_activity_bath"
android:parentActivityName="com.example.myapplication.SecondActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myapplication.SecondActivity" />
</activity>
</application>
can anyone tell me where I've gone wrong? Think its in my onCreate method on either Activity but really stuck,
many thanks
Actually - you don't pass anything from the EditText widget, you are only passing from the Button
Here lies the problem:
String a=ok.getText().toString();
Intent intent = new Intent(getApplicationContext(), secondactivity.class);
intent.putExtra("NAMEDATA",a);
You could add the contents of the EditText onto the end of this string in the same way.
Try something like this:
String nameStr = name.getText().toString();
String a = ok.getText().toString();
Intent intent = new Intent(getApplicationContext(), secondactivity.class);
intent.putExtra("NAMEDATA",a+nameStr);

landscape, portrait

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

Categories

Resources