0

Here's the code:

<RelativeLayout android:id="@+id/TextDisplayLayout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/PrintSetupTextHeight"
    android:layout_marginTop="45dp"
    android:layout_marginBottom="15dp">
</RelativeLayout>

and

public class MainScreen extends ActionBarActivity {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    RelativeLayout infoDisplay = (RelativeLayout) findViewById(R.id.TextDisplayLayout);
    TextView sdCardInfo = new TextView(this);
    sdCardInfo.setId(1);
    sdCardInfo.setText("SD Card Info Here");
    sdCardInfo.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100));
    sdCardInfo.setBackgroundColor(Color.GRAY);
    sdCardInfo.setGravity(Gravity.CENTER);
    infoDisplay.addView(sdCardInfo);


}

I've narrowed down the problem to
infoDisplay.addView(sdCardInfo);

The app works when this part is commented out, but obviously I need this part of the code. Does anybody know why it's not working?

  • did this xml file that you post is for `activity_main_screen`? because i think you need do your job (Add TextView) in Fragment class, and please post logcat file – Shayan Pourvatan May 28 '14 at 04:46
  • The xml file is fragment_main_screen. What fragment class? – user1434459 May 28 '14 at 04:49
  • And what error you are getting ? – Spring Breaker May 28 '14 at 04:51
  • I just get an "Unfortunately, [App] has stopped." in the AVD. – user1434459 May 28 '14 at 04:52
  • Refer this [http://stackoverflow.com/questions/23869019/nullpointerexception-thrown-when-trying-to-findviewbyid/23869046#23869046](http://stackoverflow.com/questions/23869019/nullpointerexception-thrown-when-trying-to-findviewbyid/23869046#23869046) – M D May 28 '14 at 04:54
  • 05-28 00:51:09.542: D/AndroidRuntime(1210): Shutting down VM 05-28 00:51:09.542: W/dalvikvm(1210): threadid=1: thread exiting with uncaught exception (group=0xb2a6fba8) 05-28 00:51:09.552: E/AndroidRuntime(1210): FATAL EXCEPTION: main 05-28 00:51:09.552: E/AndroidRuntime(1210): Process: com.xery.androidprinter, PID: 1210 05-28 00:51:09.552: E/AndroidRuntime(1210): – user1434459 May 28 '14 at 04:56
  • java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xery.androidprinter/com.xery.androidprinter.MainScreen}: java.lang.NullPointerException 05-28 00:51:09.552: E/AndroidRuntime(1210): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 05-28 00:51:09.552: E/AndroidRuntime(1210): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) – user1434459 May 28 '14 at 04:56
  • I bet the above initialization codes should be inside your fragment class. See @Hariharan's answer. – Spring Breaker May 28 '14 at 05:02

2 Answers2

1

Try this..

Change this..

 setContentView(R.layout.activity_main_screen);

to

setContentView(R.layout.fragment_main_screen);

and remove

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
}

also

Change this..

public class MainScreen extends ActionBarActivity

to

public class MainScreen extends Activity

OR

In your PlaceholderFragment inside onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main_screen, container,
            false);
    RelativeLayout infoDisplay = (RelativeLayout) rootView.findViewById(R.id.TextDisplayLayout);
    TextView sdCardInfo = new TextView(getActivity());
    sdCardInfo.setId(1);
    sdCardInfo.setText("SD Card Info Here");
    sdCardInfo.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100));
    sdCardInfo.setBackgroundColor(Color.GRAY);
    sdCardInfo.setGravity(Gravity.CENTER);
    infoDisplay.addView(sdCardInfo);

    return rootView;
}
Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

Try out this code...

public class MainActivity extends Activity {

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

    RelativeLayout infoDisplay = (RelativeLayout) findViewById(R.id.TextDisplayLayout);
    TextView sdCardInfo = new TextView(this);
    sdCardInfo.setId(1);
    sdCardInfo.setText("SD Card Info Here");
    sdCardInfo.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 100));
    sdCardInfo.setBackgroundColor(Color.GRAY);
    sdCardInfo.setGravity(Gravity.CENTER);
    infoDisplay.addView(sdCardInfo);
    }
}
Rohit
  • 2,646
  • 6
  • 27
  • 52