0

Okay, I am beyond confused here. I'm new to Java/Android programming, and I'm teaching myself, so bear with me.

Basically, my entire onCreate is made up of a giant if/else statement. I have a couple methods that check if various applications are installed on the device, and then I have a method called RunAsRoot that simply executes a shell command as su. Here's the full onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CharSequence message = null;
    Button button = (Button) findViewById(R.id.button1);
    boolean installed = appInstalledOrNot("x");

    if(installed) {
        message = "whateverhere";
        button.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) {
                RunAsRoot(commands);
            }
        });
    }
    else {
        message = "whateverhere";
        button.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "You don't have packagenamehere installed, sorry!", Toast.LENGTH_LONG).show();
            }
        });
    }   

    TextView textView = (TextView )findViewById(R.id.textView);
    textView.setTextSize(20);
    textView.setText(message);
}

The whole thing is pretty basic... If the package is installed, okay cool, do the command. If it isn't installed, too bad, you don't get to run the commands. And it works great. But here's my problem... I would like to check if a device is rooted at the start of the app. If it is rooted, proceed to the if/else that I posted above. If not rooted, setContentView to a different layout. I can't figure anything out! Literally nothing is working.

I have a method called isRooted() that checks if su is present in /system/xbin. But I can't figure out how to incorporate that into my if/else statement. I'd like it to check for root before anything else happens, and if root isn't found, just switch the bloody layout and that's it. But everything crashes due to a NullPointerException.

Here is isRooted():

public boolean isRooted() {
    try {
        Process process = new ProcessBuilder()
            .command("/system/xbin/su")
            .redirectErrorStream(false)
            .start();
    } catch (IOException e) { 
        return true;
    }
    return false;
}

I guess I'm just confused as to where to go from here. Should I just wrap my entire if/else in another if/else that checks for root? And I guess another thing is, how do I call isRooted()? I think I'm doing it wrong:

if (isRooted()) {
    //everything here
}

else {
    setContentView(R.layout.notrooted);
}

Logcat output shows that the NullPointerException is occurring in my onCreate, which I assume is directly due to the if (isRooted())

Any suggestions on where I should go from here?

TL;DR: I want to check if I can run something as root, and if I can, continue program, if I can't, change the layout and just stop.

EDIT: Here is all I'm getting out of LogCat:

07-24 09:00:51.257: E/AndroidRuntime(16282): Process: com.test.app, PID: 16282
07-24 09:00:51.257: E/AndroidRuntime(16282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.app/com.test.app.MainActivity}: java.lang.NullPointerException
07-24 09:00:51.257: E/AndroidRuntime(16282): at com.test.app.MainActivity.onCreate(MainActivity.java:126)
IAmTheSquidward
  • 562
  • 6
  • 22
  • 3
    "Logcat output shows that the NullPointerException is occurring in my onCreate" -- the stack trace should point you to the exact line in your code that is triggering the exception. – CommonsWare Jul 24 '14 at 13:05
  • I guess I don't know where to find that. I posted my LogCat output in an edit. – IAmTheSquidward Jul 24 '14 at 13:08
  • It is happening in line 126 of `MainActivity.java`, as is noted in the bottom line of the portion of the stack trace you have in your question. – CommonsWare Jul 24 '14 at 13:12
  • ...wow okay all I needed was you guys to point out what the heck those numbers meant. Line #126 was `textView.setTextSize`, which is present only in the first layout. It was outside of `if isRooted())`, so even if the device was **not** rooted (and therefore, layout change occurred), I was still changing the font size...**of a non-existent textview**. I just put it inside of `if isRooted())`. Thanks guys, everything's working great now! – IAmTheSquidward Jul 24 '14 at 13:18
  • 1
    Good to hear. Logcat has tons of great info, consider reading a tutorial just about what it contains and how to read it. It will help you a lot in the future. – takendarkk Jul 24 '14 at 13:26
  • Will do! I appreciate the help! – IAmTheSquidward Jul 24 '14 at 13:28

1 Answers1

0

I've faced issues with running su commands in Android. Here was my question: Working Directory : null environment when running Process.Builder on android

Basically I figured out if I had root access or not based on the output I would get from using the Process.Builder. You may be able to use that to determine if you have root privileges or not.

Community
  • 1
  • 1
asm
  • 837
  • 1
  • 16
  • 41