My Android app is simple. It has only 1 activity. I created two layouts for the same activity: one for the portrait position (inside the res/layout folder) and one for the landscape position (inside the res/layout-land folder). I give the code for both at the end of this question.
I have nothing special inside the myActivity.java file, I just inflate the layout:
package com.example.myApp;
import android.app.Activity;
import android.os.Bundle;
public class MCentralActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myActivity);
}
}
Everything works fine at this point. If I hold my device in the portrait position, the app will call the appropriate XML. It works like a charm too, in case I decide to hold it in the landscape position.
The problem arises when I decide to add a little bit of code to the aforementioned myActivity.java file; it is still really simple!
package com.example.myApp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MCentralActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageButton ibPacientes;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myActivity);
ibPacientes = (ImageButton)findViewById(R.id.imageButton_Pacientes);
ibPacientes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//NOTHING INSIDE HERE!!
}
});
}
}
After implementing that little code, if I decide to go landscape, the App will stop abruptly saying "Unfortunately myApp has stopped". Interesting enough this won't occur if I don't implement onClickListener!
The exact error given by LogCat is as shown:
03-28 13:39:27.870: E/SurfaceFlinger(157): DRAW orientation 1 viewport:(0, 0, 1920, 1080) frame(0, 0, 1920, 1080)
03-28 13:39:15.360: E/SurfaceFlinger(157): STATE orientation 1 viewport:(0, 0, 1920, 1080) frame(0, 0, 1920, 1080)
03-28 13:39:15.360: I/SurfaceFlinger(157): ######## orientation:1, transformOri:4
Don't think the layout XML files has to do much in my problem, but I will copy and paste them in this link (for portrait) and this other link (for landscape), in order to not to make this question excessively long.
Thanks in advance!
In your landscape XML file you have the ImageButton called imageButton_Users instead of imageButton_Pacientes. Rename it and everything should work fine.
You're trying to find a view that's not there and so your app will crash.
Related
OK, I'm working on the chapter 9 tutorial in Android Boot Camp and I'm having...actually a few issues. The book was written for the older versions of Android Studio but my class is using the latest version. I've done my best to look up tutorials for the latest version but they've become quite rare.
Chapter 9 covers a Master/Detail flow tutorial that some things have worked in and others have not.
Where I stand now is a TextView/WebView issue.
I tried simply converting the WebView to match TextView but then .loadUrl won't work and when I use WebView I get an "unexpected cast error. Layout tag was TextView." And Android studio won't tell me where the layout tag was declared so I'm currently combing through all the files line by line. I'm not certain if this source layout tag is in an .xml, a .java or if I should be looking in the manifest.
I believe this means I have to change the source layout to WebView though I can't find anything in the chapter itself about it except ensuring I have the correct import.
package com.example.bikeandbarge;
import android.app.Activity;
import android.support.design.widget.CollapsingToolbarLayout;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;
import com.example.bikeandbarge.dummy.DummyContent;
public class ItemDetailFragment extends Fragment {
public static final String ARG_ITEM_ID = "item_id";
private DummyContent.DummyItem mItem;
public ItemDetailFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
Activity activity = this.getActivity();
CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
if (appBarLayout != null) {
appBarLayout.setTitle(mItem.content);
}
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.item_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem.id.equals("1")) {
rootView = inflater.inflate(R.layout.photos, container, false);
}
if (mItem.id.equals("2")) {
rootView = inflater.inflate(R.layout.tour, container, false);
}
// Can not get this to update to WebView-not certain where the layout tab is textView
//if (mItem.id.equals("3")) {
// ((WebView) rootView.findViewById( R.id.item_detail )).loadUrl( mItem.item_url ); }
//Can replace WebView with TextView but won't recognize .loadUrl without WebView
if (mItem.id.equals("3")) {
((WebView) rootView.findViewById( R.id.item_detail )).loadUrl( mItem.item_url );
}
return rootView;
}
}
I would love for this to run with loadUrl actually working.
Neither WebView nor TextView will allow me to run the program. The apk file simply can't be compiled for me to even test it. I'd like to at least get to a point where I can compile the apk file and attempt to run it.
Open the layout file item_detail.xml(or is it fragment_item_detail.xml inn the book?) and change the <TextView>-element to <WebView>. Then the "Unexpected cast error" should go away.
In your webView and manifest add the following code:
AndroidManifest.xml
<application
....
android:usesCleartextTraffic="true"
android:hardwareAccelerated="true"
.....
>
In Your Layout where the webView is
<WebView
....
android:usesCleartextTraffic="true"
....
></WebView>
Just making a test application that should display a log message on changing orientation, but when i change orientation using ctrl + f11, there is nothing shown in the logcat.
what is the issue in the coding
A Part From Manifest
<activity
android:name="com.example.orientationtest.MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize" >
A part from Java
package com.example.orientationtest;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.d("error", " orientation changes");
}
}
try this if target api 13 and above
android:configChanges="orientation|keyboardHidden|screenLayout|screenSize"
or
android:configChanges="orientation|keyboardHidden|screenLayout"
for lower than 13 ..... this will help you... :)
Using configChanges is not a good idea if you only want to know if the screen rotated. It basically means that you plan to handle the whole rotation change by yourself in the code.
If you don't plan to do this, just check what is the current rotation when your Activity is created:
int currentOrientation = getResources().getConfiguration().orientation;
Then just do whatever you want using this check:
if(currentOrientation == Configuration.ORIENTATION_LANDSCAPE){
//if landscape
} else {
//if portrait
}
This way you don't need to touch your manifest file and you let your app manage the configuration changes by itself.
You can keep the previous orientation used in the activity saved instance if you need to compare the previous and the current one.
The issue is that you prevented the android from changing its orientation
android:configChanges="orientation|screenSize"
that line of code is preventing it to configuration change and that why your not getting any response..
I have 2 classes for an Android project.
The first class is the Activity and the second class is just a OnClickListener which implements the interface.
If I run the project on my phone I always get an runtime error.
Also I got the message:
The specified activity does not exist! Getting the launcher activity.
Here are my two classes
SendActivity
package kops.sms;
//import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
public class SendActivity extends Activity {
Button buttonSend= (Button) findViewById(R.id.buttonSend);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
buttonSend.setOnClickListener(new ButtonListener());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.send, menu);
return true;
}
}
and the ButtonListener
package kops.sms;
import android.view.View;
import android.view.View.OnClickListener;
public class ButtonListener implements OnClickListener {
#Override
public void onClick(View v)
{
}
}
I donĀ“t know what is wrong...
I look forward to your replies! :)
You cannot call findViewById() until after you call setContentView(). Please move:
Button buttonSend= (Button) findViewById(R.id.buttonSend);
to after:
setContentView(R.layout.activity_send);
and before:
buttonSend.setOnClickListener(new ButtonListener());
Also, in the future, please use LogCat (e.g., in the DDMS perspective in Eclipse) to examine the Java stack trace associated with your crashes. You would have been told about your NullPointerException, and that may have helped you to fix your problem.
Be sure that your Activity is declared in your manifest. Also, change your onCreate()
public class SendActivity extends Activity {
Button buttonSend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send);
buttonSend = (Button) findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new ButtonListener());
}
You can't call a View such as a Button before you call setContentView() as it exists in your Layout and you haven't inflated your Layout until you call setContentViewe().
If these don't fix your problem then please post Logcat
Edit
Unless I missed it, you need to have all of your Activitys in your manifest. Something like:
<activity
android:name="your.package.name.SendActivity"
// activity attributes such as config changes, lable, etc...
</activity>
Logcat
Logcat output can be one of the most important pieces to determining a crash. It lists what the error was and a line number with activity where the problem occurred. If using Eclipse,
Window-->Show View-->Other-->Android-->Logcat
If you copy/paste the Logcat using coding brackets, it makes getting help much easier. You can also set filters for the logs so you don't get every single message and it is much more manageable. For example, I have a filter with: Filter Name: Runtime, by Log Tag: AndroidRuntime, by Log Level: error. This gives me only error messages for runtime errors/crashes. These filters are on the left side of the logcat view. Hope this helps
this is the current code
import android.os.Bundle;
import android.app.Activity;
import android.opengl.GLSurfaceView;
public class OpenglstencilActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GLSurfaceView view = new GLSurfaceView(this);
view.setEGLConfigChooser(8, 8, 8, 8, 16, 4);
view.setRenderer(new OpenGLRenderer());
setContentView(view);
}
}
and i updated the manifest to include
if i change the stencilsize from 4 to 0 then the app will load. any value other than 0 and i get an illegal argument exception. i need the stencil size set as im trying to use the stencil buffer.
any ideas?
If you are using the Android emulator, none of the EGL configurations are suitable to enable stencil.
So no luck for using stencil on the emulator.
If you are using a device, then it is a different matter and this answer would be wrong.
i am fairly new to android programming and usually find my answers to my problems by searching, but this one i just cant and its very confusing.
The code itself doesn't show any signs of problems, well i do get 2 java exception breakpoints but i dont know how to fix those as they are "unknown"but when i run it on the emulator it says the application has stopped unexpectedly force close. I try to debug it but i dont know how to do it that well. any way here are the codes btw the app is just a test all it to do is have buttons that take me to other "pages" and back. I would appreciate any help.
Main java file
package com.simbestia.original;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class original extends Activity implements View.OnClickListener {
Button button1, button2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.pagetwo);
button2 = (Button) findViewById(R.id.main);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.pagetwo:
setContentView(R.layout.pagetwo);
break;
case R.id.main:
setContentView(R.layout.main);
break;
}
}
}
Main xml
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:text="pagetwo" android:id="#+id/pagetwo" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Well here is what i change the code to this one is just one button but it works with multiple and i made a class for every page...
package com.simbestia.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mainmenu = (Button) findViewById(R.id.mainmenu);
mainmenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), mainmenu.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Works how i wanted to so its all good i guess ty again
In the command line/terminal, use ./adb logcat to see, in real time, warnings, erros and such from your device, while you run your app. That should help you a lot.
Note: Don't forget to be in the right folder... <android-sdk-version>/platform-tools, that's where the ADB is.
You should learn to debug your own application. Starting with a break point right in the first line of your onCreate() method.
You can also take a look here: http://www.droidnova.com/debugging-in-android-using-eclipse,541.html
Another possibility is to add a log call in the first line of your onCreate() so you can see where the log of your app starts...
edit:
The way you want to switch the layout is wrong. Try layout switcher or start a new activity for your new layout. calling setContentView more than once is basically just wrong...
Some things to check: make sure your code source folder is the same as your package name (com.simbestia.original) ; make sure it builds (without errors) before you try and run it and make sure that your manifest file has its package attribute set to your package name (com.simbestia.original).