Why is this:
public class HelpTab extends Activity
{
LinearLayout helpLayout; //was changed to LinearLayout helpLayout = new LinearLayout(this);
TextView helpText = new TextView(this);
Button tips = new Button(this);
Button walkThrough = new Button(this);
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
buttonCreator();
setContentView(helpLayout);
}
public void buttonCreator()
{
//Button featuress defined in here (setText("") and adding them to the layout, addView();)
}
Causing my program to crash?
I have looked at the code extensively and I can't put my finger on it, and the debugger also says source not found when it opens the new page tab thing to tell me what is happening.
Try calling setContentView(helpLayout); first and then buttonCreator();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(helpLayout);
buttonCreator();
}
Assuming that you are trying to initialize your button inside buttonCreater() with respect to a button you have declared in your helplayout, you might run into Null Pointer Exception.
Related
I'm integrating Unity as a subview in a native Android app.
I'd need to prepare Unity when the app starts, but without showing it, because I need to change between scenes when the user taps a button, without showing the Unity screen presentation.
The flow is:
User chooses a scene in Activity A -> Display scene in PlayGameActivity with UnityPlayer.UnitySendMessage, without Unity splash/loading screen
In iOS I did it like this:
class AppDelegate: UIResponder, UIApplicationDelegate {
var currentUnityController: UnityAppController!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.currentUnityController = UnityAppController()
self.currentUnityController.application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
But I don't find something similar in Android.
This is how I'm showing the UnityPlayer in a subview:
public class PlayGameActivity extends UnityPlayerActivity {
private static final String TAG = "PlayGameActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
// Create the UnityPlayer
mUnityPlayer = new UnityPlayer(this);
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
// Add the Unity view
RelativeLayout layout = (RelativeLayout) findViewById(R.id.container_unity_play_game_activity);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) layout.getLayoutParams();
layout.addView(mUnityPlayer.getView(), 0, lp);
String unityScene = "some_scene";
Log.d(TAG, "calling scene " + unityScene);
UnityPlayer.UnitySendMessage("AController", "AMethodToChangeScene", unityScene);
}
}
I tried changing scene in an Activity with the player and the buttons below, and it works, because I wait for the player to be ready. Unity does receive the message of changing scene and does so after loading itself.
So is there a way to init Unity without showing it like in iOS? Or a way to know when the UnityPlayer is ready/isn't showing the splash screen?
I tested an approach that I think may work for you.
First I made a two View variables so that the views can be manipulated throughout the class.
public class PlayGameActivity extends UnityPlayerActivity {
private static final String TAG = "PlayGameActivity";
private View playerView;
private RelativeLayout layout;
RelativeLayout.LayoutParams lp
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
// Create the UnityPlayer
mUnityPlayer = new UnityPlayer(this);
int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
boolean trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
//MODIFIED VERSION OF YOUR CODE
playerView = mUnityPlayer.getView();
// Add the Unity view SLIGHTY MODIFIED
layout = (RelativeLayout) findViewById(R.id.container_unity_play_game_activity);
lp = (RelativeLayout.LayoutParams) layout.getLayoutParams();
String unityScene = "some_scene";
Log.d(TAG, "calling scene " + unityScene);
UnityPlayer.UnitySendMessage("AController", "AMethodToChangeScene", unityScene);
}
}
Then in my test, I had two buttons with listeners set up. The first button let me send a message to Unity,
Button theButton = (Button)findViewById(R.id.button);
theButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mUnityPlayer.UnitySendMessage("CallThis", "MessageFromAndroid", "HI!");
}
});
Then the second button did the following,
Button good = (Button)findViewById(R.id.button2);
good.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.addView(playerView);
mUnityPlayer.requestFocus();
}
});
So the basic idea is don't add mUnityPlayer using layout.addView() to the view until you are ready. You could have your program call UnitySendMessage, and then from within Unity make a call into Android that runs the second line from the button I made. That is, just replace that button I used with a function that Unity can call. You're probably familiar with it already but I'll link to the Android Scripting API anyway.
EDIT: I ran another test and it is not necessary to put mUnityPlayer.requestFocus() in the method that adds the UnityPlayer to your view.
I want to create multiple TextViews inside a LinearLayout.The following code builds successfully but gives a NullPointerException at the line root.addView(t[i]);
public class MainActivity extends ActionBarActivity {
TextView t[];
LinearLayout root;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
root=(LinearLayout)findViewById(R.id.master);
t=new TextView[10];
LinearLayout.LayoutParams dim=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int i=0;i<10;i++)
{
t[i]=new TextView(this);
t[i].setLayoutParams(dim);
t[i].setText("YOHOHO: "+i);
root.addView(t[i]);
}
setContentView(root);
}
This really has no aim Iam just trying to learn things!
It's giving NPE because you are not setting your activity layout properly.
Do this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.whereLinearLayoutMasterIs); // Add your layout here
root=(LinearLayout)findViewById(R.id.master);
t=new TextView[10];
LinearLayout.LayoutParams dim=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int i=0;i<10;i++)
{
t[i]=new TextView(this);
t[i].setLayoutParams(dim);
t[i].setText("YOHOHO: "+i);
root.addView(t[i]);
}
}
NOTER.layout.whereLinearLayoutMasterIs is indicative, use your layout in which R.id.master is
The problem is that root is null - this is because you've not yet set your Activity's content view via setContentView.
You need to do something like this:
super.onCreate(...);
setContentView(R.layout.yourLayoutName);
root=(LinearLayout)findViewById(R.id.master);
I used to have some experience with developing for android but I started up again after 6 months and forgot most of it. I am now using a macbook to do my developing on and had to set up Eclipse, the Android SDK and AVD all over again and I'm worried I messed something up.
When I start a new project with the default activity that displays "Hello World" on my screen the app runs fine. I then tried to put in two buttons that cause the text in a new TextView to change. But whenever I include the textView part I get a runtime error. When I comment it out, the app runs but obviously nothing happens. Based upon the tutorials I've been reading, this is the appropriate place and way to declare/create the textView but I can't figure out what's wrong. Any suggestions?
[Edit] I was messing around and found that I can make the mytext a field instead of a TextView and that worked. So in my onCreate(), I put
mytext = (TextField)findViewById(R.id.TextView1);
but that doesn't seem the right way to do things.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class IntroActivity extends Activity {
TextView myText = (TextView)findViewById(R.id.textView1);
//i've tried this with final added on to it as well (recommended by eclipse)
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setButtonClickListener();
}
private void setButtonClickListener() {
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myText.setText("Hello");
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// myText.setText("Goodbye");
}
});
}
}
This:
TextView myText = (TextView)findViewById(R.id.textView1);
should be separated. The declaration should be at the same place:
private TextView myText;
But the assignment should come only after setContentView:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView)findViewById(R.id.textView1);
This is done since before setContentView, Dalvik doesn't know from which layout to take the view that matchs the id R.id.textView1
Well I am trying to see if a checkbox is checked or not, but I get errors.
Defining the checkbox code:
public class Preferences extends PreferenceActivity {
CheckBoxPreference togglePref;
...
}
CheckBox Code:
public void checkNotify() {
if (togglePref.isChecked()==(true)) {
...
}
}
OnCreate code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//SharedPreferences settings = getSharedPreferences("EasySettingsPreferences", MODE_PRIVATE);
//boolean notify = settings.getBoolean("notify", false);
checkNotify();
rootView = new LinearLayout(this);
rootView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
rootView.setOrientation(LinearLayout.VERTICAL);
togglePref = new CheckBoxPreference(this);
textView = new TextView(this);
textView.setText(R.string.app_name);
titleView = new LinearLayout(this);
titleView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 26));
titleView.setBackgroundResource(R.drawable.pattern_carbon_fiber_dark);
titleView.addView(textView);
preferenceView = new ListView(this);
preferenceView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
preferenceView.setId(android.R.id.list);
PreferenceScreen screen = createPreferenceHierarchy();
screen.bind(preferenceView);
preferenceView.setAdapter(screen.getRootAdapter());
rootView.addView(titleView);
rootView.addView(preferenceView);
this.setContentView(rootView);
setPreferenceScreen(screen);
}
Logcat Picture:
logcat pic http://img710.imageshack.us/img710/8529/unledxq.png
Debugger Picture
debugger pic http://img847.imageshack.us/img847/1192/unled1rn.png
Please help me if you can. Thanks!
I guess that you never initialise togglePref. To be sure we need to see the onCreate(). (I will update my answer if my guess was wrong...)
edit
I was right. You call checkNotify() before your even initialized the togglePref variable. Check your logic if it really makes sense to call that method before everything else or if it is fine if you call it later.
A tip: You can simplify your if statement:
// yours:
if (togglePref.isChecked()==(true)) {
// simplified:
if (togglePref.isChecked()) {
In your onCreate() method, you're calling checkNotify() before togglePref has been initialized. You want to move that initialization up (or move checkNotify() down.)
I'm having trouble adding a button to a layout that I've created in XML. Here's what I want to achieve:
//some class
else {
startActivity(new Intent(StatisticsScreen.this, ScreenTemperature.class));
}
////
//ScreenTemperatureClass
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this is where I call another class that
//displays a nice graph
setContentView(new GraphTemperature(getApplicationContext()));
}
I want to add a Button to this new screen so that it'll appear below the graph.
I've tried creating a LinearLayout view, then create a Button and add it to this view but I just get NullPointerExceptions..
Any help would be appreciated. Thanks
EDIT#1
Here's what I've tried using that created a NullPointerException and 'force close':
Button buybutton;
LinearLayout layout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphTemperature(getApplicationContext()));
layout = (LinearLayout) findViewById(R.id.statsviewlayout);
Button buyButton = new Button(this);
buyButton.setText(R.string.button_back);
buyButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(buyButton);
}
And here's the logcat error:
ERROR/AndroidRuntime(293): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.weatherapp/com.weatherapp.ScreenTemperature}: java.lang.NullPointerException
ERROR/AndroidRuntime(293): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
ERROR/AndroidRuntime(293): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
ERROR/AndroidRuntime(293): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
ERROR/AndroidRuntime(293): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
theres abviously more lines to do with this error in logcat, not sure if you want it?
EDIT#2
So i tried bhups method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GraphTemperature GT = new GraphTemperature(getApplicationContext());
layout = (LinearLayout) findViewById(R.id.statsviewlayout);
Button buyButton = new Button(this);
buyButton.setText(R.string.button_back);
buyButton.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout.addView(GT); // line 27
layout.addView(buyButton);
setContentView(layout);
}
This method produced the same logcat error as above, NullPointerException, indicating it was something to do with line no. 27 which is the layout.addView line of code. Any ideas? Thanks again
If you just have included a layout file at the beginning of onCreate() inside setContentView and want to get this layout to add new elements programmatically try this:
ViewGroup linearLayout = (ViewGroup) findViewById(R.id.linearLayoutID);
then you can create a new Button for example and just add it:
Button bt = new Button(this);
bt.setText("A Button");
bt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linerLayout.addView(bt);
This line:
layout = (LinearLayout) findViewById(R.id.statsviewlayout);
Looks for the "statsviewlayout" id in your current 'contentview'. Now you've set that here:
setContentView(new GraphTemperature(getApplicationContext()));
And i'm guessing that new "graphTemperature" does not set anything with that id.
It's a common mistake to think you can just find any view with findViewById. You can only find a view that is in the XML (or appointed by code and given an id).
The nullpointer will be thrown because the layout you're looking for isn't found, so
layout.addView(buyButton);
Throws that exception.
addition:
Now if you want to get that view from an XML, you should use an inflater:
layout = (LinearLayout) View.inflate(this, R.layout.yourXMLYouWantToLoad, null);
assuming that you have your linearlayout in a file called "yourXMLYouWantToLoad.xml"