Yogeshwar Shendye
Yogeshwar Shendye

Reputation: 49

TextView is blank until activity is restarted

This code is supposed to change values of the textviews on starting the activity. But it does not. I need to restart the activity manualy. Please help me.

package com.example.chinmay.departmentalapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;

import org.json.JSONArray;
import org.json.JSONObject;

public class StudentDetails extends AppCompatActivity {
TextView rollNo, name, contact1, contact2, avg;
Intent i;


protected void onCreate(Bundle savedInstanceState) {
    i = getIntent();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_student_details);

    rollNo = (TextView)findViewById(R.id.rollNo);
    name = (TextView) findViewById(R.id.studName);
    contact1 = (TextView) findViewById(R.id.contact1);
    contact2 = (TextView) findViewById(R.id.contact2);
    avg = (TextView) findViewById(R.id.avgPresenty);

    rollNo.setText(i.getStringExtra("roll"));
    name.setText(i.getStringExtra("name"));
    contact1.setText(i.getStringExtra("contact"));
    contact2.setText(i.getStringExtra("con2"));
    avg.setText(i.getStringExtra("avgPres"));
}
}

StudentDetails.java looks like this. I tried to solve it but evertything failed. I dont know what's wrong with this code.

Upvotes: 1

Views: 83

Answers (2)

arjun shrestha
arjun shrestha

Reputation: 1425

Write your code below and this will work fine i hope so

setContentView(R.layout.activity_student_details);

Upvotes: 1

Pranay Pujari
Pranay Pujari

Reputation: 35

Try to write

i.getIntent()

After

super.oncreate()
setContentView()

Because this activity is created after

super.oncreate()

is called. For first execution activity is not created when getting the intent. The problem is you are trying to get intent in an activity which has not yet been created. Hope it helps :)

Upvotes: 0

Related Questions