findViewById doesn't find my view - android

This is my java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
counter = 0;
add = (Button) findViewById(R.id.add_butt);
sub = (Button) findViewById(R.id.sub_butt);
display = (TextView) findViewById(R.id.tv_text);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter++;
display.setText("Your Total is " + counter);
}
});
and this is my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
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"
tools:context="com.example.myfirstapp.StartingPoint$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Total is 0"
android:textSize="40sp"
android:layout_gravity="center"
android:id="#+id/tv_text" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add 1"
android:layout_gravity="center"
android:id="#+id/add_butt" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Sub 1"
android:layout_gravity="center"
android:id="#+id/sub_butt" />
</LinearLayout>
I get a NullPointerExeption error when I try to add the listeners, because the findViewById method returns null on all the views... I think that everything is in place. All the views are in the XML and they have the proper ID's, so what did i do wrong?

You are trying to findViewById of views that are located in the Fragment.
Try moving this code to your PlaceholderFragment class (OnCreateView)

Related

Changing Activities through a button and display xml

I am very new at android development, and I am trying to make an app which has 4 buttons in its main activity and when I click on one of its button it takes me to another activity and displays its xml file, what should I write in the 2nd activity? Here is my code so far.
main xml file
<?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="vertical">
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#color/colorAccent"
android:text="Overview"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Information" />
<TableRow
android:id="#+id/hr1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Work Experience" />
<TableRow
android:id="#+id/hr3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
</LinearLayout>
</LinearLayout>
........................
main activity
package com.lakshay.display.piechart;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
Button btn1 , btn2 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
String nextAct = null ;
String shield = "com.lakshay.display.piechart";
Integer flag= -1;
switch (v.getId())
{
case (R.id.one ):
nextAct = shield + "ContactActicity";
break;
default:
Toast.makeText(MainActivity.this , "Item Currently Unavailable"
, Toast.LENGTH_SHORT).show();
}
try {
if (nextAct!=null)
{
intent = new Intent(MainActivity.this , Class.forName(nextAct));
flag = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
if (flag != -1 ){
intent.setFlags(flag);
} startActivity(intent);
}
} catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
...................
xml file for 2nd activity
<?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"
android:background="#color/colorAccent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
tools:context="com.lakshay.display.piechart.ContactActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Name"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:inputType="text"
android:paddingBottom="20dp"
android:paddingLeft="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Address"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:id="#+id/address"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:id="#+id/number"
android:paddingBottom="20dp"
android:background="#android:color/background_light"
android:paddingLeft="20dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
</LinearLayout>
...................
My answer as a checklist:
1.- If you are using android studio you should create 2nd activity with the assistant so you dont get into more complications.
The 2nd activity must have an xml file and a class file.
2.- You should add android:onClick propertie for your button in the xml file on the activity.
3.- Your 2nd activity must have an onCreate method in the class file to fill the contents of the 2nd activity.
3b.- You can leave onCreate with default content but your xml file must then have al the info of your textviews.
I can elaborate further if needed.
Let's say you have two activities : Activity1.java and Activity2.java.
to start Activity2 from Activity1 just do :
final Intent intent = new Intent(this, Activity2.class);>startActivity(intent)
If you want to start activity on button click you have to write this codein an onClickListener. To do that, write the following attribute in your button definition in Activity1 xml file.
android:onCLick="onButtonClick"
Then in your activity write the following listener :
public void onButtonClick(final View v) {
// put your intent here
}
This code help you ....
public class MainActivity extends AppCompatActivity {
Button btn1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn1);
final String name = editText.getText().toString();
button.setOnClickListener(new View.OnClickListener() {//when your btn1 button press
public void onClick(View v) {
Intent intent = new Intent(this, Activity2.class);//this is call your second activity
startActivity(intent);//start activity
}
});
}
}
First of all, you must get your Button id from your xml. Or you will get NullPointerException so change your onCreate like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
And if you want to call intent with Class, you can see this solution
OR
You can simply call another activity like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(firstActivity.this, secondActivity.class));
}
});
}

Android: How to inflate different layout from different buttons?

I trying to do making something tabs but using button.
but each time I click on button it add me a row.
I want it to change the all layout to another..
MainActivity
public class MainActivity extends Activity {
Button btn1;
Button btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(switchListener);
btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(switchListener);
}
private OnClickListener switchListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//ScrollView sv = (ScrollView) findViewById(R.id.MyScrollView);
LinearLayout ll = (LinearLayout) findViewById(R.id.MyLinearLayout);
LayoutInflater inflater = LayoutInflater
.from(getApplicationContext());
if (btn1 == v) {
View v1 = inflater.inflate(R.layout.firstview, null);
ll.addView(v1);
} else {
View v2 = getLayoutInflater().inflate(R.layout.secondview, null);
ll.addView(v2);
}
}
};
}
and here my Main XML file
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Layout 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Layout 2" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ScrollView
android:id="#+id/MyScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/MyLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</TableRow>
and here my other two layouts that I want to inflate
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="First View"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:background="#android:color/holo_blue_light"/>
and the second
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Second View"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:background="#android:color/holo_red_light"/>
First make match parent your tableview.
and now do below on your code:-
if(ll.getChildCount() > 0) {
ll.removeAllViews();
}
if (btn1 == v) {
View v1 = inflater.inflate(R.layout.firstview, null);
ll.addView(v1);
} else {
View v2 = getLayoutInflater().inflate(R.layout.secondview, null);
ll.addView(v2);
}
the problem is you are just adding child on layout one by one so you need to remove first then add into layout so try above code.
before adding views to your ll linear layout check, as
if(ll.getChildCount() > 0) {
ll.removeAllViews();
}
then perform:
ll.addView(view) v1 or v2 as required.

Android Problems with a TextView

this sounds easy at the beginning but is driving me insane.
So i downloaded the latest android sdk and eclipse and now there is somthing new.... :
when iam creating a Activity and a Layout it generates me 2 Layout files somthing like: main_laout.xml and fragment_main.xml
however eclipse opend up only the fragment file and i made my GUI there. When iam starting my Application all my Buttons and TextViews are there. I Press a button and a Second Activity starts.
And here my Problem: The Second Activity is like the First one (the 2 layout xml files but here called status)
when iam trying to change a TextView there i get a nullPointer exception. Can anyone plz help me with this iam getting crazy.
My Code so far:
statusActivity:
public class StatusActivity extends ActionBarActivity{
private TextView version,dbstatus,dbrows;
private Button done,refresh;
NetworkTask task;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
version=(TextView) findViewById(R.id.versionsOutputTextSTATUS);
dbstatus=(TextView) this.findViewById(R.id.dbstatusOutputTextSTATUS);
dbrows=(TextView) this.findViewById(R.id.dbRowsOutputTextSTATUS);
done=(Button) this.findViewById(R.id.beendenButtonSTATUS);
refresh=(Button) this.findViewById(R.id.refreshButtonSTATUS);
version.setText("Test");
}
And my xml files:
activity_status.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.project.zigbeecontrol.StatusActivity"
tools:ignore="MergeRootFrame" />
fragment_status.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:background="#drawable/eisblumen"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="de.project.zigbeecontrol.StatusActivity$PlaceholderFragment" >
<TextView
android:id="#+id/welcomeTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:text="#string/status"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/versionsOutputTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/versionTextSTATUS"
android:layout_alignBottom="#+id/versionTextSTATUS"
android:layout_marginLeft="24dp"
android:layout_toRightOf="#+id/versionTextSTATUS"
android:text="#string/empty"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/dbstatusOutputTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/dbstatusTextSTATUS"
android:layout_alignLeft="#+id/versionsOutputTextSTATUS"
android:text="#string/empty"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/beendenButtonSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/welcomeTextSTATUS"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/welcomeTextSTATUS"
android:background="#drawable/button_trans"
android:text="#string/endeStatus"
android:onClick="onClickStatus" />
<TextView
android:id="#+id/dbRowsTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/beendenButtonSTATUS"
android:layout_alignRight="#+id/dbstatusTextSTATUS"
android:layout_marginBottom="26dp"
android:text="#string/dbrows"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/dbstatusTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/beendenButtonSTATUS"
android:text="#string/datenbankstatus"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/versionTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/dbstatusTextSTATUS"
android:layout_below="#+id/welcomeTextSTATUS"
android:layout_marginTop="56dp"
android:text="#string/version"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/dbRowsOutputTextSTATUS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/dbRowsTextSTATUS"
android:layout_alignBottom="#+id/dbRowsTextSTATUS"
android:layout_alignLeft="#+id/dbstatusOutputTextSTATUS"
android:text="#string/empty"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/refreshButtonSTATUS"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/welcomeTextSTATUS"
android:layout_marginBottom="17dp"
android:layout_marginLeft="31dp"
android:layout_toRightOf="#+id/welcomeTextSTATUS"
android:text="#string/refresh"
android:onClick="onClickStatus" />
</RelativeLayout>
even when its working i post the parts of the "main" programm too so you can see how i worked there :
MainActivity:
public class MainActivity extends ActionBarActivity{
//Buttons
private Button beendenButton;
private Button statusbutton;
private Button restartButton;
private Button auswertungButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
/*Zuweißung der Buttons */
beendenButton=(Button) this.findViewById(R.id.endButtonMAIN);
statusbutton=(Button) this.findViewById(R.id.statusButtonMAIN);
restartButton=(Button) this.findViewById(R.id.restartButtonMAIN);
auswertungButton=(Button)this.findViewById(R.id.auswertungButtonMAIN);
/*Fertig mit initzialisieren warten auf Eingabe*/
}
private void statusMethod() {
try{
Intent intent = new Intent(this, StatusActivity.class);
startActivity(intent);
this.finish();
}catch(Exception e){errorMessage();}
}
//.... Some uninterresting Stuff here
public void onClick(View v)
{
switch (v.getId())
{
/*Auswahl was gedrückt wurde und aufruf der Entsprechenden Methode */
case R.id.endButtonMAIN: endActivity(); break;
case R.id.statusButtonMAIN: statusMethod();break;
case R.id.restartButtonMAIN: restartMethod();break;
case R.id.auswertungButtonMAIN: sensorMethod(); break;
default: break;
}
}
}
And the two xmls from layout:
activity_main:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.project.zigbeecontrol.MainActivity"
tools:ignore="MergeRootFrame"
/>
and last but not least fragment_main:
<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="#drawable/eisblumen"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="de.project.zigbeecontrol.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/endButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/welcomeTextMAIN"
android:background="#drawable/button_trans"
android:text="#string/beenden"
android:onClick="onClick" />
<TextView
android:id="#+id/welcomeTextMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/endButtonMAIN"
android:layout_centerHorizontal="true"
android:text="#string/welcome"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/statusButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/welcomeTextMAIN"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:background="#drawable/button_trans"
android:text="#string/status"
android:onClick="onClick" />
<Button
android:id="#+id/restartButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/statusButtonMAIN"
android:layout_below="#+id/statusButtonMAIN"
android:layout_marginTop="19dp"
android:background="#drawable/button_trans"
android:text="#string/neustart"
android:onClick="onClick" />
<Button
android:id="#+id/auswertungButtonMAIN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/restartButtonMAIN"
android:layout_below="#+id/restartButtonMAIN"
android:layout_marginTop="33dp"
android:background="#drawable/button_trans"
android:text="#string/sensoren"
android:onClick="onClick" />
</RelativeLayout>
So PLZ! why do i get a NPE when trying:
version.setText("Test");
regards
Try this..
Change this..
setContentView(R.layout.activity_status);
to
setContentView(R.layout.fragment_status);
because TextViews and Buttons are in fragment_status.xml so setContentView should refer fragment_status.xml
and remove
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Ans same like that in MainActivity.java
Change this..
setContentView(R.layout.activity_main);
to
setContentView(R.layout.fragment_main);
and remove
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Here is your problem
setContentView(R.layout.activity_status);
but your text view is not present in activity_status.xml. Create your Views in activity_status.xml instead of
fragment_status.xml and remove
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

How to create a Button in the camera view in vuforia?

I am using Vuforia AR sdk and want to create a button on the camera preview on the screen.
I cannot figure out where and how to add the button.
I have edit the camera_overlay_udt.xml like this.. In my layout design i have placed back button and listview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/header"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#android:color/transparent"
android:src="#drawable/back" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Swipart"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/arcstarButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:background="#android:color/transparent"
android:src="#drawable/star_button" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/favListingLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/headerLayout"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ListView
android:id="#+id/favlist"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:layout_marginLeft="7dp"
android:cacheColorHint="#00000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/bottom_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="#color/overlay_bottom_bar_background"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="visible"
android:weightSum="1" >
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#color/overlay_bottom_bar_separators" />
<ImageButton
android:id="#+id/camera_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:contentDescription="#string/content_desc_camera_button"
android:onClick="onCameraClick"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:src="#drawable/camera_button_background" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#id/bottom_bar"
android:background="#color/overlay_bottom_bar_separators" />
</RelativeLayout>
after that please Edit that ImageTargets.java class
private void addOverlayView(boolean initLayout) {
// Inflates the Overlay Layout to be displayed above the Camera View
LayoutInflater inflater = LayoutInflater.from(this);
mUILayouts = (RelativeLayout) inflater.inflate(
R.layout.camera_overlay_udt, null, false);
mUILayouts.setVisibility(View.VISIBLE);
// If this is the first time that the application runs then the
// uiLayout background is set to BLACK color, will be set to
// transparent once the SDK is initialized and camera ready to draw
if (initLayout) {
mUILayouts.setBackgroundColor(Color.TRANSPARENT);
}
// Adds the inflated layout to the view
addContentView(mUILayouts, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// Gets a reference to the bottom navigation bar
mBottomBar = mUILayouts.findViewById(R.id.bottom_bar);
// Gets a reference to the Camera button
mCameraButton = mUILayouts.findViewById(R.id.camera_button);
mCameraButton.setVisibility(View.GONE);
favButton = (ImageButton) mUILayouts.findViewById(R.id.arcstarButton);
listview = (ListView) mUILayouts.findViewById(R.id.favlist);
backButton = (ImageButton) mUILayouts.findViewById(R.id.backButton);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
finish();
}
});
listview.setVisibility(View.GONE);
galleryList = SendFile.getFavourites();
if (galleryList != null) {
gridviewAdapter = new GridviewAdapter(ImageTargets.this);
listview.setAdapter(gridviewAdapter);
}
favButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (galleryList != null && galleryList.size() > 0) {
if (listview.getVisibility() == View.GONE) {
listview.setVisibility(View.VISIBLE);
} else {
listview.setVisibility(View.GONE);
}
} else {
Toast.makeText(ImageTargets.this, "Favourites not fond",
Toast.LENGTH_LONG).show();
}
}
});
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> paramAdapterView,
View paramView, int positon, long paramLong) {
SendFile.setFavourite(galleryList.get(positon));
Intent intent = new Intent(ImageTargets.this,
LoadingScreen.class);
Bundle bundle = new Bundle();
bundle.putInt("x", x_Axis);
bundle.putInt("y", y_Axis);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
});
showDialogHandler = new Handler() {
public void handleMessage(Message msg) {
String aResponse = msg.getData().getString("message");
if ((null != aResponse)) {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Server Response: " + aResponse, Toast.LENGTH_SHORT)
.show();
showAlertDialog(aResponse);
} else {
// ALERT MESSAGE
Toast.makeText(getBaseContext(),
"Not Got Response From Server.", Toast.LENGTH_SHORT)
.show();
}
};
};
loadingDialogHandler.captureButtonContainer = mUILayouts
.findViewById(R.id.camera_button);
mUILayouts.bringToFront();
}
They showing there layouts using handlers
Start you camera preview in a normal way. Place a layout on top of it with transparent background like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff000000"
android:layout_height="match_parent">
<ImageView
android:id="#+id/start_image_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:layout_weight="1"
android:src="#drawable/scan_image"/>
</RelativeLayout>
In java file, you can add this layout like this:
private View mStartupView;
mStartupView = getLayoutInflater().inflate(
R.layout.startup_screen, null);
// Add it to the content view:
addContentView(mStartupView, new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
This way you will get to see your button on top of camera preview. Hope it helps
You can add buttons in cameraoverlay layout which is in layout folder and you can initialize buttons in initAR function which is in mainactivity.
Step 1: Add the button in the camera_overlay.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/camera_overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
style="#android:style/Widget.ProgressBar"
android:id="#+id/loading_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="51dp"
android:text="Button" />
</RelativeLayout>
Step 2: Edit the ImageTargets.java class
private static final String LOGTAG = "ImageTargets";
private Button b1;
Step 3: Modify the initApplicationAR() function of ImageTargets.java class
private void initApplicationAR()
{
// Create OpenGL ES view:
int depthSize = 16;
int stencilSize = 0;
boolean translucent = Vuforia.requiresAlpha();
mGlView = new SampleApplicationGLView(this);
mGlView.init(translucent, depthSize, stencilSize);
mRenderer = new ImageTargetRenderer(this, vuforiaAppSession);
mRenderer.setTextures(mTextures);
mGlView.setRenderer(mRenderer);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b1.setVisibility(View.GONE);
}
});
}
Now lay back and watch your button disappear on a click!
Although it's a long time since the post.. yet I found one article.. wherein you can have the desired thing..
Ref: https://medium.com/nosort/adding-views-on-top-of-unityplayer-in-unityplayeractivity-e76240799c82
Solution:
Step1: Make a custom layout XML file (vuforia_widget_screen.xml). For example, button has been added.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout">
<FrameLayout
android:id="#+id/unity_player_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#null"
android:text="#string/welcome" />
</FrameLayout>
Step 2: Make following changes in the UnityPlayerActivity.java
Replace "setContentView(mUnityPlayer);" with
setContentView(R.layout.vuforia_widget_screen);
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.addView(mUnityPlayer.getView());
-> For anyone, who will face the issue in future. :)

android onClickListener (newbie)

I copied the following code from a book. And when I run the app, it fails to launch.
The offending line is aboutButton.setOnClickListener(this);. If I knock it out, the app works.
Any clues?
Thanks.
G.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View aboutButton = findViewById(R.id.main_about_button);
aboutButton.setOnClickListener(this);
setContentView(R.layout.activity_main);
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
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:gravity="center"
tools:context=".MainMenu"
>
<TextView
android:id="#+id/welcome_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="25sp"
android:text="#string/welcome_title"
/>
<Button
android:id="#+id/search_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/search_button"
/>
<Button
android:id="#+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/new_button"
/>
<Button
android:id="#+id/help_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/help_button"
/>
<Button
android:id="#+id/exit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exit_button"
/>
<Button
android:id="#+id/main_about_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/exit_button"
android:layout_alignLeft="#+id/exit_button"
android:text="#string/main_about_button"
android:clickable="true"
/>
</LinearLayout>
aboutButton is null. You can't initialize it until you have inflated the layout. Change it to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Switch these two lines
View aboutButton = findViewById(R.id.main_about_button);
aboutButton.setOnClickListener(this);
}
Your Views exist within your layout which means they will return null if you try to initialize them before inflating your layout, using a layout inflater or by calling setContentView(), which will give you a NPE when trying to call a function on them or setting a listener
had to put the listener statements AFTER the seContentView command. that fixed it.
my guess is, that the setContentView imports all the variables & ids from the layout. and the findViewById just returned a null pointer or something.
if anyone can confirm my guess, i'd appreciate it.
I recommend you to declare the aboutButton as a Button. Try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button aboutButton = (Button) findViewById(R.id.main_about_button);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something
}
});
}
I hope it helps!

Categories

Resources