[英]Android Studio - Execution failed for task ':app:compileDebugJavaWithJavac'
[英]android studio: 2 errors: [cannot find symbol variable sharedPref] && [Execution failed for task ':app:compileDebugJavaWithJavac']
我在MainActivity.java
遇到兩個錯誤,這可能是什么原因引起的? 在我的舊計算機上工作正常。 試圖在eclipse ADT
上運行它,就像它在舊計算機上的工作方式一樣,但是仍然給我同樣的錯誤。
錯誤1:找不到符號變量sharedPref
錯誤2:任務compileDebugJavaWithJavac的執行失敗。
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
public class MainScreenActivity extends Activity{
Button btnViewProducts;
Button btnNewProduct;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
// Buttons
btnViewProducts = (Button) findViewById(R.id.btnViewProducts);
btnNewProduct = (Button) findViewById(R.id.btnCreateProduct);
// view products click event
btnViewProducts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// display AlertDialog with EditText to input IP address
AlertDialog.Builder IPalert = new AlertDialog.Builder(MainScreenActivity.this);
IPalert.setTitle("Enter IP Address");
// Set an EditText view to get user input
final EditText inputIP = new EditText(MainScreenActivity.this);
inputIP.setText("192.168.0.115/tapmanager");
IPalert.setView(inputIP);
IPalert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
final String ip = inputIP.getText().toString();
if(ip.matches("")){
inputIP.requestFocus();
inputIP.setError("Please Enter Valid IP Address!");
Toast.makeText(MainScreenActivity.this, "Please Enter Valid IP Address", Toast.LENGTH_LONG).show();
}else{
//Toast.makeText(MainActivity.this, ip, Toast.LENGTH_LONG).show();
SharedPreferences.Editor editOffLine = sharedPref.edit();
editOffLine.putBoolean("isOnline", false);
editOffLine.putString("ip_address", ip);
editOffLine.commit();
// Launching All products Activity
Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
startActivity(i);
}
}
});
IPalert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
IPalert.show();
//finish();
//break; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
});
// view products click event
btnNewProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launching create new product activity
Intent i = new Intent(getApplicationContext(), NewProductActivity.class);
startActivity(i);
}
});
}
添加代碼
SharedPreferences sharedPref = getSharedPreferences(
"MyPreferenceName", Context.MODE_PRIVATE);
之前
SharedPreferences.Editor editOffLine = sharedPref.edit();
editOffLine.putBoolean("isOnline", false);
editOffLine.putString("ip_address", ip);
editOffLine.commit();
並嘗試升級成績文件中的buildToolsVersion
compileSdkVersion 23
buildToolsVersion "23.0.1"
希望對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.