Simple Android App wont run [duplicate] - java

This question already has answers here:
NullPointerException accessing views in onCreate()
(13 answers)
Closed 8 years ago.
Whenever I try to run my Android app made in Eclipse it gives me Has stopped working.
It's a simple button and text view text setting. Here is my code.
package com.example.polidoesnttwantacracker;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
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.TextView;
public class MainActivity extends ActionBarActivity {
private static final Button Button = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView v = (TextView)findViewById(R.id.Maikati);
v.setText("Welcome you Dick.");
Button but = (Button)findViewById(R.id.ButtonaNaMaikaTi);
but.setText("WTF");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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;
}
}
}
The xml file as requested.
<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: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.polidoesnttwantacracker.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/Maikati"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="149dp"
android:text="" />
<Button
android:id="#+id/ButtonaNaMaikaTi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Maikati"
android:layout_centerHorizontal="true"
android:text="OK" />
</RelativeLayout>
So if you can please help me. It's really frustrating and I can't go on with my lections if i don't get it running.
Here is the cat log that it gives me :D
06-01 19:05:28.438: E/Trace(2561): error opening trace file: No such file or directory (2)
06-01 19:05:28.534: D/AndroidRuntime(2561): Shutting down VM
06-01 19:05:28.534: W/dalvikvm(2561): threadid=1: thread exiting with uncaught exception (group=0xa61f4908)
06-01 19:05:28.538: E/AndroidRuntime(2561): FATAL EXCEPTION: main
06-01 19:05:28.538: E/AndroidRuntime(2561): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.polidoesnttwantacracker/com.example.polidoesnttwantacracker.MainActivity}: java.lang.NullPointerException
06-01 19:05:28.538: E/AndroidRuntime(2561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-01 19:05:28.538: E/AndroidRuntime(2561): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-01 19:05:28.538: E/AndroidRuntime(2561): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-01 19:05:28.538: E/AndroidRuntime(2561): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-01 19:05:28.538: E/AndroidRuntime(2561): at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 19:05:28.538: E/AndroidRuntime(2561): at android.os.Looper.loop(Looper.java:137)
06-01 19:05:28.538: E/AndroidRuntime(2561): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-01 19:05:28.538: E/AndroidRuntime(2561): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 19:05:28.538: E/AndroidRuntime(2561): at java.lang.reflect.Method.invoke(Method.java:511)
06-01 19:05:28.538: E/AndroidRuntime(2561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-01 19:05:28.538: E/AndroidRuntime(2561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-01 19:05:28.538: E/AndroidRuntime(2561): at dalvik.system.NativeStart.main(Native Method)
06-01 19:05:28.538: E/AndroidRuntime(2561): Caused by: java.lang.NullPointerException
06-01 19:05:28.538: E/AndroidRuntime(2561): at com.example.polidoesnttwantacracker.MainActivity.onCreate(MainActivity.java:23)
06-01 19:05:28.538: E/AndroidRuntime(2561): at android.app.Activity.performCreate(Activity.java:5104)
06-01 19:05:28.538: E/AndroidRuntime(2561): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-01 19:05:28.538: E/AndroidRuntime(2561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-01 19:05:28.538: E/AndroidRuntime(2561): ... 11 more
My teacher did it the same way I did it and it ran for him :(

Check the identifiers of the views are properly set in activity_main.xml
If you defined those views in the fragment, then check they are in fragment_main.xml and move the declaration you've done in the onCreate of the activity into the onCreateView of the fragment (using rootView to find them)
Check also you are using the proper theme in your application or activity in AndroidManifest.xml
android:theme="#style/Theme.AppCompat.Light"
Cheers

Related

.com.process stopped unexpectedly in Android emulator and mobile

I am trying to make a simple android app that will add two numbers and display the output.However, I am getting the following error continuously.
"calculator.com.process stopped unexpectedly"
I used API 10 for rendering layout and API 17 as compiler and Target SDK.
Could post the code finally after all these trouble.
This is the code of main_activity.java
package com.example.calculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
EditText txtNum1;
EditText txtNum2;
TextView Display;
Button bCal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNum1=(EditText) findViewById(R.id.Num1);
txtNum1=(EditText) findViewById(R.id.Num2);
Display=(TextView)findViewById(R.id.tvDisplay);
bCal=(Button)findViewById(R.id.bAdd);
bCal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View Tawfiq) {
// TODO Auto-generated method stub
double x=Double.parseDouble(txtNum1.getText().toString());
double y=Double.parseDouble(txtNum2.getText().toString());
double total=x+y;
Display.setText("Sum is"+total);
}
});
}
#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);
}
}
The xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/bAdd"
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.calculator.MainActivity" >
<EditText
android:id="#+id/Num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/Num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Num1"
android:layout_below="#+id/Num1"
android:layout_marginTop="33dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="#+id/bAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDisplay"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Button" />
<TextView
android:id="#+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Num2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Sum"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
This is the logcat.
01-24 16:06:39.840: D/AndroidRuntime(1130): Shutting down VM
01-24 16:06:39.870: W/dalvikvm(1130): threadid=1: thread exiting with uncaught exception (group=0xb60164f0)
01-24 16:06:39.950: E/AndroidRuntime(1130): FATAL EXCEPTION: main
01-24 16:06:39.950: E/AndroidRuntime(1130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.os.Looper.loop(Looper.java:130)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-24 16:06:39.950: E/AndroidRuntime(1130): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 16:06:39.950: E/AndroidRuntime(1130): at java.lang.reflect.Method.invoke(Method.java:507)
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-24 16:06:39.950: E/AndroidRuntime(1130): at dalvik.system.NativeStart.main(Native Method)
01-24 16:06:39.950: E/AndroidRuntime(1130): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.example.calculator.MainActivity.onCreate(MainActivity.java:27)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-24 16:06:39.950: E/AndroidRuntime(1130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-24 16:06:39.950: E/AndroidRuntime(1130): ... 11 more
You are having problem at line no-27
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.example.calculator.MainActivity.onCreate(MainActivity.java:27)
Change the relativeLayouts Id in the xml file.
It should not be same as for the add button.
android:id="#+id/relativeLayout"
Change it as above.
Two id in the same layout cannot be same.
Your add button and the relative layout is having the same id,.
And during the findViewById(), relative layout is being caste in the Button.
That's why class cast exception is coming.
Edit:
I compiled your code:-
the next mistake you have made is-
txtNum1=(EditText) findViewById(R.id.Num2);
It should be
txtNum2=(EditText) findViewById(R.id.Num2);
Mistake-You are not assigning the reference to the second edittext.

Android App Counter Button [duplicate]

This question already has answers here:
NullPointerException accessing views in onCreate()
(13 answers)
Closed 8 years ago.
When I try to open this page it crashes. I have tried to make this button count various ways and can't get it to count! I have tried all kinds of tutorials by thenewboston and other people but it doesn't work for me.
Medical.java
package com.fullcyclestudios.rust;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.app.Activity;
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.TextView;
import android.content.Intent;
public class Medical extends ActionBarActivity {
Button add, sub;
int counter;
TextView textView;
TextView quantityDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_medical);
add=(Button) findViewById(R.id.addButtonMedical);
sub=(Button) findViewById(R.id.minusButtonMedical);
quantityDisplay = (TextView) findViewById(R.id.textViewQuantityMedical);
counter=1;
add.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
counter++;
quantityDisplay.setText("it"+counter);
}
});
sub.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
counter--;
quantityDisplay.setText("it"+counter);
}
});
Intent intent = getIntent();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.medical, 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_medical,
container, false);
return rootView;
}
}
/** Bandage button */
public String bandageMaterials(View view)
{
String materials = "Bandage\r\n\r\n2 Cloth";
textView = (TextView) findViewById(R.id.textViewMatMedical);
textView.setText(materials);
return materials;
}
/** Small Medkit button */
public String smallMedkitMaterials(View view)
{
String materials = "Small Medkit\r\n\r\n2 Cloth\r\n2 Blood";
textView = (TextView) findViewById(R.id.textViewMatMedical);
textView.setText(materials);
return materials;
}
/** Large Medkit button */
public String largeMedkitMaterials(View view)
{
String materials = "Large Medkit\r\n\r\n3 Cloth\r\n3 Blood";
textView = (TextView) findViewById(R.id.textViewMatMedical);
textView.setText(materials);
return materials;
}
}
fragment_medical.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: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.fullcyclestudios.rust.Medical$PlaceholderFragment" >
<ScrollView
android:id="#+id/scrollViewMedical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/bandageButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="150dp"
android:layout_height="35dp"
android:text="#string/bandage"
android:onClick="bandageMaterials" />
<Button
android:id="#+id/smallMedkitButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="150dp"
android:layout_height="35dp"
android:text="#string/smallMedkit"
android:onClick="smallMedkitMaterials" />
<Button
android:id="#+id/largeMedkitButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="150dp"
android:layout_height="35dp"
android:text="#string/largeMedkit"
android:onClick="largeMedkitMaterials" />
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/textViewMatMedical"
android:layout_width="120dp"
android:layout_height="fill_parent"
android:layout_marginTop="140dp"
android:background="#color/gray"
android:text="#string/blank" />
<TextView
android:id="#+id/textViewQuantityMedical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/minusButtonMedical"
android:layout_alignRight="#+id/minusButtonMedical"
android:layout_below="#+id/addButtonMedical"
android:gravity="center_horizontal"
android:text="#string/one"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="25sp" />
<Button
android:id="#+id/minusButtonMedical"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="80dp"
android:layout_alignLeft="#+id/addButtonMedical"
android:layout_alignRight="#+id/addButtonMedical"
android:text="#string/minus"
android:textSize="30sp"
android:textStyle="bold" />
<Button
android:id="#+id/addButtonMedical"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignLeft="#+id/textViewMatMedical"
android:layout_alignRight="#+id/textViewMatMedical"
android:text="#string/add"
android:textSize="30sp" />
</RelativeLayout>
ERRORS
06-08 16:10:38.890: W/dalvikvm(855): threadid=1: thread exiting with uncaught exception (group=0xb2a40ba8)
06-08 16:10:38.990: E/AndroidRuntime(855): FATAL EXCEPTION: main
06-08 16:10:38.990: E/AndroidRuntime(855): Process: com.fullcyclestudios.rust, PID: 855
06-08 16:10:38.990: E/AndroidRuntime(855): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fullcyclestudios.rust/com.fullcyclestudios.rust.Medical}: java.lang.NullPointerException
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.os.Handler.dispatchMessage(Handler.java:102)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.os.Looper.loop(Looper.java:136)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-08 16:10:38.990: E/AndroidRuntime(855): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 16:10:38.990: E/AndroidRuntime(855): at java.lang.reflect.Method.invoke(Method.java:515)
06-08 16:10:38.990: E/AndroidRuntime(855): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-08 16:10:38.990: E/AndroidRuntime(855): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-08 16:10:38.990: E/AndroidRuntime(855): at dalvik.system.NativeStart.main(Native Method)
06-08 16:10:38.990: E/AndroidRuntime(855): Caused by: java.lang.NullPointerException
06-08 16:10:38.990: E/AndroidRuntime(855): at com.fullcyclestudios.rust.Medical.onCreate(Medical.java:33)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.Activity.performCreate(Activity.java:5231)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-08 16:10:38.990: E/AndroidRuntime(855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-08 16:10:38.990: E/AndroidRuntime(855): ... 11 more
06-08 16:10:43.330: I/Process(855): Sending signal. PID: 855 SIG: 9
Your issue is here:
setContentView(R.layout.activity_medical);
add=(Button) findViewById(R.id.addButtonMedical);
sub=(Button) findViewById(R.id.minusButtonMedical);
but from what you post: addButtonMedical and minusButtonMedical are inside fragment_medical and not activity_medical xml layout!

Android application crashes after launch. Button issue?

I got problem with my first Android application. Just after running, my app crashes in emulator with "Unfortunately has stopped." message. I checked many threads on Stack and I read pretty much of official documentation and I really do not know what's a cause...
I use AVD with Android 4.4.2, API Level 19. CPU/ABI - ARM. Use Host GPU - checked (without this one emulator works very slowly).
My app should play a song after pressing the button. I would be really grateful for any ideas. Cheers.
Java file:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
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.os.Build;
import android.media.MediaPlayer;
public class MainActivity extends ActionBarActivity {
#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 one = (Button) findViewById(R.id.button1);
final MediaPlayer mediaPlayer1 = MediaPlayer.create(this, R.raw.elements6);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer1.start();
}
});
}
#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;
}
}
}
And XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Errors:
05-14 17:29:21.740: D/AndroidRuntime(1145): Shutting down VM
05-14 17:29:21.740: W/dalvikvm(1145): threadid=1: thread exiting with uncaught exception (group=0xb2abcba8)
05-14 17:29:21.750: E/AndroidRuntime(1145): FATAL EXCEPTION: main
05-14 17:29:21.750: E/AndroidRuntime(1145): Process: com.example.beatzlooper, PID: 1145
05-14 17:29:21.750: E/AndroidRuntime(1145): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.beatzlooper/com.example.beatzlooper.MainActivity}: java.lang.NullPointerException
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.os.Handler.dispatchMessage(Handler.java:102)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.os.Looper.loop(Looper.java:136)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-14 17:29:21.750: E/AndroidRuntime(1145): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 17:29:21.750: E/AndroidRuntime(1145): at java.lang.reflect.Method.invoke(Method.java:515)
05-14 17:29:21.750: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-14 17:29:21.750: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-14 17:29:21.750: E/AndroidRuntime(1145): at dalvik.system.NativeStart.main(Native Method)
05-14 17:29:21.750: E/AndroidRuntime(1145): Caused by: java.lang.NullPointerException
05-14 17:29:21.750: E/AndroidRuntime(1145): at com.example.beatzlooper.MainActivity.onCreate(MainActivity.java:31)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.Activity.performCreate(Activity.java:5231)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-14 17:29:21.750: E/AndroidRuntime(1145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-14 17:29:21.750: E/AndroidRuntime(1145): ... 11 more
You are working with fragments.
The Fragment cannot be added to the R.id.container View because it doesn't exists.
Just delete the whole if-clause in the onCreate() and also delete the Placeholder Class you don't need it actually.
Comment this code
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}*/

Could not find a method sendMessage(View) in the activity class

MainActivity.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.content.Intent;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal" >
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
DisplayMessageActivity.java
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#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_display_message,
container, false);
return rootView;
}
}
}
Error Log
04-10 01:37:00.460: W/dalvikvm(1650): threadid=1: thread exiting with uncaught exception (group=0xb3aaeba8)
04-10 01:37:00.520: E/AndroidRuntime(1650): FATAL EXCEPTION: main
04-10 01:37:00.520: E/AndroidRuntime(1650): Process: com.example.myfirstapp, PID: 1650
04-10 01:37:00.520: E/AndroidRuntime(1650): java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.MainActivity for onClick handler on view class android.widget.Button
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View$1.onClick(View.java:3810)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View.performClick(View.java:4438)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View$PerformClick.run(View.java:18422)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.os.Handler.handleCallback(Handler.java:733)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.os.Handler.dispatchMessage(Handler.java:95)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.os.Looper.loop(Looper.java:136)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.reflect.Method.invoke(Method.java:515)
04-10 01:37:00.520: E/AndroidRuntime(1650): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-10 01:37:00.520: E/AndroidRuntime(1650): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-10 01:37:00.520: E/AndroidRuntime(1650): at dalvik.system.NativeStart.main(Native Method)
04-10 01:37:00.520: E/AndroidRuntime(1650): Caused by: java.lang.NoSuchMethodException: sendMessage [class android.view.View]
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.Class.getConstructorOrMethod(Class.java:472)
04-10 01:37:00.520: E/AndroidRuntime(1650): at java.lang.Class.getMethod(Class.java:857)
04-10 01:37:00.520: E/AndroidRuntime(1650): at android.view.View$1.onClick(View.java:3803)
04-10 01:37:00.520: E/AndroidRuntime(1650): ... 11 more
04-10 01:37:04.820: I/Process(1650): Sending signal. PID: 1650 SIG: 9
I've tried to look at other questions answered similar to mine, but I can't find an answer that seems to help my situation. Can anyone help?
I was struggling with a similar error following the android tutorial
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myfirstapp, PID: 18300
java.lang.IllegalStateException: Could not find method sendMessage (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20909)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
but in my case the WYSIWYG editor (IntelliJ w/Android Studio plugin) ended up populating the onClick property in activity_main.xml with some extra junk about MainActivity:
android:onClick="sendMessage (MainActivity)"
So I deleted " (MainActivity)" and it stopped crashing. I see this is different from your problem as your xml file already seems correct with android:onClick="sendMessage". But wanted to add it here for any others struggling with what I saw. This was the closest post to the issue I was seeing. I'm just getting started and this was killing me, so hope it helps someone else.
The problem is the onClick property in one of your Button tags:
android:onClick="sendMessage" />
Just make sure you have a method sendMessage(View) in your Activity.
It is because you need to implement implements android.view.View.OnClickListener in your class, therefore add the correct imports i.e. import android.view.View.

Java application reading name from a file

I am stuck trying to reading from a .txt file a name and last name into a java application, I have tryed on first method with AssetManager but my application Crash when I enter it.
The second method is with inputstream but I get 3 errors:
Description Resource Path Location Type
ByteArrayOutputStream cannot be resolved to a type MainActivity.java /MyInfo/src/com/example/myinfo line 64 Java Problem
ByteArrayOutputStream cannot be resolved to a type MainActivity.java /MyInfo/src/com/example/myinfo line 64 Java Problem
dummytext cannot be resolved or is not a field MainActivity.java /MyInfo/src/com/example/myinfo line 55 Java Problem
MainActivity.java
package com.example.myinfo;
import java.io.IOException;
import java.io.InputStream;
import android.os.Bundle;
import android.content.res.AssetManager;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
TextView name = (TextView) findViewById(R.id.name);
AssetManager assetManager = getAssets();
InputStream input;
try {
input = assetManager.open("info.txt");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
// byte buffer into a string
String text = new String(buffer);
nume.setText(text);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
TextView dummytext = (TextView) findViewById(R.id.dummytext);
dummytext.setText(readText());
}
private String readText() {
InputStream inputStream = getResources().openRawResource(R.raw.dummytext);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while(i!=-1){
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
}catch (IOException e){
e.printStackTrace();
}
return byteArrayOutputStream.toString();
}
#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;
}
}
}
and my XML is this:
<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: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.myinfo.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="87dp"
android:text="Adauga Fisier" />
<TextView
android:id="#+id/dummytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nume"
android:layout_below="#+id/nume"
android:layout_marginTop="29dp"
android:text="Prenume"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button1"
android:layout_marginRight="14dp"
android:layout_marginTop="65dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
I don't understand what I did wrong.
When I run the code that is now in Comments I notice that I get in Logcat NullPointerException.
LogCat:
04-07 10:06:11.298: I/dalvikvm(275): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-07 10:06:11.298: W/dalvikvm(275): VFY: unable to resolve virtual method 318: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
04-07 10:06:11.298: D/dalvikvm(275): VFY: replacing opcode 0x6e at 0x008b
04-07 10:06:11.308: I/dalvikvm(275): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-07 10:06:11.308: W/dalvikvm(275): VFY: unable to resolve virtual method 314: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
04-07 10:06:11.308: D/dalvikvm(275): VFY: replacing opcode 0x6e at 0x0099
04-07 10:06:11.318: D/dalvikvm(275): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-07 10:06:11.318: D/dalvikvm(275): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-07 10:06:11.488: D/AndroidRuntime(275): Shutting down VM
04-07 10:06:11.488: W/dalvikvm(275): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-07 10:06:11.498: E/AndroidRuntime(275): FATAL EXCEPTION: main
04-07 10:06:11.498: E/AndroidRuntime(275): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myinfo/com.example.myinfo.MainActivity}: java.lang.NullPointerException
04-07 10:06:11.498: E/AndroidRuntime(275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-07 10:06:11.498: E/AndroidRuntime(275): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-07 10:06:11.498: E/AndroidRuntime(275): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-07 10:06:11.498: E/AndroidRuntime(275): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-07 10:06:11.498: E/AndroidRuntime(275): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 10:06:11.498: E/AndroidRuntime(275): at android.os.Looper.loop(Looper.java:123)
04-07 10:06:11.498: E/AndroidRuntime(275): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-07 10:06:11.498: E/AndroidRuntime(275): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 10:06:11.498: E/AndroidRuntime(275): at java.lang.reflect.Method.invoke(Method.java:521)
04-07 10:06:11.498: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-07 10:06:11.498: E/AndroidRuntime(275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-07 10:06:11.498: E/AndroidRuntime(275): at dalvik.system.NativeStart.main(Native Method)
04-07 10:06:11.498: E/AndroidRuntime(275): Caused by: java.lang.NullPointerException
04-07 10:06:11.498: E/AndroidRuntime(275): at com.example.myinfo.MainActivity.onCreate(MainActivity.java:42)
04-07 10:06:11.498: E/AndroidRuntime(275): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-07 10:06:11.498: E/AndroidRuntime(275): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-07 10:06:11.498: E/AndroidRuntime(275): ... 11 more
04-07 10:06:18.688: I/Process(275): Sending signal. PID: 275 SIG: 9
use this code
`InputStream input;
AssetManager assetManager = getAssets();
try {
input = assetManager.open("helloworld.txt");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
// byte buffer into a string
String text = new String(buffer);
dummytext.setText(text);
} catch (IOException e) {
e.printStackTrace();
}
`
This code open an InputStream for the file from assets manager, get size from file content, allocate byte buffer with this size and read file in this buffer, after this create new string from buffer and set this string to your textview

Categories