I've got a TextView that crashes the app when trying to setText. I'm not using the XML layout and have set a custom view to handle everything. Obviously the app crashes when the screen is touched after the app is initalized.
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout lv = new LinearLayout(this);
lv.setOrientation(LinearLayout.VERTICAL);
mView = new MyView(this);
TextView tvX = new TextView(this);
lv.addView(tvX);
lv.addView(mView);
lv.setBackgroundColor(Color.GRAY);
tvX.setTextColor(Color.WHITE);
tvX.setText("Ahmad");
setContentView(lv);
mView.requestFocus();
public class MyView extends View {
private TextView tvX;
public void setTextView(TextView tv){
tvX = tv;
}
#Override public boolean onTouchEvent(MotionEvent event) {
tvX.setText("123");
}
Any help is appreciated!
EDIT 1
08-27 00:39:49.859: E/MessageQueue-JNI(510): at co.projx.touchpaint.TouchPaint$MyView.onTouchEvent(TouchPaint.java:286)
08-27 00:39:49.874: E/AndroidRuntime(510): at co.projx.touchpaint.TouchPaint$MyView.onTouchEvent(TouchPaint.java:286)
Your problem is the same as here: View becomes null on setText
You're missing the LayoutParams (WRAP_CONTENT, WRAP_CONTENT).
Related
I have an overlay on my screen which will show some buttons to control the feature. This overlay will have a dynamic list of buttons which will be created based on the number of steps. Example if steps are 3 then 3 button will get created. I have created a LinearLayout inside this buttons will get created. I have created a method which will set all the button attributes, but somehow it's giving me null pointer exception.
In onCreateView I have initialize the buttonArray.
Here is my fragment:
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mContext = getActivity();
gc = GlobalClass.getInstance(mContext);
db = DataBaseHelper.getInstance(mContext);
sectionButtons = new Button[3];
/*if(savedInstanceState!=null)
mVisible = savedInstanceState.getBoolean("visible",false);*/
manager = SharedPreferenceManager.getInstance(mContext);
//check wether to play video in cover mode or another mode
inflater.inflate(R.layout.fragment_video_side_by_side, container, false);
}
and onViewCreated() I am calling my method addSections():
// show overlay with buttons in onTouch
parentLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!mVisible) {
overLay.setVisibility(View.VISIBLE);
addSections(3);
mVisible = true;
} else {
overLay.setVisibility(View.GONE);
mVisible = false;
}
}
return true;
}
});
Method:
public void addSections(int numOfSection){
if(numOfSection==0)
return;
else {
for (int i = 0; i < numOfSection; i++) {
//set the properties for button
sectionButtons[i].setLayoutParams(new LinearLayout.LayoutParams(50, 50));
sectionButtons[i].setBackgroundResource(R.drawable.round_button);
sectionButtons[i].setText("Section "+i+"");
sectionButtons[i].setId(i);
//add button to the layout
mSectionLayout.addView(sectionButtons[i]);
}
}
}
mSectionLayout is my parent LinearLayout. While debugging I found my sectionButtons is not showing null but while setting the attributes it's throwing nullPointer exception.
Create Dynamic Buttons and attach with the Layout
You can easily create the dynamic button by the following way:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
TextView textView = new TextView(this);
textView.setText("Text View ");
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
layout.addView(textView, p);
Button buttonView = new Button(this);
buttonView.setText("Button");
buttonView.setOnClickListener(mThisButtonListener);
layout.addView(buttonView, p);
}
private OnClickListener mThisButtonListener = new OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Hello !",
Toast.LENGTH_LONG).show();
}
};
}
Using this code, you easily generate a list of buttons on the layout.
Happy coding
I have a simple problem with views in Android. I have two view rootView and containerView, containerView is contained in rootView, but I don't understand why when I click on containerView rootView is triggered.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
LinearLayout rootView = new LinearLayout(this);
rootView.setBackgroundColor(Color.BLUE);
rootView.setGravity(Gravity.CENTER);
rootView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
rootView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("esx","ROOT VIEW ID: "+v.getId());
}
});
LinearLayout containerView = new LinearLayout(this);
containerView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,100));
containerView.setBackgroundColor(Color.CYAN);
rootView.addView(containerView);
setContentView(rootView);
}}
I want that when I click containerView, nothing happens. Is there a way to do this?
Thanks
Try setting containerView.setClickable(true)
onClick provides a View argument. Just compare it and see if it's the rootView. If that doesn't work, try to see if that ISN'T the containerView (you'll need to declare it before adding the listener.
I have a issue trying to dynamically add some linear layout into a linearlayout container after user have clicked on a button.
private void AddView() {
MyView myView1 = new MyView("Name");
this.mainLinearLayout.addView(myView1);
}
This code works great in activity's onCreate method but not after handling user event.
Do you have any idea why it's not working ? (I mean nothing appears on UI)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AddView(); => works great
}
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddView(); => not working
}
});
Thanks,
I think that AddView is wrong.
private void addView() {
LayoutParams lparams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
MyView myView1 = new MyView("Name");
myView1.setLayoutParams(lparams);
this.mainLinearLayout.addView(myView1);
}
I've looked around SO for how to make a textview clickable, and I am doing everything they say but the onClick method is still not being called. Here's my code from the activity:
public void onClick(View v) {
switch(v.getId()) {
case R.id.month_january:
Log.w("onclick", "yes");
TextView temp = (TextView) findViewById(v.getId());
temp.setTextColor(Color.GRAY);
switchNumberDays(31);
break;
case R.id.month_february:...
I put the log in to see whether the onClick was being called when I clicked on the textview, and nothing was logged. Here's my xml for that specific textview:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:id="#+id/month_january"
android:text="January" />
Thanks for any help in advance!
Because your class is implementing the onClickListener interface, be sure that you register it with your textview once it is created.
public class someActivity extends Activity implements View.OnClickListener {
public void onCreate(Bundle savedInstanceState) {
TextView textView = new TextView();
textView.setOnClickListener(this);
}
}
hi please do in this way because i think yours listeners are not used properly in yours code..
For making your TextView clickable just add this to your TextView:
android:clickable="true"
after that you can set onClickListener to it by using this:
yourTextView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do your work here
}
});
or an another way is also there
public class sticks extends Activity implements View.OnTouchListener {
private TextView tv1;
private TextView tv2;
private TextView tv3
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
// bind listeners
tv1.setOnTouchListener(this);
tv2.setOnTouchListener(this);
tv3.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// check which textview it is and do what you need to do
// return true if you don't want it handled by any other touch/click events after this
return true;
}
}
This is what I've done to implement CommonsWare's answer in the following question:
How do I create a help overlay like you see in a few Android apps and ICS?
But it fails. There are no errors but nothing appears when I run (I've made sure that the isFirstTime() function below is functioning properly
#Override
public void onCreate(Bundle savedInstanceState)
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
if(isFirstTime())
{
LayoutInflater inflater = LayoutInflater.from(this);
final FrameLayout overlayFrameLayout = new FrameLayout(this);
setContentView(overlayFrameLayout);
overlayFrameLayout.addView(inflater.inflate(R.layout.main, null));
overlayFrameLayout.addView(inflater.inflate(R.layout.overlay, null));
overlayFrameLayout.setVisibility(View.VISIBLE);
overlayFrameLayout.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
overlayFrameLayout.setVisibility(View.INVISIBLE);
overlayFrameLayout.removeViewAt(1);
return false;
}
});
}
setContentView(R.layout.main);
ctx = getApplicationContext();
I'm pretty sure that I've gone wrong when creating the FrameLayout. Appreciate the help, thanks!
You don't see anything because you call setContentView twice and the R.layout.main is being inflated and replacing your previously created and assigned overlayFrameLayout. The following code should sort it out.
#Override
protected void onCreate(Bundle savedInstanceState)
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
if(isFirstTime()){
final LayoutInflater inflater = getLayoutInflater();
final FrameLayout frameLayout = new FrameLayout(this);
inflater.inflate(R.layout.main, frameLayout, true);
final View overlayView = inflater.inflate(R.layout.overlay, frameLayout, false);
overlayView.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
frameLayout.removeView(overlayView);
return false;
}
});
frameLayout.addView(overlayView);
setContentView(frameLayout);
}
else{
setContentView(R.layout.main);
}
}