Android Holo Light styling changes depending on chosen context - java

I have been trying to find the cause of weird formatting style inconsistencies for my Views throughout my application and I think I have narrowed it down with this example.
I set up two equivalent layouts of various Viewss with exactly the same procedure and only varying the Context supplied in creation. In the first set, each View is created with the application's context through Activity.getApplicationContext(), whereas in the second set the Views are fed the activity's context through this.
The result is vastly different:
Any suggestions on why using the application context causes the garbage (and inconsistent - colours are white as well as grey) formatting seen in the screenshot?
Activity code:
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TextViews
TextView tv1 = new TextView(getApplicationContext());
tv1.setText("With Application context");
TextView tv2 = new TextView(this);
tv2.setText("With Activity context");
// Spinners
Spinner sp1 = new Spinner(getApplicationContext());
sp1.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, new String[] {"App context 1", "App context 2", "App context 3"}));
Spinner sp2 = new Spinner(this);
sp2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[] {"Act context 1", "Act context 2", "Act context 3"}));
// Edittexts
EditText et1 = new EditText(getApplicationContext());
et1.setText("Application Context");
EditText et2 = new EditText(this);
et2.setText("Activity Context");
// Buttons
Button b1 = new Button(getApplicationContext());
b1.setText("Application Context");
Button b2 = new Button(this);
b2.setText("Activity Context");
// Layout structure
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(tv1);
ll.addView(sp1);
ll.addView(et1);
ll.addView(b1);
ll.addView(tv2);
ll.addView(sp2);
ll.addView(et2);
ll.addView(b2);
setContentView(ll);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light" >
<activity
android:name="com.test.test.MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

In Android sources getApplicationContext() returns Application object which extends ContextWrapper, so using getApplicationContext() you are passing ContextWrapper subclass, but when you pass this, you are passing an Activity object which extends ContextThemeWrapper so you are passing ContextThemeWrapper subclass. Now the difference between ContextWrapper and ContextThemeWrapper is that ContextWrapper simply delegates all of its calls to another Context and ContextThemeWrapper allows you to modify the theme from what is in the wrapped context.
Although, the question was more about why exactly this is happening (as opposed to the cause which was clear), here are also some helpful posts which explain the perils of incorrectly using application context and how to choose a context correctly:
When to call activity context OR application context?
Using Application context everywhere?
Most importantly from #CommonsWare:
"getApplicationContext() is not a complete Context and consequently does not support everything that Activity does."
Awesome post about Context that should clarify everything:
http://www.doubleencore.com/2013/06/context/

Related

Error: have you declared this activity in your AndroidManifest.xml? when trying to intent a class

Here's my code for clicking the button from my class showInforActivity:
btnRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(showInfoActivity.this, InformationFragment.class);
startActivity(intent);
}
});
Whenever I click the button I got error Unable to find explicit activity class {com.example.drawerapplication/com.example.drawerapplication.ui.information.InformationFragment}; have you declared this activity in your AndroidManifest.xml? and it loads the previous class and layout instead of loading this
InformationFragment class:
package com.example.drawerapplication.ui.information;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.example.drawerapplication.databinding.FragmentInformationBinding;
public class InformationFragment extends Fragment {
private FragmentInformationBinding binding;
private EditText name, address, age, contact;
private Button btnAdd, btnViewData;
DatabaseHelper mDatabaseHelper;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentInformationBinding.inflate(inflater, container, false);
View root = binding.getRoot();
name = binding.name;
address = binding.address;
age = binding.age;
contact = binding.contact;
btnAdd = binding.add;
btnViewData = binding.viewdata;
mDatabaseHelper = new DatabaseHelper(getActivity());
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (name.length() != 0 && address.length() != 0
&& age.length() != 0 && contact.length() != 0){
mDatabaseHelper.addData(name.getText().toString(), address.getText().toString(),
age.getText().toString().trim(), Long.valueOf(contact.getText().toString().trim()));
toastMessages("Data Successfully Inserted!");
}else {
toastMessages("Please complete all the requirements needed");
}
}
});
btnViewData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), ListDataActivity.class);
startActivity(intent);
}
});
return root;
}
#Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
private void toastMessages(String message){
Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
}
}
The layout of this InformationFragment is fragment_information:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.information.InformationFragment"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp">
...
Heres my **AndroidManifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.drawerapplication">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<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/Theme.DrawerApplication">
<activity android:name=".ui.information.ListDataActivity"/>
<activity android:name=".ui.information.showInfoActivity"/>
<activity
android:name=".MainActivity"
android:exported="true"
android:label="#string/app_name"
android:theme="#style/Theme.DrawerApplication.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I tried adding InformationFragment there however, it says Class referenced in the manifest, com.example.drawerapplication.InformationFragment, was not found in the project or the libraries
Please let me know what I'm missing or if you need more information. Thanks!
You cannot use an Intent to start/create a Fragment much like you would for an Activity. A Fragment actually lives inside an Activity i.e. its lifecycle is bound to an Activity. To start a Fragment you have to use the FragmentManager that is available with the Activity instance
This error is coming up because Android thinks that your InformationFragment is an Activity rather than a Fragment and hence it is asking you if you have declared it in your AndroidManifest.xml file because as the rule says, you need to have each Activity in your app declared inside the AndroidManifest.xml file
So, now you can use something like this, inside your showInfoActivity. Technically there are many solutions available as to what you could essentially do and that's why I linked you to all possible solutions rather than just writing them here
You can not use Intent for navigate to Fragment from a Activity.
For this reason you are getting this error. As, Intent receive 2 parameter Intent(FromActivity, ToActivity.class) . you are using fragment in 2nd parameter. We know that, when we create a activity then it it declared with AndroidManifest.xml file. Here, in your manifest file there are not any activity called InformationFragment.
You can learn Android Navigation Component to easily communicate with Activity and fragments.

How create a view inside activity?

My goal is to create a simple ball in the mobile screen center so I can move it around the screen as they drag my finger.
For that I wanted to use the Canvas and bitmap (I do not know if it's the best way seen as already said I am new in the Android world).
I created a function in my Activity so that when a button was clicked, a activity containing the script to the canvas design was created (Again not know if it's the best way. In a php script or javascript I would create the ball in the function itself). My code is as follows:
public void StartGame(View v) {
Intent i = new Intent(MainActivity.this, ball.class);
startActivity(i);
}
By doing this it should call the following activity:
com.teste package;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.view.View;
    
    
    public class ball extends View {
    
        Paint pincelPreto private;
        Paint pincelAmerelo private;
    
        Public ball (Context context) {
            super (context);
        }
    
        Public ball (Context context, attributeSet attrs) {
            super (context, attrs);
    
            setBackgroundColor (Color.LTGRAY);
            pincelPreto = new Paint ();
            pincelPreto.setColor (Color.BLACK);
            pincelAmerelo = new Paint ();
            pincelPreto.setColor (Color.YELLOW);
    
            setFocusable (true);
        }
    
        #Override
        protected void onDraw (Canvas canvas) {
            super.onDraw (canvas);
    
            canvas.drawCircle (200, 200, 200, pincelAmerelo);
        }
    }
And both draw a ball at every location ...
But I get the following error:
Android.content.ActivityNotFoundException: Unable to find explicit activity class {com.teste / com.teste.ball}; have you declared this activity in your AndroidManifest.xml?
What am I doing wrong?
OBS.: I maybe call activity because i dont know the real name of it.
Expanding on Neo's answer above and given your edited question:
The reason you are getting the Android.content.ActivityNotFoundException is because "ball" is a View, not an Activity, and so you cannot start "ball" as an activity.
public void StartGame(View v) {
Intent i = new Intent(MainActivity.this, ball.class);
startActivity(i);
}
Since you are new to Android, try to visualize an Activity as a component that usually takes over the whole screen, and a View as a smaller piece of that. You want your ball to display within your activity.
In your res/layout folder you should have a layout xml file for your MainActivity. You can add your ball view in here with some xml, similar to the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.teste.ball
android:id="#+id/ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.teste.ball>
</LinearLayout>
You may hit other problems once you've gotten that far, but it should be easier to debug then.
I agree with the comment above to learn the basics first before jumping in to a complicated application. Take a look at the Android Developer pages to learn the components and trying simple tutorials. There are a lot of components that make up an Android application and fully understanding them and how they interact with each other will make your life easier :). Good luck and welcome to Android!
Edit to answer question below ...can i do it with java? i searched something about "inflate" a view but i dont know if it is correct?
Yes you can add a view to the Activity's parent layout using addView. Inflate is for instantiating views in your XML into Java objects, so it will not work in your case because you don't have an xml file for your Ball layout. You would need to create a new Ball object and use addView to add it to your layout:
LinearLayout layout = findViewById(R.id.main);
Ball ball = new Ball(this);
ball.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
layout.addView(ball);
where layout is the parent layout found in your activity's XML. You may need to assign it an ID if you haven't already, so if we use my example above, your LinearLayout tag would look like:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
If you want to create an activity then you need to extend it by Activity class. By extending view you can create a custom view, which you can use within xml or dynamically in your activity or fragment. For example ImageView, TextView, EditText, All layouts extends View, that you can use within your activity or fragment either by xml or dynamically.
As others have noted, Ball extends View, which is not an Activity. To use the startActivity() method, you need to pass it an Activity. First create your BallView:
package com.test.myapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class BallView extends View {
private final Paint pincelPreto;
private final Paint pincelAmerelo;
public BallView(final Context context) {
super(context);
setBackgroundColor(Color.LTGRAY);
pincelPreto = new Paint();
pincelPreto.setColor(Color.BLACK);
pincelAmerelo = new Paint();
pincelAmerelo.setColor(Color.YELLOW);
}
#Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(200, 200, 200, pincelAmerelo);
}
}
Then create a BallActivity. This consists of both a Java class and an XML resource that you put under res/layout/activity_ball.xml. Below is an example XML resource:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.test.myapplication.BallView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"/>
</FrameLayout>
And the BallActivity class:
package com.test.myapplication;
import android.app.Activity;
import android.os.Bundle;
public class BallActivity extends Activity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ball);
}
}
The last thing that needs to be done in order to launch BallActivity is to add it to AndroidManifest.xml (otherwise you'll continue to get ActivityNotFoundException):
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.test.myapplication"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
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=".BallActivity"/>
</application>
</manifest>
Hope this helps.

Android emulator does not show image stored

I'm working on a project designed to turn a smartphone into a Microscope (little bit of image processing too); I've decided to build my program in Android Studio for android phones.
Looking online I found a few tutorials on how to access the camera, capture an image and store it in memory. For some reason the emulator used to run the program is showing is only capturing the image, but not storing it in memory. Is their an issue with the emulator? How would I transfer this program I'm writing to my Android Google Nexus 5 phone?
here is some XML code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.smartphonemicroscope">
<uses-feature android:name="android.hardware.camera2"></uses-feature> <!-- Acess camera2 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Microscope">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
here is some Java:
package com.example.user.smartphonemicroscope;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.hardware.camera2.CameraDevice;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
public class Microscope extends Activity {
Button button;
ImageView imageView;
static final int CAM_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_microscope);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.image_view);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent, CAM_REQUEST);
}
});
}
private File getFile() {
File folder = new File("sdcard/camera_app");
if(!folder.exists())
{
folder.mkdir();
}
File image_file = new File(folder,"cam_image.jpg");
return image_file;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/camera_app/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}
}
I also have a few images of what it looks like in the interface.
Emulator Camera Accessed, after pressing capture button
Image Captured, but will not save
File folder = new File("sdcard/camera_app");
That value is wrong on ~1.5 billion Android devices.
More generally, never hardcode paths. Always use some method to derive a root location to write into. You appear to be wishing to write to external storage. In that case, use a method like getExternalFilesDir() (on Context) or methods on Environment to get a root location to write to.

ads are not appearing in my app (using libgdx)

I am creating a game using libgdx, i finished the coding and everything is working
but when i implement the ad it doesnt appear
This is my MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import com.google.ads.AdRequest;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.*;
import com.pixelstudioz.RevengeOfTheTunnels.ROTTGame;
public class MainActivity extends AndroidApplication{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
//setContentView(R.layout.main);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams gameViewParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//gameViewParams.bottomMargin = 150;
requestWindowFeature( Window.FEATURE_NO_TITLE );
View gameView = initializeForView(new ROTTGame(), cfg);
layout.addView(gameView, gameViewParams);
AdView adView = new AdView(this);
adView.setAdUnitId("YOUR ADMOB AD ID");
//adView.setAdUnitId("app-id");
adView.setAdSize(com.google.android.gms.ads.AdSize.BANNER);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, adParams);
com.google.android.gms.ads.AdRequest request = new com.google.android.gms.ads.AdRequest.Builder()
.addTestDevice(AdRequest.TEST_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
adView.loadAd(request);
setContentView(layout);
initialize(new ROTTGame(), cfg);
}
}
and this is my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
package="com.package.appname"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
<activity
android:name="com.google.android.gms.samples.ads.GoogleAdsSampleActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.pixelstudioz.RevengeOfTheTunnels.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and this is my layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="unitid"
ads:adSize="SMART_BANNER"
ads:testDevices="TEST_EMULATOR"
ads:loadAdOnCreate="true"/>
</LinearLayout>
PLEASE HELP
DO i need the app id first??? like from admob?? or what??
anw check the code, and thanksss
The problem seems to be that you are initializing your Game twice...
First, you are using the intitializeForView() method to get back the main window view that is used and which contains the OpenGL Window your libgdx stuff runs in.
This way, by the way, you will never need the layout.xml... Libgdx creates a layout Object (the gameView you get back from initializeForView(). So although the layout.xml is present, it's never actually used, so it also doesn't matter if you put an adview in there.
And at the bottom of your onCreate() you are calling initialize(new ROTTGame(), cfg); again, creating a second game view with opengl window and a new instance of your game. And this will override the first instance you have created and to which you have added your AdView instance.
So if you remove that last line, it should work.
Nevertheless, you will still need to input all necessary data from your AdMob account, meaning your ID etc and ideally adding a test device. Before adding the ID, you most likely won't get any ads from AdMob and ergo won't get anything to show...
Hope it helps...

startActivity() causing crash in Android

So I just started learning to develop Android apps, and I have a programming background (Python mainly), so I somewhat know what I'm doing. I'm having a problem with using startActivity(). I've commented code to figure out exactly where the error gets thrown, and it happens right as startActivity() is encountered. The error I get is from the emulator and it is just a popup window that says, "Unfortunately, Test has stopped." (Test is my program name) after I click my button. My code is this
package com.test.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class TestActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.test.test.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public class DisplayMessageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
}
public void sendMessage(View view) {
setContentView(R.layout.main);
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.textBox1);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
Now I know that won't do anything yet, but why is it crashing?
My XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText android:id="#+id/textBox1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/textBox1Hint" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1"
android:onClick="sendMessage" />
</LinearLayout>
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestActivity"
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=".DisplayMessageActivity" >
</activity>
</application>
</manifest>
I saw that a lot of other people were having problems with this because they had not declared their activities in the manifest, but I think I have done so properly. Any help would be so greatly appreciated.
One possible problem is that
EditText editText = (EditText) findViewById(R.id.textBox1);
returns null because you dont set anywhere the layout for your activity (with setContentView)
If your editetext is included in view (passed to sendMessage), you can find it with
EditText editText = (EditText) view.findViewById(R.id.textBox1);
I think problem is you are on DisplayMessageActivity and starting the same activity.
What you need to do is start test activity and call DisplayMessageActivity from intent.
Use separate files for TestActivity and DisplayMessageActivity classes. In your given code you are not specifying a layout for your DisplayMessageActivity activity using setContentView.
the problem is this,
you declare in the manifest that the main class is TestActivity:
<activity
android:name=".TestActivity"
android:label="#string/app_name" >
but you wanted to start DisplayMessageActivity,
so, change this to the following:
public class TestActivity extends Activity {
#Override
I can't understand what you are actually trying to achieve by starting the same activity again. Try this,
public class DisplayMessageActivity extends Activity {
private String message ;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setLayout(R.layout.<name of the xml file> ); // change the name if it is not in layout folder
EditText editText = (EditText) findViewById(R.id.textBox1);
String message = editText.getText().toString();
Button submitBtn = (Button) findViewById(R.id.button1);
submitBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(DisplayMessageActivity.this,
message,
Toast.LENGTH_SHORT);
toast.show();
}});
}
}
The above code simply displays the String, This is just a sample code... Hope that helps!!!
EDIT
To run this code you must also add id tag to your Button Tag inside XML file having value button1..
Actullay you have created the Activity inside activity. Which is not much pref-able but if want to stick with that then you have to change the android:name=".DisplayMessageActivity" to

Categories

Resources