【问题标题】:Android Studio - errors in the code [closed]Android Studio - 代码中的错误[关闭]
【发布时间】:2017-02-14 15:26:29
【问题描述】:

我在尝试将程序运行到 Android Studio 时遇到的错误需要帮助。该应用程序是一个应用程序页面,允许用户上传任何图片并在上传后将其保存到应用程序中,我需要帮助解决我遇到的错误。

这是我的 Java 代码(从@override 到最后,我得到一个错误“需要类或接口”):

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>

【问题讨论】:

  • 不要忘记实际的错误信息。
  • 您在 onCreate 末尾缺少 }。在您的 onClick 结束时,虽然这可能只是您停止复制粘贴的地方。
  • 分享完整的活动代码和正确的缩进以及错误日志
  • 这些 EditTextView 是从哪里来的?

标签: java android android-studio compiler-errors


【解决方案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);

        }

    });
 }
}

【讨论】:

  • 嘿,我刚刚这样做了,但仍然出现错误(同一个)你可以尝试在你的 android studio 上运行它,看看你得到了什么?
  • 让我看看,你能尝试用更新的代码编辑你的原始帖子吗?请尝试包含您的完整课程代码,而不仅仅是部分代码。
  • 嘿,Drew,这是原帖中的完整代码吗?每次我尝试运行它时,都会出现 3 个错误。尝试将原始帖子中的代码粘贴到 android studio 中,看看是否能找到它的错误。非常感谢任何帮助!
  • HoundsofJustic,我不确定你是不是不小心把它漏掉了,但你的整个外层都不见了。查看我编辑的答案,它适用于 Android Studio。
猜你喜欢
  • 1970-01-01
  • 2017-02-12
  • 2018-12-17
  • 1970-01-01
  • 2017-07-27
  • 2013-01-12
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多