[英]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. 嗨,我是Java的新手,正在尝试开发一个已经存在的Anti-ragging应用程序,以支持更新的api,例如姜饼的棒棒糖。 I have decompiled the apk and extracted the source code and built it in Gradle system.
我已经反编译了apk并提取了源代码,并在Gradle系统中构建了它。
Although the source code is correct,I am getting 3 errors in java code...not been able to figure out from a week. 尽管源代码正确,但是我在Java代码中遇到了3个错误...一周之内无法弄清楚。
The app contains 4 screens. 该应用程序包含4个屏幕。 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.
主屏幕有一个发送按钮来发送消息,第二个设置屏幕有两个按钮来设置我们要发送的消息并设置我们要发送消息的联系人...其他两个屏幕是的自定义对话框布局Set Meaasge和联系方式。
No errors in this main Java file just giving it for a reference 该主要Java文件中没有错误,仅供参考
MainActivity.java: 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: 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. 我无意为该应用程序赢得任何荣誉,因为我还没有开发它……只是想使用用户界面并尝试一下新的api。
The return
statement will cause the onCreateDialog
method to return
and any subsequent lines will not be executed. return
语句将导致onCreateDialog
方法return
并且任何后续行都将不执行。 That's why you get the unreachable code
error message. 这就是为什么您收到
unreachable code
错误消息的原因。 Similarly, the break
statement will cause the for
loop to end. 同样,
break
语句将导致for
循环结束。 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. 最后,您将收到
/*Error: Variable Str2 might not have been initialized*/
错误,因为必须首先初始化所有局部变量,然后才能首次使用它们。
For /*Error: Variable Str2 might not have been initialized*/
: 对于
/*Error: Variable Str2 might not have been initialized*/
:
Change String str2;
更改
String str2;
to String str2 = "";
到
String str2 = "";
As for the other two errors, you have a return;
至于其他两个错误,您有
return;
and break;
和
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. 无条件
return
和break
之后的任何语句都将无法访问,即,那里的代码(后续行)将无法执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.