0

Well, I'm doing this app just for trial. And there's this error. It says:

04-21 04:11:41.618: E/AndroidRuntime(333): FATAL EXCEPTION: main
04-21 04:11:41.618: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.windteste/com.example.windteste.MainActivity}: java.lang.NullPointerException
04-21 04:11:41.618: E/AndroidRuntime(333): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
04-21 04:11:41.618: E/AndroidRuntime(333): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-21 04:11:41.618: E/AndroidRuntime(333): at android.app.ActivityThread.access2300ドル(ActivityThread.java:125)

..and a lot more lines of error. So, these are the two methods (Dfig and MainActivity):

package com.example.windteste;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Dfig extends Activity{
 @Override
 protected void onCreate(Bundle blablu) {
 // TODO Auto-generated method stub
 super.onCreate(blablu);
 setContentView(R.layout.activity_main);
 MediaPlayer windsound = MediaPlayer.create(Dfig.this, R.raw.windsound);
 windsound.start();
 Thread timer = new Thread(){
 public void run(){
 try{
 sleep(4000);
 }catch (InterruptedException e){
 e.printStackTrace();
 }finally{
 Intent openMain = new Intent("com.example.windteste.MAIN");
 startActivity(openMain);
 }
 }
 };
 timer.start();
 windsound.release();
 }
}
 package com.example.windteste;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.text.InputType;
----------------------------------------------------------------------------------
public class MainActivity extends Activity {
 Button check = (Button) findViewById(R.id.calculator);
 final EditText UserInput = (EditText) findViewById(R.id.windspeed);
 final TextView P = (TextView) findViewById(R.id.potencia);
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.layout2);
 UserInput.setInputType(InputType.TYPE_CLASS_NUMBER);
 check.setOnClickListener(new View.OnClickListener() {
 public void onClick(View v) {
 Number Ws = (Number) UserInput.getText();
 P.setText("Your total is " + Ws);
 }
 });
 }
 @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;
 } 
}

Well, I hope you can help me.
Thank you!

Bananeweizen
22.1k8 gold badges74 silver badges92 bronze badges
asked Apr 21, 2013 at 4:38
3
  • Is the activity declared in your Manifest? Commented Apr 21, 2013 at 4:39
  • 1
    HINT: always look at the part of the error message with a line number for your code. That's usually a good way to zero in on the problem. SUGGESTION: Don't initialize "check", "UserInput" and "P" outside of a method. All then inside a method, after you've called "setContentView". Commented Apr 21, 2013 at 4:44
  • Yeah, but it had some weird numbers... It showed java:1249, but my code doesn't even have so many lines. Thank you. Commented Apr 21, 2013 at 5:09

2 Answers 2

2

Try:

public class MainActivity extends Activity {
 Button check;
 final EditText UserInput;
 final TextView P;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.layout2);
 check = (Button) findViewById(R.id.calculator);
 UserInput = (EditText) findViewById(R.id.windspeed);
 P = (TextView) findViewById(R.id.potencia);
 //Rest is same 

You cannot use findViewById() until after setContentView() has been called.

answered Apr 21, 2013 at 4:40
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, it helped! But know it has another problem in the line Number Ws = (Number) UserInput.getText(); P.setText("Your total is " + Ws);.
@user2303633 getText() will return a string. String s= UserInput.getText(); then set P.setText("Your total is " + s);.
Yes, but I wanted I number input from user. But I found it how to do it. You saved dude.
you can use Integer.parseInt(s) for the same. put it in a try catch block of the input is not a number will throw a numberformatexception or android:inputType="number" specify the attribute for edittext
0

Make sure both the activities are listed in the manifest. What it looks like is MainActivity is not listed in your manifest file.

answered Apr 21, 2013 at 4:46

1 Comment

Yes, they're listed. Thank you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.