2017-02-14 52 views
0

当我尝试将程序运行到Android Studio时,我需要获得帮助。该应用程序是一个应用程序页面,可让用户上传任何图片并在上传后将其保存到应用程序中,并且我需要获取有关我收到的错误的帮助。Android Studio - 代码中的错误

这是我的Java代码(来自@覆盖到了最后,我得到一个错误 “类或接口预期”):

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button b = (Button) findViewById(R.id.button1); 
    b.setOnClickListener(new OnClickListener() { 


    EditText text = (EditText)findViewById(R.id.editText1); 
    EditText text2 = (EditText)findViewById(R.id.editText2); 



@Override 
public void onClick(View v) { 

final String name = text.getText().toString(); 
final String placeName = text2.getText().toString(); 

    String place = placeName.substring(0,3); 
    String direct = name + place ; 

    File folder = new File("/sdcard/CameraTest/" + direct + "/"); 
    folder.mkdirs(); 

    Intent myIntent = new Intent(CameraTestActivity.this, Press.class); 
    myIntent.putExtra("key", "/sdcard/CameraTest/" + direct + "/"); 
    startActivity(myIntent); 

    } 
    }); 

这是我的activity_upload.xml代码:

?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 

android:orientation="vertical" 

android:layout_width="fill_parent" 

android:layout_height="fill_parent" 

> 

<Button 

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="Click To Upload File" 

    android:id="@+id/uploadButton" 

    /> 



<TextView 

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="" 

    android:id="@+id/messageText" 

    android:textColor="#000000" 

    android:textStyle="bold" 

    /> 

</LinearLayout> 

最后但并非最不重要的,这是AndroidManifest.xml中的代码:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.lediskodra.sendesigniii" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="15" 
    android:targetSdkVersion="25" /> 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission 
android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission 
android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

<uses-feature 
    android:name="android.hardware.camera" 
    android:required="true" /> 

<application 
    android:name="com.android.tools.fd.runtime.BootstrapApplication" 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.picupload.MainActivity" 
     android:configChanges="orientation|screenSize|screenLayout" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" \ 
/> 
     </intent-filter> 
    </activity> 

    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
</application> 

</manifest> 
+0

不要忘记实际的错误信息。 – AntonH

+0

您错过了onCreate结尾处的}。在onClick结束时,尽管这可能只是停止复制粘贴的地方。 –

+3

[类或接口预期]的可能的重复(http://stackoverflow.com/questions/28978782/class-or-interface-expected) –

回答

1

您需要关闭的onCreate用}。把它放在onClick后

public class Foo { 

    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button b = (Button) findViewById(R.id.button1); 
    b.setOnClickListener(new OnClickListener() { 


     EditText text = (EditText) findViewById(R.id.editText1); 
     EditText text2 = (EditText) findViewById(R.id.editText2); 


     @Override 
     public void onClick(View v) { 

      final String name = text.getText().toString(); 
      final String placeName = text2.getText().toString(); 

      String place = placeName.substring(0, 3); 
      String direct = name + place; 

      File folder = new File("/sdcard/CameraTest/" + direct + "/"); 
      folder.mkdirs(); 

      Intent myIntent = new Intent(CameraTestActivity.this, Press.class); 
      myIntent.putExtra("key", "/sdcard/CameraTest/" + direct + "/"); 
      startActivity(myIntent); 

     } 

    }); 
} 
} 
+0

嘿,我只是这样做,但仍然得到错误(同一个)你可以尝试运行它在你的android工作室,看看你得到什么? – HoundsofJustice

+0

让我看一下,你可以尝试使用更新后的代码编辑原始帖子吗?请尝试包含完整的课程代码,而不仅仅是其中的一部分。 –

+0

嘿德鲁,那是原始帖子中的完整代码?每次我尝试运行它时,都会出现3个错误。尝试将原始帖子中的代码粘贴到android studio并查看是否可以找到它的错误。非常感谢任何帮助! – HoundsofJustice