-1

new Android Dev here. Okay so I made a simple intent to take me to another activity. The problem is that it is not able to take me to that specific activity (but it does open other activities just fine.). Here is the error it is giving me:

FATAL EXCEPTION: main
Process: com.shahrukhraza.app, PID: 5628
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.shahrukhraza.app/com.shahrukhraza.app.otherAppActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
    at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:68)
    at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
    at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
    at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
    at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
    at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:188)
    at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:172)
    at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:512)
    at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184)
    at com.shahrukhraza.app.otherAppActivity.<init>(otherAppActivity.java:14)
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 

And this is my MainActivity.java:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_app);
        Button b1 = (Button) findViewById(R.id.btn1);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, otherAppActivity.class);
                startActivity(intent);
            }
        });
    }
}

And this is the java class that is causing problems :

public class otherAppActivity extends AppCompatActivity {

    EditText eText1 = (EditText) findViewById(R.id.eText1);
    EditText eText2 = (EditText) findViewById(R.id.eText2);
    Button btn = (Button) findViewById(R.id.button);
    TextView result = (TextView)findViewById(R.id.textView);

    double n1,n2,sum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other_activity_app);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              n1 = Double.parseDouble(eText1.getText().toString());
              n2 = Double.parseDouble(eText2.getText().toString());
                sum = n1 +n2;
                result.setText(Double.toString(sum));
            }
        });
    }
}

3 Answers3

0

Is your otherAppActivity.class in your manifest file as a activity? Like this

<activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".PromotionMain"
        android:label="Promotions"
        android:theme="@style/AppTheme.NoActionBar"
        >
    </activity>
    <activity
        android:name=".MyWebViewMain"
        android:label="MyWebView"
        android:theme="@style/AppTheme.NoActionBar"
        >
    </activity>

if the class is not set up in your Manifest file like this then the class will never load from a intent. You intent is set up right and should work as far as I can tell

MNM
  • 2,673
  • 6
  • 38
  • 73
0

You should initilize the button and all the stuff in OnCreate or they will be null.

EditText eText1, eText2;
TextView result;
double n1,n2,sum;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.);
    eText1 = (EditText) findViewById(R.id.eText1);
    eText2 = (EditText) findViewById(R.id.eText2);
    btn = (Button) findViewById(R.id.button);
    result = (TextView)findViewById(R.id.textView);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          n1 = Double.parseDouble(eText1.getText().toString());
          n2 = Double.parseDouble(eText2.getText().toString());
            sum = n1 +n2;
            result.setText(Double.toString(sum));
        }


    });
}
Abdelilah El Aissaoui
  • 4,204
  • 2
  • 27
  • 47
0

in the otherActivity, in the setContentView you only have R.layout. without specifying the layout id. This might cause the error

minh nguyen
  • 89
  • 3
  • 3