简体   繁体   中英

Why do I get an “unreachable code” and “variable not initialized” compilation error?

Hi I am New to java and trying to develop an already existing Anti-ragging application to support newer api like lollipop from gingerbread. I have decompiled the apk and extracted the source code and built it in Gradle system.

Although the source code is correct,I am getting 3 errors in java code...not been able to figure out from a week.

The app contains 4 screens. The main screen is with a send button to send message, the second settings screen with two buttons to set message which we want to send and set contacts whom we want to send the message... the other two screens are the custom dialog layouts of the Set Meaasge & contact.

No errors in this main Java file just giving it for a reference

MainActivity.java:

package com.eminem.sharath.antiragging;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    String[] arrr1;
    private ImageButton ib1;
    private double latitude;
    private LocationManager lm;
    private double longitude;
    private SharedPreferences sp;
    private SharedPreferences sp1;

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setLogo(R.mipmap.ic_launcher);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayUseLogoEnabled(true);

        this.ib1 = (ImageButton)findViewById(R.id.imageButton1);
        this.lm = (LocationManager)getSystemService(LOCATION_SERVICE);
        this.ib1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramAnonymousView) {
                MainActivity.this.sp = MainActivity.this.getSharedPreferences("demo", 1);
                final String str1 = MainActivity.this.sp.getString("aaa", "");
                MainActivity.this.sp1 = MainActivity.this.getSharedPreferences("sdat", 1);
                String str2 = MainActivity.this.sp1.getString("snum", "");
                MainActivity.this.arrr1 = str2.split(",");
                Toast.makeText(MainActivity.this.getApplicationContext(), str2 + str1, Toast.LENGTH_SHORT).show();
                System.out.println("LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL" + str2 + str1);
                MainActivity.this.lm.requestLocationUpdates("gps", 1000L, 5.0F, new LocationListener() {
                    public void onLocationChanged(Location paramAnonymous2Location) {
                        Location localLocation = MainActivity.this.lm.getLastKnownLocation("gps");
                        MainActivity.this.latitude = localLocation.getLatitude();
                        MainActivity.this.longitude = localLocation.getLongitude();
                        Toast.makeText(MainActivity.this.getApplicationContext(), "Latitude:" + MainActivity.this.latitude + "\n" + "Longitude:" + MainActivity.this.longitude, Toast.LENGTH_SHORT).show();
                        String str = "http://maps.google.com/maps?=" + MainActivity.this.latitude + "," + MainActivity.this.longitude;
                        for (int i = 0; ; i++) {
                            if (i >= MainActivity.this.arrr1.length) {
                                return;
                            }
                            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + MainActivity.this.arrr1);
                            SmsManager.getDefault().sendTextMessage(MainActivity.this.arrr1[i], null, str1 + " Come at this location" + str, null, null);
                        }
                    }

                    public void onProviderDisabled(String paramAnonymous2String) {
                    }

                    public void onProviderEnabled(String paramAnonymous2String) {
                    }

                    public void onStatusChanged(String paramAnonymous2String, int paramAnonymous2Int, Bundle paramAnonymous2Bundle) {
                    }
                });
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        startActivity(new Intent(getApplicationContext(), Settingss.class));
        return super.onOptionsItemSelected(item);
    }
}

The three errors are in this settings file...i have commented the error name beside the error for reference.

Settings.java:

package com.eminem.sharath.antiragging;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView.CommaTokenizer;
import android.widget.Toast;
import java.io.PrintStream;
import java.util.ArrayList;

public class Settingss extends AppCompatActivity
{
    private ArrayList<String> alist = new ArrayList();
    private Button b1;
    private Button b2;
    SharedPreferences.Editor ed;
    private String setnum = "";
    SharedPreferences sp;

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.contact);

        this.b1 = ((Button)findViewById(R.id.button1));
        this.b2 = ((Button)findViewById(R.id.button2));
        this.b1.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View paramAnonymousView)
            {
                Settingss.this.showDialog(1);
            }
        });
        this.b2.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View paramAnonymousView)
            {
                Settingss.this.showDialog(2);
            }
        });
    }

    protected Dialog onCreateDialog(int paramInt) {
        if (paramInt == 1) {
            final Dialog localDialog1 = new Dialog(this);
            localDialog1.setContentView(R.layout.tosetmessage);
            localDialog1.setTitle("Set Message");
            final EditText localEditText = (EditText)findViewById(R.id.editText1);
            Button localButton1 = (Button)findViewById(R.id.button1);
            Button localbutton2 = ((Button) findViewById(R.id.button2));
            localbutton2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View paramAnonymousView) {
                    Settingss.this.sp = Settingss.this.getSharedPreferences("demo", 2);
                    Settingss.this.ed = Settingss.this.sp.edit();
                    Settingss.this.ed.putString("aaa", localEditText.getText().toString());
                    Settingss.this.ed.commit();
                    localDialog1.dismiss();
                }
            });
            localButton1.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View paramAnonymousView)
                {
                    localDialog1.dismiss();
                }
            });
            localDialog1.show();
        }
        while(true)
        {
            return super.onCreateDialog(paramInt);
            /*Error: unreachale statement*/     if (paramInt == 2)
            {
                final Dialog localDialog2 = new Dialog(this);
                localDialog2.setContentView(R.layout.multiautotext);
                localDialog2.setTitle("Set Contacts");
                final MultiAutoCompleteTextView localMultiAutoCompleteTextView = (MultiAutoCompleteTextView)localDialog2.findViewById(R.id.multiAutoCompleteTextView1);
                Button localButton2 = (Button)localDialog2.findViewById(R.id.button1);
                Button localButton3 = (Button)localDialog2.findViewById(R.id.button2);
                Cursor localCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, "display_name ASC");
                if (localCursor.moveToFirst())
                {
                    do
                    {
                        String str1 = localCursor.getString(localCursor.getColumnIndex("display_name"));
                        String str2 = localCursor.getString(localCursor.getColumnIndex("data1"));
                        String str3 = str1 + "%" + str2;
                        this.alist.add(str3);
                    } while (localCursor.moveToNext());
                    ArrayAdapter localArrayAdapter = new ArrayAdapter(this, R.layout.simple_list_item, this.alist);
                    localMultiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
                    localMultiAutoCompleteTextView.setAdapter(localArrayAdapter);
                }
                localButton2.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View paramAnonymousView)
                    {
                        String[] arrayOfString = localMultiAutoCompleteTextView.getText().toString().split(",");
                        int i = 0;
                        if (i >= arrayOfString.length)
                        {
                            System.out.println("************************" + Settingss.this.setnum);
                            Toast.makeText(Settingss.this.getApplicationContext(), Settingss.this.setnum, Toast.LENGTH_SHORT).show();
                            Settingss.this.sp = Settingss.this.getSharedPreferences("sdat", 2);
                            Settingss.this.ed = Settingss.this.sp.edit();
                            Settingss.this.ed.putString("snum", Settingss.this.setnum);
                            Settingss.this.ed.commit();
                            Settingss.this.setnum = "";
                            Settingss.this.finish();
                            return;
                        }
                        String str2;
                        if (arrayOfString[i].contains("%"))
                            str2 = arrayOfString[i].split("%")[1];
                        String str1;
                        for (Settingss.this.setnum = (Settingss.this.setnum + /*Error: Variable Str2 might not have been initialized*/  str2 + ",");; Settingss.this.setnum = (Settingss.this.setnum + str1 + ","))
                        {
                            i++;
                            break;
                            /*Error: unreachale statement*/     str1 = arrayOfString[i];
                        }
                    }
                });
                localButton3.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View paramAnonymousView)
                    {
                        localDialog2.dismiss();
                    }
                });
                localDialog2.show();
            }
        }
    }
}

Please help me debug the application.

I have no intention to take any credits for the application because as I haven't developed it...just want to play with the user interface and give it a taste of new api.

The return statement will cause the onCreateDialog method to return and any subsequent lines will not be executed. That's why you get the unreachable code error message. Similarly, the break statement will cause the for loop to end. Any subsequent lines will not be executed.

Finally, you get a /*Error: Variable Str2 might not have been initialized*/ error because all local variables must be initialized before they can be used for the first time.

For /*Error: Variable Str2 might not have been initialized*/ :

Change String str2; to String str2 = "";

As for the other two errors, you have a return; and break; before those lines.
Any statements after a unconditional return and break will be unreachable, ie there is no way that the code there (subsequent lines) will execute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM