HeavenHM
HeavenHM

Reputation: 992

Android Application Crashes ActivityManager permission Denied on Eclipse

My Application is crashing every time when I Run it. only HelloWorld app runs fine.
It says Activity Manager permission denied. Please have a look.

Console :
1.[2016-04-18 17:21:21 - Demo] ActivityManager: open: Permission denied
1. [2016-04-18 17:21:21 - Demo] ActivityManager:

Starting: Intent act=android.intent.action.MAIN cat= [android.intent.category.LAUNCHER] cmp=com.example.demo/.MainActivity

and here is my LogCat File.. Android LogCat

And Here is my MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
EditText et = (EditText)findViewById(R.id.editText1);
Button btn = (Button)findViewById(R.id.button1);
TextView tv = (TextView)findViewById(R.id.textView1);

@Override
protected void onCreate(Bundle savedInstanceState) {
btn.setOnClickListener(this);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public void onClick(View v) {
tv.setText(et.getText().toString());
}}

and Here is my Manifest File.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo"
 android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />                
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter></intent-filter>
</activity>
</application>
</manifest>

Upvotes: 0

Views: 674

Answers (4)

Mahesh Giri
Mahesh Giri

Reputation: 1840

Ui is created in onCreate method of activity

public class MainActivity extends Activity implements OnClickListener {
     EditText et;
     Button btm;
     TextView tv;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          et = (EditText)findViewById(R.id.editText1);
          btm = (Button)findViewById(R.id.button1);
          tv = (TextView)findViewById(R.id.textView1);
          btm.setOnClickListener(this);
     }

     @Override
     public void onClick(View v) {
         if(v.getId==btm.getId()) tv.setText(et.getText().toString());
     }
}

Upvotes: 1

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

Do proper initialization of all components. Can not initialize components of view before setting the content view.

public class MainActivity extends Activity implements OnClickListener {
EditText et ;
Button btn ;
TextView tv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et = (EditText)findViewById(R.id.editText1);
btn = (Button)findViewById(R.id.button1);
tv = (TextView)findViewById(R.id.textView1);
btn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
tv.setText(et.getText().toString());
}}

Upvotes: -1

V-rund Puro-hit
V-rund Puro-hit

Reputation: 5534

You are initializing your View outside onCreate() That is totally wrong.

Do something like this

public class MainActivity extends Activity implements OnClickListener {
     EditText et;
     Button btn;
     TextView tv;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          et = (EditText)findViewById(R.id.editText1);
          btn = (Button)findViewById(R.id.button1);
          tv = (TextView)findViewById(R.id.textView1);
          btm.setOnClickListener(this);
     }

     @Override
     public void onClick(View v) {
          tv.setText(et.getText().toString());
     }
}

Hope this will help you out..

Upvotes: 1

Abhishek Sinha
Abhishek Sinha

Reputation: 435

Your initialization is wrong. The view is never created and you are trying to access it. Move the findViewById calls inside onCreate and after the setContentView. Then only you will be able to access the btn object

Upvotes: 0

Related Questions