user3847788
user3847788

Reputation:

Android Eclipse setting on click listener error

I am new to android.. in a video tutorial I watch the guy writing a code and compiling it with no any error.. but when the same code I type gives me error.. don't know why.. Well.. Layout xml file is ok.. there is a error in java file.. whenever I add to a onclicklistener method to a button... it does not give any error in the code section but when I compile it it shows an error.. that application XXX has stopped unexpectedly. pleas try again.

code is here..

package com.abrosoft.trycommand;
import java.util.Random;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
import android.view.View.OnClickListener;

public class MainActivity extends ActionBarActivity {
EditText input;
TextView display;
@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();
    }
    Button cmd = (Button) findViewById(R.id.bCmd);
    display = (TextView) findViewById(R.id.tvcmd);
    input = (EditText) findViewById(R.id.etinput);

 //if I delete this SetOnClickListener method.. it runs ok..
 //I get xml layout on screen of emulator.. but with this method..it doesnot run
cmd.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //even I write any lines of code which should perform any action on button
        //click is giving same error
    }
});

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;

    }

}

}

here is my xml file code:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="25dp"
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"
tools:context="com.abrosoft.trycommand.MainActivity$PlaceholderFragment" >

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="type a command"
    android:id="@+id/etinput"
    android:inputType="text"
     />

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100"
    >
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bCmd"
        android:layout_weight="50"
        android:text="Command"
        />
    <ToggleButton 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tgB"
        android:layout_weight="50"
        />
</LinearLayout>

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" 
    android:id="@+id/tvcmd"
    />

thanks in advance

Upvotes: 0

Views: 259

Answers (4)

Mohammed Rampurawala
Mohammed Rampurawala

Reputation: 3112

Remove the condition

if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

Also remove the PlaceHolder Class

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;

    }

and change the setContentView() method of OnCreate(Bundle savedInstances) to

setContentView(R.layout.fragment_main);

Upvotes: 1

vipul mittal
vipul mittal

Reputation: 17401

Put below code inside onCreateView

    Button cmd = (Button) findViewById(R.id.bCmd);
    display = (TextView) findViewById(R.id.tvcmd);
    input = (EditText) findViewById(R.id.etinput);

 //if I delete this SetOnClickListener method.. it runs ok..
 //I get xml layout on screen of emulator.. but with this method..it doesnot run
cmd.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //even I write any lines of code which should perform any action on button
        //click is giving same error
    }
});

Change onCreateView to:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

   Button cmd = (Button) rootView.findViewById(R.id.bCmd);
    display = (TextView) rootView.findViewById(R.id.tvcmd);
    input = (EditText) rootView.findViewById(R.id.etinput);

  //if I delete this SetOnClickListener method.. it runs ok..
  //I get xml layout on screen of emulator.. but with this method..it doesnot run
 cmd.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //even I write any lines of code which should perform any action on button
        //click is giving same error
    }
   });

   return rootView;

}

Upvotes: 0

Mohammed Rampurawala
Mohammed Rampurawala

Reputation: 3112

remove this

if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

from the onCreate(Bundle) method. It will help

Upvotes: 0

GrIsHu
GrIsHu

Reputation: 23638

Just Comment the below section in your code and then try to run.

if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

Upvotes: 0

Related Questions